public function FileUrlFormatter::viewElements in File URL 8
Same name and namespace in other branches
- 2.0.x src/Plugin/Field/FieldFormatter/FileUrlFormatter.php \Drupal\file_url\Plugin\Field\FieldFormatter\FileUrlFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ FileUrlFormatter.php, line 72
Class
- FileUrlFormatter
- Plugin implementation of the 'file_default' formatter.
Namespace
Drupal\file_url\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $file) {
$item = $file->_referringItem;
if ($this
->getSetting('mode') === 'plain') {
$elements['delta'] = [
$elements[$delta] = [
'#markup' => file_url_transform_relative(file_create_url($file
->getFileUri())),
'#cache' => [
'tags' => $file
->getCacheTags(),
],
],
];
}
else {
$elements[$delta] = [
'#theme' => 'file_link',
'#file' => $file,
'#description' => $item->description,
'#cache' => [
'tags' => $file
->getCacheTags(),
],
];
}
// Pass field item attributes to the theme function.
if (isset($item->_attributes)) {
$elements[$delta] += [
'#attributes' => [],
];
$elements[$delta]['#attributes'] += $item->_attributes;
// Unset field item attributes since they have been included in the
// formatter output and should not be rendered in the field template.
unset($item->_attributes);
}
// Allow showing the full URI as tip.
// @todo Probably the UX/UI team should decide if the full URL should be
// permanently displayed when showing distributions.
$elements[$delta]['#attributes']['title'] = file_create_url($file
->getFileUri());
}
return $elements;
}