Builds a renderable array for a field value.
Drupal\Core\Entity\EntityInterface $entity: The entity being displayed.
string $langcode: The language associated with $items.
array $items: Array of values for this field.
array A renderable array for $items, as an array of child elements keyed by numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array();
$allowed_values = options_allowed_values($this->field, $this->instance, $entity);
foreach ($items as $delta => $item) {
if (isset($allowed_values[$item['value']])) {
$output = field_filter_xss($allowed_values[$item['value']]);
}
else {
// If no match was found in allowed values, fall back to the key.
$output = field_filter_xss($item['value']);
}
$elements[$delta] = array(
'#markup' => $output,
);
}
return $elements;
}