function LanguageFormatter::viewValue

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/LanguageFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\LanguageFormatter::viewValue()
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/LanguageFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\LanguageFormatter::viewValue()
  3. 11.x 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 114

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);
    return [
        '#plain_text' => $item->language && isset($languages[$item->language
            ->getId()]) ? $languages[$item->language
            ->getId()]
            ->getName() : '',
    ];
}

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