function StringFormatter::viewElements
Same name in other branches
- 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/StringFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter::viewElements()
- 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/StringFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter::viewElements()
- 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/StringFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\StringFormatter::viewElements()
Overrides FormatterInterface::viewElements
File
-
core/
lib/ Drupal/ Core/ Field/ Plugin/ Field/ FieldFormatter/ StringFormatter.php, line 124
Class
- StringFormatter
- Plugin implementation of the 'string' formatter.
Namespace
Drupal\Core\Field\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$entity = $items->getEntity();
$entity_type = $entity->getEntityType();
$render_as_link = FALSE;
if ($this->getSetting('link_to_entity') && !$entity->isNew() && $entity_type->hasLinkTemplate('canonical')) {
$url = $this->getEntityUrl($entity);
$access = $url->access(return_as_object: TRUE);
(new CacheableMetadata())->addCacheableDependency($access)
->applyTo($elements);
$render_as_link = $access->isAllowed();
}
foreach ($items as $delta => $item) {
if ($render_as_link) {
assert(isset($url));
$elements[$delta] = [
'#type' => 'link',
'#title' => $this->viewValue($item),
'#url' => $url,
];
}
else {
$elements[$delta] = $this->viewValue($item);
}
}
return $elements;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.