function BaseFieldFileFormatterBase::viewElements
Same name in other branches
- 9 core/modules/file/src/Plugin/Field/FieldFormatter/BaseFieldFileFormatterBase.php \Drupal\file\Plugin\Field\FieldFormatter\BaseFieldFileFormatterBase::viewElements()
- 10 core/modules/file/src/Plugin/Field/FieldFormatter/BaseFieldFileFormatterBase.php \Drupal\file\Plugin\Field\FieldFormatter\BaseFieldFileFormatterBase::viewElements()
- 11.x core/modules/file/src/Plugin/Field/FieldFormatter/BaseFieldFileFormatterBase.php \Drupal\file\Plugin\Field\FieldFormatter\BaseFieldFileFormatterBase::viewElements()
Overrides FormatterInterface::viewElements
File
-
core/
modules/ file/ src/ Plugin/ Field/ FieldFormatter/ BaseFieldFileFormatterBase.php, line 44
Class
- BaseFieldFileFormatterBase
- Base class for file formatters, which allow to link to the file download URL.
Namespace
Drupal\file\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$url = NULL;
// Add support to link to the entity itself.
if ($this->getSetting('link_to_file')) {
// @todo Wrap in file_url_transform_relative(). This is currently
// impossible. See below.
$url = file_create_url($items->getEntity()->uri->value);
}
foreach ($items as $delta => $item) {
$view_value = $this->viewValue($item);
if ($url) {
$elements[$delta] = [
'#type' => 'link',
'#title' => $view_value,
'#url' => Url::fromUri($url),
// @todo Remove the 'url.site' cache context by using a relative file
// URL (file_url_transform_relative()). This is currently impossible
// because #type => link requires a Url object, and Url objects do not
// support relative URLs: they require fully qualified URLs. Fix in
// https://www.drupal.org/node/2646744.
'#cache' => [
'contexts' => [
'url.site',
],
],
];
}
else {
$elements[$delta] = is_array($view_value) ? $view_value : [
'#markup' => $view_value,
];
}
}
return $elements;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.