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();
// Terms without tid do not exist yet, theme such terms as just their name.
foreach ($items as $delta => $item) {
if (!$item['tid']) {
$elements[$delta] = array(
'#markup' => check_plain($item['entity']
->label()),
);
}
else {
$term = $item['entity'];
$uri = $term
->uri();
$elements[$delta] = array(
'#type' => 'link',
'#title' => $term
->label(),
'#href' => $uri['path'],
'#options' => $uri['options'],
);
}
}
return $elements;
}