DirectDownloadFormatter.php in DownloadFile 8
File
src/Plugin/Field/FieldFormatter/DirectDownloadFormatter.php
View source
<?php
namespace Drupal\download_file\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;
class DirectDownloadFormatter extends FileFormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $file) {
$item = $file->_referringItem;
$id = $file
->id();
$link_text = !empty($item->description) ? $item->description : $file
->getFilename();
$elements[$delta] = [
'#theme' => 'direct_download_file_link',
'#link_text' => $link_text,
'#file_id' => $id,
'#cache' => [
'tags' => $file
->getCacheTags(),
],
];
if (isset($item->_attributes)) {
$elements[$delta] += [
'#attributes' => [],
];
$elements[$delta]['#attributes'] += $item->_attributes;
unset($item->_attributes);
}
}
return $elements;
}
}