View source
<?php
namespace Drupal\file_entity\Plugin\Field\FieldFormatter;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Utility\Token;
use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FileDownloadLinkFormatter extends FileFormatterBase implements ContainerFactoryPluginInterface {
protected $renderer;
protected $module_handler;
protected $token;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, RendererInterface $renderer, ModuleHandlerInterface $module_handler, Token $token) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->renderer = $renderer;
$this->module_handler = $module_handler;
$this->token = $token;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('renderer'), $container
->get('module_handler'), $container
->get('token'));
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['access_message'] = [
'#type' => 'textfield',
'#title' => t('No access message'),
'#description' => t("This text is shown instead of the download link if the user doesn't have permission to download the file."),
'#default_value' => $this
->getSetting('access_message'),
];
$element['text'] = array(
'#type' => 'textfield',
'#title' => t('Link text'),
'#description' => t('This field supports tokens.'),
'#default_value' => $this
->getSetting('text'),
);
if ($this->module_handler
->moduleExists('token')) {
$token_types = array(
'file',
);
if (!empty($form['#entity_type'])) {
$token_types[] = $form['#entity_type'];
}
$element['token_tree_link'] = array(
'#theme' => 'token_tree_link',
'#token_types' => $token_types,
);
}
return $element;
}
public static function defaultSettings() {
return [
'access_message' => "You don't have access to download this file.",
'text' => 'Download [file:name]',
] + parent::defaultSettings();
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$parent_entity = $items
->getParent()
->getValue();
if (!empty($parent_entity)) {
$parent_entity_type = $parent_entity
->getEntityType()
->id();
$token_data[$parent_entity_type] = $parent_entity;
}
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $file) {
$attributes = new Attribute();
$mime_type = $file
->getMimeType();
$token_data['file'] = $file;
$link_text = $this->token
->replace($this
->getSetting('text'), $token_data);
$download_url = $file
->downloadUrl(array(
'attributes' => array(
'type' => $mime_type . '; length=' . $file
->getSize(),
),
));
if ($file
->access('download')) {
$elements[$delta] = [
'#theme' => 'file_entity_download_link',
'#file' => $file,
'#download_link' => Link::fromTextAndUrl($link_text, $download_url),
'#icon' => file_icon_class($mime_type),
'#attributes' => $attributes,
'#file_size' => format_size($file
->getSize()),
];
}
else {
$elements[$delta] = [
'#markup' => $this
->getSetting('access_message'),
];
}
$this->renderer
->addCacheableDependency($elements[$delta], $file);
}
return $elements;
}
}