You are here

function language_hierarchy_get_ancestors in Language Hierarchy 2.x

Same name and namespace in other branches
  1. 8 language_hierarchy.module \language_hierarchy_get_ancestors()
  2. 7 language_hierarchy.module \language_hierarchy_get_ancestors()

Returns ancestors language code of the provided language.

Parameters

\Drupal\language\ConfigurableLanguageInterface $language: The language to get ancestors for.

Return value

array Ordered array with all ancestors, most specific on the top.

3 calls to language_hierarchy_get_ancestors()
LanguageHierarchyConfigFactoryOverride::getFallbackChainFromConfigEntities in src/Config/LanguageHierarchyConfigFactoryOverride.php
Compute the fallback chain for a language code.
language_hierarchy_calculate_depth in ./language_hierarchy.module
Get the depth of a language inside hierarchy.
language_hierarchy_form_language_admin_overview_form_alter in ./language_hierarchy.module
Implements hook_form_FORM_ID_alter() for language_admin_overview_form().

File

./language_hierarchy.module, line 255
Add sublanguage handling functionality to Drupal.

Code

function language_hierarchy_get_ancestors(ConfigurableLanguageInterface $language) {
  $ancestors = [];

  // Record which languages have been iterated over, so loops can be avoided.
  $iterated = [];
  while (($ancestor_langcode = $language
    ->getThirdPartySetting('language_hierarchy', 'fallback_langcode')) && !in_array($ancestor_langcode, $iterated, TRUE)) {
    $iterated[] = $ancestor_langcode;
    if ($ancestor = ConfigurableLanguage::load($ancestor_langcode)) {
      $ancestors[$ancestor
        ->getId()] = $ancestor;
      $language = $ancestor;
    }
  }
  return $ancestors;
}