diff --git a/media_entity_download.module b/media_entity_download.module new file mode 100644 index 0000000..159f23a --- /dev/null +++ b/media_entity_download.module @@ -0,0 +1,22 @@ + [ + 'variables' => [ + 'media_entity' => NULL, + 'link' => NULL, + 'url' => NULL, + 'title' => NULL, + 'filetype' => NULL, + 'filesize' => NULL, + 'options' => NULL, + ], + 'template' => 'media-entity-download-link', + ], + ]; +} diff --git a/src/Plugin/Field/FieldFormatter/DownloadLinkFieldFormatter.php b/src/Plugin/Field/FieldFormatter/DownloadLinkFieldFormatter.php index d40f636..5bf4824 100644 --- a/src/Plugin/Field/FieldFormatter/DownloadLinkFieldFormatter.php +++ b/src/Plugin/Field/FieldFormatter/DownloadLinkFieldFormatter.php @@ -84,6 +84,7 @@ class DownloadLinkFieldFormatter extends LinkFormatter { // @todo: replace with DI when this issue is fixed: https://www.drupal.org/node/2053415 /** @var \Drupal\file\FileInterface $file */ $file = \Drupal::entityTypeManager()->getStorage('file')->load($item->target_id); + $media = \Drupal::entityTypeManager()->getStorage('media')->load($parent); if (empty($file)) { continue; @@ -121,9 +122,22 @@ class DownloadLinkFieldFormatter extends LinkFormatter { $url = Url::fromRoute('media_entity_download.download', $route_parameters, $url_options); $elements[$delta] = [ - '#type' => 'link', + '#theme' => 'media_entity_download_link', + '#media_entity' => $media, + '#link' => [ + '#type' => 'link', + '#url' => $url, + '#title' => $filename, + '#options' => $url_options, + ], '#url' => $url, '#title' => $filename, + '#filetype' => strtoupper(pathinfo($file->getFilename(), PATHINFO_EXTENSION)), + '#filesize' => format_size($file->getSize()), + '#options' => $url_options, + '#cache' => [ + 'tags' => $media->getCacheTags(), + ] ]; } diff --git a/templates/media-entity-download-link.html.twig b/templates/media-entity-download-link.html.twig new file mode 100644 index 0000000..a8a5b00 --- /dev/null +++ b/templates/media-entity-download-link.html.twig @@ -0,0 +1,22 @@ +{# +/** + * @file + * Default theme implementation to display a media entity download link. + * + * Available variables: + * - media_entity: Full media entity object. + * - link: The default full link render array. + * - url: The link URL. + * - title: The link title. + * - filetype: The file type extension (eg. PDF). + * - filesize: The formatted file size (eg. 12.3KB). + * - options: The link options. + * + * @see DownloadLinkFieldFormatter.php + * + * @ingroup themeable + */ +#} +{% block media_entity_download_link %} + {{ link }} +{% endblock media_entity_download_link %}