View source
<?php
namespace Drupal\media_entity_download\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\link\Plugin\Field\FieldFormatter\LinkFormatter;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class DownloadLinkFieldFormatter extends LinkFormatter {
public static function defaultSettings() {
return [
'disposition' => ResponseHeaderBag::DISPOSITION_ATTACHMENT,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['disposition'] = [
'#type' => 'radios',
'#title' => t('Download behavior'),
'#description' => t('Whether browsers will open a "Save as..." dialog or automatically decide how to handle the download.'),
'#default_value' => $this
->getSetting('disposition'),
'#options' => [
ResponseHeaderBag::DISPOSITION_ATTACHMENT => $this
->t('Force "Save as..." dialog'),
ResponseHeaderBag::DISPOSITION_INLINE => $this
->t('Browser default'),
],
];
return $elements;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$settings = $this
->getSettings();
if ($settings['disposition'] == ResponseHeaderBag::DISPOSITION_ATTACHMENT) {
$summary[] = $this
->t('Force "Save as..." dialog');
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$parent = $items
->getParent()
->getValue()
->id();
$settings = $this
->getSettings();
foreach ($items as $delta => $item) {
$route_parameters = [
'media' => $parent,
];
$url_options = [];
if ($delta > 0) {
$route_parameters['query']['delta'] = $delta;
}
$file = \Drupal::entityTypeManager()
->getStorage('file')
->load($item->target_id);
if (empty($file)) {
continue;
}
$filename = $file
->getFilename();
$mime_type = $file
->getMimeType();
$url_options['attributes'] = [
'type' => "{$mime_type}; length={$file->getSize()}",
'title' => $filename,
'class' => [
'file',
'file--mime-' . strtr($mime_type, [
'/' => '-',
'.' => '-',
]),
'file--' . file_icon_class($mime_type),
],
];
$url_options['query'][$settings['disposition']] = NULL;
if ($settings['disposition'] == ResponseHeaderBag::DISPOSITION_INLINE) {
if (!empty($settings['target'])) {
$url_options['attributes']['target'] = $settings['target'];
}
}
if (!empty($settings['rel'])) {
$url_options['attributes']['rel'] = $settings['rel'];
}
$url = Url::fromRoute('media_entity_download.download', $route_parameters, $url_options);
$elements[$delta] = [
'#type' => 'link',
'#url' => $url,
'#title' => $filename,
];
}
return $elements;
}
protected function viewValue(FieldItemInterface $item) {
return nl2br(Html::escape($item->value));
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return $field_definition
->getFieldStorageDefinition()
->getTargetEntityTypeId() == 'media';
}
}