You are here

public function LanguageItem::getPossibleOptions in Custom Language field 8

Parameters

\Drupal\Core\Session\AccountInterface $account: (optional) The user account for which to filter the possible options. If omitted, all possible options are returned.

string $format: Extra parameter for formatting options.

Overrides LanguageItem::getPossibleOptions

2 calls to LanguageItem::getPossibleOptions()
LanguageItem::getPossibleValues in src/Plugin/Field/FieldType/LanguageItem.php
Returns an array of possible values.
LanguageItem::storageSettingsForm in src/Plugin/Field/FieldType/LanguageItem.php
Returns a form for the storage-level settings.

File

src/Plugin/Field/FieldType/LanguageItem.php, line 281

Class

LanguageItem
Plugin implementation of the 'language' field type.

Namespace

Drupal\languagefield\Plugin\Field\FieldType

Code

public function getPossibleOptions(AccountInterface $account = NULL, $format = 'en') {

  // Caching as per https://www.drupal.org/node/2661204
  static $possible_options = [];
  $field_name = $this
    ->getFieldDefinition()
    ->getName();
  if (isset($possible_options[$field_name])) {
    return $possible_options[$field_name];
  }

  // No need to cache this data. It is a hardcoded list.
  $languages = \Drupal::languageManager()
    ->getStandardLanguageList();

  // Add the custom languages to the list.
  $languages += CustomLanguageManager::getCustomLanguageList();

  // Format the array to Options format.
  foreach ($languages as $langcode => $language_names) {
    $language_name = '';
    switch ($format) {
      case 'en':
        $language_name .= $this
          ->t($language_names[0]);
        break;
      case 'loc':
        $language_name .= $language_names[1];
        break;
      case 'both':
        $language_name .= $this
          ->t($language_names[0]);
        if (mb_strlen($language_names[1])) {
          $language_name .= ' (' . $language_names[1] . ')';
        }
        $language_name .= ' [' . $langcode . ']';
        break;
    }
    $possible_options[$field_name][$langcode] = $language_name;
  }
  asort($possible_options[$field_name]);
  return $possible_options[$field_name];
}