You are here

public static function CustomLanguageManager::allowedValues in Custom Language field 8

Gets a list of allowed values.

Parameters

array $settings: Settings.

Return value

array|\Drupal\Core\Language\LanguageInterface[] Array or language interface.

3 calls to CustomLanguageManager::allowedValues()
LanguageAutocompleteController::autocomplete in src/Controller/LanguageAutocompleteController.php
Returns response for the language autocomplete widget.
languagefield_allowed_values in ./languagefield.module
Returns the array of allowed values for a Custom language field.
LanguageItem::getSettableOptions in src/Plugin/Field/FieldType/LanguageItem.php
Returns an array of settable values with labels for display.

File

src/Entity/CustomLanguageManager.php, line 137

Class

CustomLanguageManager
Defines the CustomLanguage entity.

Namespace

Drupal\languagefield\Entity

Code

public static function allowedValues(array $settings) {
  $languages = [];
  $subsets = $settings['language_range'];
  foreach ($subsets as $subset => $active) {
    $subsettable_languages = [];
    if (!$active) {
      continue;
    }
    switch ($subset) {
      case LanguageInterface::STATE_CONFIGURABLE:
      case LanguageInterface::STATE_LOCKED:
      case LanguageInterface::STATE_ALL:
        $subsettable_languages = \Drupal::languageManager()
          ->getLanguages($subset);

        // Convert to $langcode => $name array.
        foreach ($subsettable_languages as $langcode => $language) {
          $subsettable_languages[$langcode] = $language
            ->isLocked() ? t('- @name -', [
            '@name' => $language
              ->getName(),
          ]) : $language
            ->getName();
        }
        break;

      /*
       * The following values are copied from
       * LanguageConfiguration::getDefaultOptions()
       * The following evaluations are copied from
       * function language_get_default_langcode()
       * LanguageInterface::LANGCODE_SITE_DEFAULT =>
       * t("Site's default language", array('@language' =>
       * \Drupal::languageManager()->getDefaultLanguage()->name)),
       * 'current_interface' => t('Current interface language'),
       * 'authors_default' => t("Author's preferred language"),
       */
      case LanguageInterface::LANGCODE_NOT_SPECIFIED:
        $subsettable_languages = [
          LanguageInterface::LANGCODE_NOT_SPECIFIED => 'Language neutral',
        ];
        break;
      case CustomLanguageManager::LANGUAGEFIELD_LANGUAGES_CUSTOM:
      case LanguageInterface::LANGCODE_SITE_DEFAULT:
      case 'current_interface':
      case 'authors_default':
        $subsettable_languages = self::getLanguageConfigurationOptions($subset);
        break;
      case CustomLanguageManager::LANGUAGEFIELD_LANGUAGES_PREDEFINED:

        // 'All predefined languages'.
        $standard_languages = \Drupal::languageManager()
          ->getStandardLanguageList();
        foreach ($standard_languages as $langcode => $language_names) {
          $subsettable_languages[$langcode] = $language_names[0];
        }
        asort($subsettable_languages);
        break;
    }
    $languages += $subsettable_languages;
  }
  $included_languages = array_filter($settings['included_languages']);
  if (!empty($included_languages)) {
    $languages = array_intersect_key($languages, $included_languages);
  }
  if (!empty($settings['excluded_languages'])) {
    $languages = array_diff_key($languages, $settings['excluded_languages']);
  }
  if (!empty($settings['groups'])) {
    $grouped_languages = [];
    $found_languages = [];
    $languages += [
      'other' => t('Other languages'),
    ];
    foreach (explode("\n", $settings['groups']) as $line) {
      if (strpos($line, '|') !== FALSE) {
        list($group, $langs) = explode('|', $line, 2);
        $langs = array_filter(array_map('trim', explode(',', $langs)));
        $langs = array_intersect_key($languages, array_combine($langs, $langs));
        $found_languages += $langs;
        $grouped_languages[$group] = $langs;
      }
      else {
        $langs = array_filter(array_map('trim', explode(',', $line)));
        if (!empty($langs)) {
          $langs = array_intersect_key($languages, array_combine($langs, $langs));
          $found_languages += (array) $langs;
          $grouped_languages += (array) $langs;
        }
      }
    }
    $missing_languages = array_diff_key($languages, $found_languages);
    foreach ($grouped_languages as $index => $options) {
      if (is_array($options)) {
        if (isset($options['other'])) {
          unset($options['other']);
          if ($missing_languages) {
            $grouped_languages[$index] = array_merge($grouped_languages[$index], $missing_languages);
            $missing_languages = FALSE;
          }
        }
      }
    }
    if (isset($grouped_languages['other'])) {
      unset($grouped_languages['other']);
      if ($missing_languages) {
        $grouped_languages = array_merge($grouped_languages, $missing_languages);
      }
    }
    return $grouped_languages;
  }
  return $languages;
}