function ConfigurableLanguageManager::getLanguageSwitchLinks

Same name in other branches
  1. 9 core/modules/language/src/ConfigurableLanguageManager.php \Drupal\language\ConfigurableLanguageManager::getLanguageSwitchLinks()
  2. 8.9.x core/modules/language/src/ConfigurableLanguageManager.php \Drupal\language\ConfigurableLanguageManager::getLanguageSwitchLinks()
  3. 10 core/modules/language/src/ConfigurableLanguageManager.php \Drupal\language\ConfigurableLanguageManager::getLanguageSwitchLinks()

Overrides LanguageManager::getLanguageSwitchLinks

File

core/modules/language/src/ConfigurableLanguageManager.php, line 405

Class

ConfigurableLanguageManager
Overrides default LanguageManager to provide configured languages.

Namespace

Drupal\language

Code

public function getLanguageSwitchLinks($type, Url $url) {
    if ($this->negotiator) {
        foreach ($this->negotiator
            ->getNegotiationMethods($type) as $method_id => $method) {
            if (is_subclass_of($method['class'], LanguageSwitcherInterface::class)) {
                $original_languages = $this->negotiatedLanguages;
                $result = $this->negotiator
                    ->getNegotiationMethodInstance($method_id)
                    ->getLanguageSwitchLinks($this->requestStack
                    ->getCurrentRequest(), $type, $url);
                if (!empty($result)) {
                    // Allow modules to provide translations for specific links.
                    $this->moduleHandler
                        ->alter('language_switch_links', $result, $type, $url);
                    $result = array_filter($result, function (array $link) : bool {
                        $url = $link['url'] ?? NULL;
                        $language = $link['language'] ?? NULL;
                        if ($language instanceof LanguageInterface) {
                            $this->negotiatedLanguages[LanguageInterface::TYPE_CONTENT] = $language;
                            $this->negotiatedLanguages[LanguageInterface::TYPE_INTERFACE] = $language;
                        }
                        try {
                            return $url instanceof Url && $url->access();
                        } catch (\Exception) {
                            return FALSE;
                        }
                    });
                    $this->negotiatedLanguages = $original_languages;
                    $links = (object) [
                        'links' => $result,
                        'method_id' => $method_id,
                    ];
                    break;
                }
            }
        }
    }
    return $links ?? NULL;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.