Problem/Motivation

The core file module (Drupal\file\Plugin\Field\FieldFormatter\GenericFileFormatter) relies on template_preprocess_file_link() to print file type attributes and additionally provides a span wrapper for a download link.

This is useful with theming when one wants to add a download icon for instance.

Proposed resolution

Follow the core and provide file type attributes as well as span wrapper around the media download link.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mbovan created an issue. See original summary.

mbovan’s picture

I am uploading the least disruptive way I could find to accomplish the task without introducing a new template. However, a new template might be a better solution?

The code borrows some logic from template_preprocess_file_link().

Berdir’s picture

Status: Needs review » Needs work
  1. +++ b/src/Plugin/Field/FieldFormatter/DownloadLinkFieldFormatter.php
    @@ -45,19 +45,37 @@ class DownloadLinkFieldFormatter extends LinkFormatter {
    +      // @todo: replace with DI when this issue is fixed: https://www.drupal.org/node/2053415
    

    I think that @todo is bogus, formatters aren't typed data, they are regular plugins and can use DI.

    That said, the code is just moved around and kinda unrelated to clean that up here.

  2. +++ b/src/Plugin/Field/FieldFormatter/DownloadLinkFieldFormatter.php
    @@ -45,19 +45,37 @@ class DownloadLinkFieldFormatter extends LinkFormatter {
    +      $file = \Drupal::entityTypeManager()->getStorage('file')->load($item->getValue()['target_id']);
    

    Lets use $item->target_id here.

  3. +++ b/src/Plugin/Field/FieldFormatter/DownloadLinkFieldFormatter.php
    @@ -45,19 +45,37 @@ class DownloadLinkFieldFormatter extends LinkFormatter {
    +        '#prefix' => '<span class="' . implode(' ', $classes) . '">',
    +        '#suffix' => '</span>',
    

    hm, kinda ugly, wondering if we want to add a template for this, similar to file_link.

    Alternatively, we could maybe add the clases on the link, that might be enough for theming? I'll try to get some input on that.

Berdir’s picture

3. Discussed a bit, lets just add those classes to the link.

mbovan’s picture

  • Berdir committed d228ea4 on 8.x-2.x authored by mbovan
    Issue #3076527 by mbovan: Print file type attributes and wrap a download...
Berdir’s picture

Status: Needs review » Fixed

Committed and pushed, thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.