ConfigurableLanguageManager::getLanguages

public ConfigurableLanguageManager::getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE)

Returns a list of languages set up on the site.

Parameters

int $flags: (optional) Specifies the state of the languages that have to be returned. It can be: LanguageInterface::STATE_CONFIGURABLE, LanguageInterface::STATE_LOCKED, or LanguageInterface::STATE_ALL.

Return value

\Drupal\Core\Language\LanguageInterface[] An associative array of languages, keyed by the language code.

Overrides LanguageManager::getLanguages

File

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

Class

ConfigurableLanguageManager
Overrides default LanguageManager to provide configured languages.

Namespace

Drupal\language

Code

public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE) {
  // If a config override is set, cache using that language's ID.
  if ($override_language = $this->getConfigOverrideLanguage()) {
    $static_cache_id = $override_language->getId();
  }
  else {
    $static_cache_id = $this->getCurrentLanguage()->getId();
  }

  if (!isset($this->languages[$static_cache_id][$flags])) {
    // Initialize the language list with the default language and default
    // locked languages. These cannot be removed. This serves as a fallback
    // list if this method is invoked while the language module is installed
    // and the configuration entities for languages are not yet fully
    // imported.
    $default = $this->getDefaultLanguage();
    $languages = array($default->getId() => $default);
    $languages += $this->getDefaultLockedLanguages($default->getWeight());

    // Load configurable languages on top of the defaults. Ideally this could
    // use the entity API to load and instantiate ConfigurableLanguage
    // objects. However the entity API depends on the language system, so that
    // would result in infinite loops. We use the configuration system
    // directly and instantiate runtime Language objects. When language
    // entities are imported those cover the default and locked languages, so
    // site-specific configuration will prevail over the fallback values.
    // Having them in the array already ensures if this is invoked in the
    // middle of importing language configuration entities, the defaults are
    // always present.
    $config_ids = $this->configFactory->listAll('language.entity.');
    foreach ($this->configFactory->loadMultiple($config_ids) as $config) {
      $data = $config->get();
      $data['name'] = $data['label'];
      $languages[$data['id']] = new Language($data);
    }
    Language::sort($languages);

    // Filter the full list of languages based on the value of $flags.
    $this->languages[$static_cache_id][$flags] = $this->filterLanguages($languages, $flags);
  }

  return $this->languages[$static_cache_id][$flags];
}
doc_Drupal
2016-10-29 08:55:15
Comments
Leave a Comment

Please login to continue.