View source
<?php
namespace Drupal\file_download_link\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Utility\Token;
use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Extension\ModuleHandler;
class FileDownloadLink extends FileFormatterBase implements ContainerFactoryPluginInterface {
protected $token;
protected $moduleHandler;
protected $tokenEntityMapper;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, Token $token, ModuleHandler $module_handler, $token_entity_mapper) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->token = $token;
$this->moduleHandler = $module_handler;
$this->tokenEntityMapper = $token_entity_mapper;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$module_handler = $container
->get('module_handler');
if ($module_handler
->moduleExists('token')) {
$token_entity_mapper = $container
->get('token.entity_mapper');
}
else {
$token_entity_mapper = NULL;
}
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('token'), $module_handler, $token_entity_mapper);
}
public static function defaultSettings() {
$options = parent::defaultSettings();
$options['link_text'] = 'Download';
$options['link_title'] = NULL;
$options['new_tab'] = TRUE;
$options['force_download'] = TRUE;
$options['custom_classes'] = '';
return $options;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
if ($this->fieldDefinition
->getTargetEntityTypeId() == 'media') {
if (!$this->moduleHandler
->moduleExists('file_download_link_media')) {
$form['media_warning'] = [
'#type' => 'container',
'#markup' => $this
->t("Did you know the file_download_link_media module allows you render a Media reference field as a link to the Media's source file or image? Consider enabling the module if that sounds helpful."),
'#attributes' => [
'class' => [
'messages messages--warning',
],
],
];
}
}
$form['link_text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Link Text'),
'#default_value' => $this
->getSetting('link_text'),
'#description' => $this
->t('This text is linked to the file. If left empty, the filename will be used.'),
];
if ($this->moduleHandler
->moduleExists('token')) {
$form['tokens'] = [
'#theme' => 'token_tree_link',
'#token_types' => [
$this->tokenEntityMapper
->getTokenTypeForEntityType('file'),
$this->tokenEntityMapper
->getTokenTypeForEntityType($this->fieldDefinition
->getTargetEntityTypeId()),
],
];
$form['token_example'] = [
'#type' => 'details',
'#title' => $this
->t('Example Token'),
'0' => [
'#markup' => $this
->getTokenExampleMarkup(),
],
];
}
else {
$form['token_warning'] = [
'#type' => 'container',
'#markup' => $this
->getTokenWarningMarkup(),
'#attributes' => [
'class' => [
'messages messages--warning',
],
],
];
}
$form['new_tab'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Open file in new tab'),
'#default_value' => $this
->getSetting('new_tab'),
];
$form['force_download'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Force Download'),
'#default_value' => $this
->getSetting('force_download'),
'#description' => $this
->t('This adds the <i>download</i> attribute to the link, which works in many modern browsers.'),
];
$form['link_title'] = [
'#type' => 'textfield',
'#title' => $this
->t('Link Title'),
'#default_value' => $this
->getSetting('link_title'),
'#description' => $this
->t('Many browsers show the title attribute in a tooltip.'),
];
if ($this->moduleHandler
->moduleExists('token')) {
$form['tokens_2'] = [
'#theme' => 'token_tree_link',
'#token_types' => [
$this->tokenEntityMapper
->getTokenTypeForEntityType('file'),
$this->tokenEntityMapper
->getTokenTypeForEntityType($this->fieldDefinition
->getTargetEntityTypeId()),
],
];
}
$form['custom_classes'] = [
'#type' => 'textfield',
'#title' => $this
->t('Custom CSS Classes'),
'#default_value' => $this
->getSetting('custom_classes'),
'#description' => $this
->t('Enter space-separated CSS classes to be added to the link.'),
];
if ($this->moduleHandler
->moduleExists('token')) {
$form['tokens_3'] = [
'#theme' => 'token_tree_link',
'#token_types' => [
$this->tokenEntityMapper
->getTokenTypeForEntityType('file'),
$this->tokenEntityMapper
->getTokenTypeForEntityType($this->fieldDefinition
->getTargetEntityTypeId()),
],
];
}
return $form;
}
public function settingsSummary() {
$summary = [];
$summary[] = $this
->t('Link Text: @link_text', [
'@link_text' => $this
->getSetting('link_text'),
]);
if ($this
->getSetting('new_tab')) {
$summary[] = $this
->t('Open in new tab');
}
if ($this
->getSetting('force_download')) {
$summary[] = $this
->t('Force download');
}
if ($this
->getSetting('link_title')) {
$summary[] = $this
->t('Link Title: @link_title', [
'@link_title' => $this
->getSetting('link_title'),
]);
}
if ($this
->getSetting('custom_classes')) {
$summary[] = $this
->t('Classes: @classes', [
'@classes' => $this
->getSetting('custom_classes'),
]);
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $file) {
$mime_type_explosion = explode("/", $file
->getMimeType());
$file_type = reset($mime_type_explosion);
$file_extension = end($mime_type_explosion);
$options = [
'attributes' => [
'class' => [
'file-download',
'file-download-' . $file_type,
'file-download-' . $file_extension,
],
],
];
if ($this
->getSetting('new_tab')) {
$options['attributes']['target'] = '_blank';
}
if ($this
->getSetting('force_download')) {
$options['attributes']['download'] = TRUE;
}
if ($this
->getSetting('link_title')) {
$options['attributes']['title'] = $this
->getSetting('link_title');
}
$elements[$delta] = [
'#type' => 'link',
'#title' => $this
->getSetting('link_text'),
'#url' => Url::fromUri(file_create_url($file
->getFileUri())),
'#options' => $options,
'#cache' => [
'tags' => $file
->getCacheTags(),
],
];
if ($this->moduleHandler
->moduleExists('token')) {
$data = [];
$data[$this->tokenEntityMapper
->getTokenTypeForEntityType($file
->getEntityTypeId())] = $file;
$entity = $items
->getEntity();
$field = $this->fieldDefinition
->getName();
$entity_token_type = $this->tokenEntityMapper
->getTokenTypeForEntityType($entity
->getEntityTypeId());
$data[$entity_token_type] = $entity;
$bubbleable_metadata = new BubbleableMetadata();
if ($this
->getSetting('link_text')) {
$text = $this
->getSetting('link_text');
$text = $this
->addDeltaToTokens($text, $delta, $entity_token_type, $field);
$text = $this->token
->replace($text, $data, [
'langcode' => $langcode,
'clear' => TRUE,
], $bubbleable_metadata);
$text = Html::decodeEntities($text);
$elements[$delta]['#title'] = $text;
}
if ($this
->getSetting('link_title')) {
$title = $this
->getSetting('link_title');
$title = $this
->addDeltaToTokens($title, $delta, $entity_token_type, $field);
$title = $this->token
->replace($title, $data, [
'langcode' => $langcode,
'clear' => TRUE,
], $bubbleable_metadata);
$title = Html::decodeEntities($title);
if ($title) {
$elements[$delta]['#options']['attributes']['title'] = $title;
}
else {
unset($elements[$delta]['#options']['attributes']['title']);
}
}
if ($this
->getSetting('custom_classes')) {
$custom_classes = $this
->getSetting('custom_classes');
$custom_classes = $this
->addDeltaToTokens($custom_classes, $delta, $entity_token_type, $field);
$custom_classes = $this->token
->replace($custom_classes, $data, [
'langcode' => $langcode,
'clear' => TRUE,
], $bubbleable_metadata);
$custom_classes = Html::decodeEntities($custom_classes);
}
$bubbleable_metadata
->applyTo($elements[$delta]);
}
if (empty($elements[$delta]['#title'])) {
$elements[$delta]['#title'] = $file
->getFilename();
}
if ($this
->getSetting('custom_classes')) {
if (!isset($custom_classes)) {
$custom_classes = $this
->getSetting('custom_classes');
}
if (!empty($custom_classes)) {
$classes = explode(" ", $custom_classes);
foreach ($classes as $class) {
$elements[$delta]['#options']['attributes']['class'][] = Html::cleanCssIdentifier($class);
}
}
}
}
return $elements;
}
protected function getExampleToken() {
$entity_type = $this->fieldDefinition
->getTargetEntityTypeId();
$field = $this->fieldDefinition
->getName();
$type = $this->fieldDefinition
->getType();
if ($this->moduleHandler
->moduleExists('token')) {
$entity_type = $this->tokenEntityMapper
->getTokenTypeForEntityType($entity_type);
}
if ($type == 'file') {
return "[{$entity_type}:{$field}:description] ([file:size])";
}
else {
return "[{$entity_type}:{$field}:alt] ([file:size])";
}
}
protected function getTokenWarningMarkup() {
$type = $this->fieldDefinition
->getType();
if ($type == 'file') {
return $this
->t('<p>Enable the <a href="https://www.drupal.org/project/token\\" target="_blank\\">token module</a> to allow more flexible link text. For example, you would be able show the file description followed by the file size like this:<code>@example</code></p>', [
'@example' => $this
->getExampleToken(),
]);
}
else {
return $this
->t('<p>Enable the <a href="https://www.drupal.org/project/token" target="_blank">token module</a> to allow more flexible link text. For example, you would be able show the alt text followed by the file size like this:<code>@example</code></p>', [
'@example' => $this
->getExampleToken(),
]);
}
}
protected function getTokenExampleMarkup() {
$type = $this->fieldDefinition
->getType();
$delta_help = '';
if ($this->fieldDefinition
->getFieldStorageDefinition()
->getCardinality() != 1) {
$field = $this->fieldDefinition
->getName();
$delta_help = $this
->t('<p>Note that you do not need to indicate a delta value for the @field token. The appropriate delta is used automatically.</p>', [
'@field' => $field,
]);
}
if ($type == 'file') {
return $this
->t('<p>You can show the file description followed by the file size like this:<code>@example</code></p>@delta_help', [
'@example' => $this
->getExampleToken(),
'@delta_help' => $delta_help,
]);
}
else {
return $this
->t('<p>You can show the alt text followed by the file size like this:<code>@example</code></p>@delta_help', [
'@example' => $this
->getExampleToken(),
'@delta_help' => $delta_help,
]);
}
}
protected function addDeltaToTokens($string, $delta, $entity_token_type, $field) {
$string = str_replace("[{$entity_token_type}:{$field}:", "[{$entity_token_type}:{$field}:{$delta}:", $string);
$string = str_replace("[{$entity_token_type}:{$field}]", "[{$entity_token_type}:{$field}:{$delta}]", $string);
return $string;
}
}