protected function LanguageFormatter::viewValue in Custom Language field 8
Generate the output appropriate for one field item.
Parameters
\Drupal\Core\Field\FieldItemInterface $item: One field item.
Return value
array The textual output generated as a render array.
Overrides StringFormatter::viewValue
File
- src/
Plugin/ Field/ FieldFormatter/ LanguageFormatter.php, line 126
Class
- LanguageFormatter
- Plugin implementation of the 'language_field' formatter.
Namespace
Drupal\languagefield\Plugin\Field\FieldFormatterCode
protected function viewValue(FieldItemInterface $item) {
$settings = $this
->getSettings();
$langcode = $item->value;
// Do NOT use \Drupal::languageManager,
// since it only uses installed languages.
// Do call LanguageItem::getLanguage to have
// the benefit of added custom languages.
$language = CustomLanguageManager::createFromLangcode($langcode);
$language_translated_name = $language
->getName();
// Create the markup for this value.
$markup = [];
if (!empty($settings['format']['iso'])) {
$markup[] = $langcode;
}
if (!empty($settings['format']['name'])) {
// @todo Use language of user of of content entity?
$markup[] = $language_translated_name;
}
if (!empty($settings['format']['name_native'])) {
// @todo Create feature request to add function to D8 core.
$native_name = $item
->getNativeName();
$markup[] = empty($settings['format']['name']) ? $native_name : '(' . $native_name . ')';
}
$markup = empty($markup) ? $language_translated_name : implode(' ', $markup);
$result = [
'#type' => 'processed_text',
'#context' => [
'value' => $item->value,
],
'#format' => $item->format,
];
// Add variables for languageicons theme function.
if (!empty($settings['format']['icon']) && $this->moduleHandler
->moduleExists('languageicons')) {
$result += [
'language' => $language,
'title' => $markup,
];
languageicons_link_add($result, $language_translated_name);
unset($result['language']);
unset($result['html']);
}
else {
// The text value has no text format assigned to it, so the user input
// should equal the output, including newlines.
$result += [
'#text' => $markup,
];
}
return $result;
}