You are here

protected function PluginBase::listLanguages in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/PluginBase.php \Drupal\views\Plugin\views\PluginBase::listLanguages()
  2. 9 core/modules/views/src/Plugin/views/PluginBase.php \Drupal\views\Plugin\views\PluginBase::listLanguages()

Makes an array of languages, optionally including special languages.

Parameters

int $flags: (optional) Flags for which languages to return (additive). Options:

  • \Drupal\Core\Language::STATE_ALL (default): All languages (configurable and default).
  • \Drupal\Core\Language::STATE_CONFIGURABLE: Configurable languages.
  • \Drupal\Core\Language::STATE_LOCKED: Locked languages.
  • \Drupal\Core\Language::STATE_SITE_DEFAULT: Add site default language; note that this is not included in STATE_ALL.
  • \Drupal\views\Plugin\views\PluginBase::INCLUDE_NEGOTIATED: Add negotiated language types.
  • \Drupal\views\Plugin\views\PluginBase::INCLUDE_ENTITY: Add entity row language types. Note that these are only supported for display options, not substituted in queries.

array|null $current_values: The currently-selected options in the list, if available.

Return value

array An array of language names, keyed by the language code. Negotiated and special languages have special codes that are substituted in queries by PluginBase::queryLanguageSubstitutions(). Only configurable languages and languages that are in $current_values are included in the list.

File

core/modules/views/src/Plugin/views/PluginBase.php, line 557

Class

PluginBase

Namespace

Drupal\views\Plugin\views

Code

protected function listLanguages($flags = LanguageInterface::STATE_ALL, array $current_values = NULL) {
  $manager = \Drupal::languageManager();
  $languages = $manager
    ->getLanguages($flags);
  $list = [];

  // The entity languages should come first, if requested.
  if ($flags & PluginBase::INCLUDE_ENTITY) {
    $list['***LANGUAGE_entity_translation***'] = $this
      ->t('Content language of view row');
    $list['***LANGUAGE_entity_default***'] = $this
      ->t('Original language of content in view row');
  }

  // STATE_SITE_DEFAULT comes in with ID set
  // to LanguageInterface::LANGCODE_SITE_DEFAULT.
  // Since this is not a real language, surround it by '***LANGUAGE_...***',
  // like the negotiated languages below.
  if ($flags & LanguageInterface::STATE_SITE_DEFAULT) {
    $name = $languages[LanguageInterface::LANGCODE_SITE_DEFAULT]
      ->getName();

    // The language name may have already been translated, no need to
    // translate it again.
    // @see Drupal\Core\Language::filterLanguages().
    if (!$name instanceof TranslatableMarkup) {
      $name = $this
        ->t($name);
    }
    $list[PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT] = $name;

    // Remove site default language from $languages so it's not added
    // twice with the real languages below.
    unset($languages[LanguageInterface::LANGCODE_SITE_DEFAULT]);
  }

  // Add in negotiated languages, if requested.
  if ($flags & PluginBase::INCLUDE_NEGOTIATED) {
    $types_info = $manager
      ->getDefinedLanguageTypesInfo();
    $types = $manager
      ->getLanguageTypes();

    // We only go through the configured types.
    foreach ($types as $id) {
      if (isset($types_info[$id]['name'])) {
        $name = $types_info[$id]['name'];

        // Surround IDs by '***LANGUAGE_...***', to avoid query collisions.
        $id = '***LANGUAGE_' . $id . '***';
        $list[$id] = $this
          ->t('@type language selected for page', [
          '@type' => $name,
        ]);
      }
    }
    if (!empty($current_values)) {
      foreach ($types_info as $id => $type) {
        $id = '***LANGUAGE_' . $id . '***';

        // If this (non-configurable) type is among the current values,
        // add that option too, so it is not lost. If not among the current
        // values, skip displaying it to avoid user confusion.
        if (isset($type['name']) && !isset($list[$id]) && in_array($id, $current_values, TRUE)) {
          $list[$id] = $this
            ->t('@type language selected for page', [
            '@type' => $type['name'],
          ]);
        }
      }
    }
  }

  // Add real languages.
  foreach ($languages as $id => $language) {
    $list[$id] = $language
      ->getName();
  }
  return $list;
}