function LanguageNegotiator::initializeType

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

File

core/modules/language/src/LanguageNegotiator.php, line 128

Class

LanguageNegotiator
Class responsible for performing language negotiation.

Namespace

Drupal\language

Code

public function initializeType($type) {
    $language = NULL;
    if ($this->currentUser) {
        // Execute the language negotiation methods in the order they were set up
        // and return the first valid language found.
        foreach ($this->getEnabledNegotiators($type) as $method_id => $info) {
            if (!isset($this->negotiatedLanguages[$method_id])) {
                try {
                    $this->negotiatedLanguages[$method_id] = $this->negotiateLanguage($type, $method_id);
                } catch (PluginNotFoundException $e) {
                    // If a plugin is not found, log the error so user can handle it.
                    $this->getLogger('language')
                        ->error($e->getMessage());
                }
            }
            // Since objects are references, we need to return a clone to prevent
            // the language negotiation method cache from being unintentionally
            // altered. The same methods might be used with different language types
            // based on configuration.
            $language = !empty($this->negotiatedLanguages[$method_id]) ? clone $this->negotiatedLanguages[$method_id] : NULL;
            if ($language) {
                $this->getNegotiationMethodInstance($method_id)
                    ->persist($language);
                break;
            }
        }
    }
    if (!$language) {
        // If no other language was found use the default one.
        $language = $this->languageManager
            ->getDefaultLanguage();
        $method_id = static::METHOD_ID;
    }
    return [
        $method_id => $language,
    ];
}

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