function language_hierarchy_language_fallback_candidates_alter in Language Hierarchy 2.x
Same name and namespace in other branches
- 8 language_hierarchy.module \language_hierarchy_language_fallback_candidates_alter()
- 7 language_hierarchy.module \language_hierarchy_language_fallback_candidates_alter()
Implements hook_language_fallback_candidates_alter().
File
- ./
language_hierarchy.module, line 24 - Add sublanguage handling functionality to Drupal.
Code
function language_hierarchy_language_fallback_candidates_alter(array &$candidates, array $context) {
if (empty($context['langcode'])) {
return;
}
$attempted_langcode = $context['langcode'];
$candidates = [];
// Record which languages have been iterated over, so loops can be avoided.
$iterated = [];
/** @var Drupal\language\Entity\ConfigurableLanguage $language */
$language = ConfigurableLanguage::load($attempted_langcode);
while (!empty($language) && !in_array($language
->getId(), $iterated, TRUE)) {
$iterated[] = $language
->getId();
$fallback_langcode = $language
->getThirdPartySetting('language_hierarchy', 'fallback_langcode', '');
// Include this candidate if there was a fallback language and it was not
// the same as the original langcode (which LocaleLookup already tried) and
// if it is not already in the list. Avoid endless loops and fruitless work.
if (!empty($fallback_langcode) && $attempted_langcode != $fallback_langcode && !isset($candidates[$fallback_langcode])) {
$candidates[$fallback_langcode] = $fallback_langcode;
$language = ConfigurableLanguage::load($fallback_langcode);
}
else {
$language = NULL;
}
}
// If the fallback context is not locale_lookup, allow
// LanguageInterface::LANGCODE_NOT_SPECIFIED as a final candidate after the
// normal fallback chain, and put the attempted language as the top candidate.
// LocaleLookup would already have tried the attempted language, and should
// only be based on explicit configuration. Only languages within this
// fallback chain are allowed otherwise.
if (empty($context['operation']) || $context['operation'] != 'locale_lookup') {
$candidates = [
$attempted_langcode => $attempted_langcode,
] + $candidates;
$candidates[LanguageInterface::LANGCODE_NOT_SPECIFIED] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
}
}