public function FileDownloadUriFieldFormatter::viewElements in File Download 8
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/ FileDownloadUriFieldFormatter.php, line 65
Class
- FileDownloadUriFieldFormatter
- Plugin implementation of the "file_download_uri_formatter" formatter.
Namespace
Drupal\file_download\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$settings = $this
->getSettings();
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $file) {
$uri = $file
->getFileUri();
$parts = explode('://', $uri);
$url = Url::fromRoute('file_download.link', [
'scheme' => $parts[0],
'fid' => $file
->id(),
]);
$url
->setAbsolute($settings['absolute_url']);
$elements[$delta] = [
'#markup' => $url
->toString(),
];
}
return $elements;
}