protected function LanguageCookieSubscriber::getLanguage in Language Cookie 8
Helper method that gets the language code to set the cookie to.
Loops through all available language negotiation methods with higher priority than the Language Cookie method itself.
Return value
\Drupal\Core\Language\LanguageInterface|null An string with the language code or FALSE.
See also
\Drupal\language_cookie\LanguageCookieSubscriber::setLanguageCookie()
1 call to LanguageCookieSubscriber::getLanguage()
- LanguageCookieSubscriber::setLanguageCookie in src/
EventSubscriber/ LanguageCookieSubscriber.php - Event callback for setting the language cookie.
File
- src/
EventSubscriber/ LanguageCookieSubscriber.php, line 109
Class
- LanguageCookieSubscriber
- Provides a LanguageCookieSubscriber.
Namespace
Drupal\language_cookie\EventSubscriberCode
protected function getLanguage() {
if (!$this->languageManager
->isMultilingual()) {
return $this->languageManager
->getDefaultLanguage();
}
$config = $this->configFactory
->get('language_cookie.negotiation');
// In the install hook for this module, we assume the interface language
// will be used to set the cookie. If you want to use another language
// negotiation type instead (ie. content/URL), you can use "language_type"
// config key.
$type = $config
->get('language_type');
// Get all methods available for this language type.
$methods = $this->languageNegotiator
->getNegotiationMethods($type);
// Sort the language negotiation methods by weight.
uasort($methods, '\\Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
// We ignore this language method or else it will always return a language.
unset($methods[LanguageNegotiationSelected::METHOD_ID]);
foreach ($methods as $method_id => $method_definition) {
// Do not consider language providers with a lower priority than the
// cookie language provider, nor the cookie provider itself.
if ($method_id === LanguageNegotiationCookie::METHOD_ID) {
return NULL;
}
$language_id = $this->languageNegotiator
->getNegotiationMethodInstance($method_id)
->getLangcode($this->event
->getRequest());
if (!empty($language_id)) {
return $this->languageManager
->getLanguage($language_id);
}
}
// If no other language was found, use the default one.
return $this->languageManager
->getDefaultLanguage();
}