function LanguageFormatter::viewValue

Same name in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/LanguageFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\LanguageFormatter::viewValue()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/LanguageFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\LanguageFormatter::viewValue()
  3. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/LanguageFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\LanguageFormatter::viewValue()

Overrides StringFormatter::viewValue

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/LanguageFormatter.php, line 115

Class

LanguageFormatter
Plugin implementation of the 'language' formatter.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

protected function viewValue(FieldItemInterface $item) {
    // The 'languages' cache context is not necessary because the language is
    // either displayed in its configured form (loaded directly from config
    // storage by LanguageManager::getLanguages()) or in its native language
    // name. That only depends on formatter settings and no language condition.
    $languages = $this->getSetting('native_language') ? $this->languageManager
        ->getNativeLanguages(LanguageInterface::STATE_ALL) : $this->languageManager
        ->getLanguages(LanguageInterface::STATE_ALL);
    // \Drupal\Core\Language\LanguageInterface::LANGCODE_NOT_SPECIFIED
    // and \Drupal\Core\Language\LanguageInterface::LANGCODE_NOT_APPLICABLE are
    // not returned from the language manager above.
    $value = [];
    if (isset($item->language)) {
        $name = isset($languages[$item->language
            ->getId()]) ? $languages[$item->language
            ->getId()]
            ->getName() : $item->language
            ->getId();
        $value = [
            '#plain_text' => $name,
        ];
    }
    return $value;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.