public function viewElements(FieldItemListInterface
$items,
$langcode) { $elements =
[];
// Only collect allowed options if there are actually items to display.
if ($items->
count()) { $provider =
$items->
getFieldDefinition() ->
getFieldStorageDefinition() ->
getOptionsProvider('value',
$items->
getEntity());
// Flatten the possible options, to support opt groups.
$options = OptGroup::
flattenOptions($provider->
getPossibleOptions());
foreach ($items as $delta =>
$item) { $value =
$item->value;
// If the stored value is in the current set of allowed values, display
// the associated label, otherwise just display the raw value.
$output =
$options[$value] ??
$value;
$elements[$delta] =
[ '#markup' =>
$output,
'#allowed_tags' => FieldFilteredMarkup::
allowedTags(),
];
} }