Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
3.7 kB
1
Indexable
Never
/**
     * Get language toggled site URL if available.
     *
     * @return string
     *   URL of toggled site.
     */
    public function getLanguageToggleData()
    {
        $hearsayClientCustomization = new HearsayClientCustomization();
        $node = \Drupal::routeMatch()->getParameter('node');
        $nodeType = $term = $slugNode = $nid = $alias = $returnData = '';
        if ($node != null) {
            // For preview mode  - use preview configuration.
            if ($this->siteTools->isPreview() == true) {
                $slug = $this->siteTools->queryString('slug');
                // Get base slug language.
                $slugLanguage = $hearsayClientCustomization->getBaseSlugLanguage($slug);
                $returnData = [
                    'toggleUrl' => $alias,
                    'currentUrlLanguage' => $slugLanguage,
                ];
            } else {
                $nodeType = $node->getType();
                // Get current base slug data.
                $slugDataByNode = (object)$this->getSlugByNode();
                // Get slug for en/fr site if exist.
                if ($slugDataByNode->field_baseslug_default_language->value == 'English') {
                    // Create slug to check if en/fr site exist or not.
                    $languageToFind = 'French';
                } elseif ($slugDataByNode->field_baseslug_default_language->value == 'French') {
                    // Create slug to check if en/fr site exist or not.
                    $languageToFind = 'English';
                }
                // Get slug for en/fr site if exist.
                $term = \Drupal::entityQuery('taxonomy_term')->accessCheck(false)->condition('vid', 'base_slugs')->condition('name', $slugDataByNode->name->value)->condition('field_baseslug_default_language', $languageToFind)->execute();
                $term = reset($term);
                $otherLanguageSlug = Term::Load($term);
                if ($otherLanguageSlug) {
                    $node = \Drupal::routeMatch()->getParameter('node');
                    $nodeType = substr($node->getType(), 0, -3);
                    $languageAbbr = $hearsayClientCustomization->getLanguageAbbreviation($languageToFind);
                    // Get all the node types for EN and FR.
                    $slugNode = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
                        'type' => $nodeType . '_' . $languageAbbr,
                        'field_ref_taxo_base_slug' => $term,
                    ]);
                    if ($slugNode) {
                        $nid = reset($slugNode)->id();
                        $alias = \Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $nid);
                    }
                    if ($otherLanguageSlug->field_path_url->value) {
                        $parts = parse_url($otherLanguageSlug->field_path_url->value);
                        $domain = $parts['scheme'] . '://' . $parts['host'] . $alias;
                    }
                }
                $currentPageUrl = Xss::filter(\Drupal::request()->getUri());
                $returnData = [
                    'toggleUrl' => $alias,
                    'currentPageUrl' => $currentPageUrl,
                    'currentUrlLanguage' => $slugDataByNode->field_baseslug_default_language->value,
                    'otherLanguageUrl' => $domain ?? '',
                    'languageAbbr' => strtoupper($hearsayClientCustomization->getLanguageAbbreviation($slugDataByNode->field_baseslug_default_language->value)),
                ];
            }
        }
        return $returnData;
    }
Leave a Comment