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) {
$element = array();
$settings = $this
->getSettings();
foreach ($items as $delta => $item) {
// By default use the full URL as the link text.
$link_title = $item['url'];
// If the title field value is available, use it for the link text.
if (empty($settings['url_only']) && !empty($item['title'])) {
// Unsanitizied token replacement here because $options['html'] is FALSE
// by default in theme_link().
$link_title = \Drupal::token()
->replace($item['title'], array(
$entity
->entityType() => $entity,
), array(
'sanitize' => FALSE,
'clear' => TRUE,
));
}
// Trim the link text to the desired length.
if (!empty($settings['trim_length'])) {
$link_title = truncate_utf8($link_title, $settings['trim_length'], FALSE, TRUE);
}
if (!empty($settings['url_only']) && !empty($settings['url_plain'])) {
$element[$delta] = array(
'#type' => 'markup',
'#markup' => check_plain($link_title),
);
}
else {
$element[$delta] = array(
'#type' => 'link',
'#title' => $link_title,
'#href' => $item['path'],
'#options' => $item['options'],
);
}
}
return $element;
}