You are here

function update_language_list in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/includes/update.inc \update_language_list()

Returns a list of languages set up on the site during upgrades.

Parameters

$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, ordered by weight ascending and name ascending.

File

core/includes/update.inc, line 703
Drupal database update API.

Code

function update_language_list($flags = LanguageInterface::STATE_CONFIGURABLE) {
  $languages =& drupal_static(__FUNCTION__);

  // Initialize master language list.
  if (!isset($languages)) {

    // Initialize local language list cache.
    $languages = array();

    // Fill in master language list based on current configuration.
    $default = \Drupal::languageManager()
      ->getDefaultLanguage();
    if (\Drupal::languageManager()
      ->isMultilingual() || \Drupal::moduleHandler()
      ->moduleExists('language')) {

      // Use language module configuration if available. We can not use
      // entity_load_multiple() because this breaks during updates.
      $language_entities = \Drupal::configFactory()
        ->listAll('language.entity.');

      // Initialize default property so callers have an easy reference and can
      // save the same object without data loss.
      foreach ($language_entities as $langcode_config_name) {
        $langcode = substr($langcode_config_name, strlen('language.entity.'));
        $info = \Drupal::config($langcode_config_name)
          ->get();
        $languages[$langcode] = new Language(array(
          'default' => $info['id'] == $default
            ->getId(),
          'name' => $info['label'],
          'id' => $info['id'],
          'direction' => $info['direction'],
          'locked' => $info['locked'],
          'weight' => $info['weight'],
        ));
      }
      Language::sort($languages);
    }
    else {

      // No language module, so use the default language only.
      $languages = array(
        $default
          ->getId() => $default,
      );

      // Add the special languages, they will be filtered later if needed.
      $languages += \Drupal::languageManager()
        ->getDefaultLockedLanguages($default
        ->getWeight());
    }
  }

  // Filter the full list of languages based on the value of the $all flag. By
  // default we remove the locked languages, but the caller may request for
  // those languages to be added as well.
  $filtered_languages = array();

  // Add the site's default language if flagged as allowed value.
  if ($flags & LanguageInterface::STATE_SITE_DEFAULT) {
    $default = \Drupal::languageManager()
      ->getDefaultLanguage();

    // Rename the default language.
    $default
      ->setName(t("Site's default language (@lang_name)", array(
      '@lang_name' => $default
        ->getName(),
    )));
    $filtered_languages[LanguageInterface::LANGCODE_SITE_DEFAULT] = $default;
  }
  foreach ($languages as $langcode => $language) {
    if ($language
      ->isLocked() && !($flags & LanguageInterface::STATE_LOCKED) || !$language
      ->isLocked() && !($flags & LanguageInterface::STATE_CONFIGURABLE)) {
      continue;
    }
    $filtered_languages[$langcode] = $language;
  }
  return $filtered_languages;
}