Follow-up issue from #2986967: Use Media name for the "Download Link" title where we discussed how to make the link title more flexible.
That issue was a bug report, this is a feature request to enhance functionality:

Wouldn't it perhaps be better to make the link formatter more flexible by adding options and perhaps work with media tokens instead of a hard coded solution? I don't think that there will be THAT ONE perfect link text.

Yes a selection sounds like a good option:

  • Media title
  • File name
  • Description
  • Perhaps a custom text option (translatable) would also make sense to have a custom link title. I think that's also needed in many cases.

Also we could add a checkbox to use the file description as title text for the link to provide flexibilty to use it as tooltip, etc... with minimal effort.

Let's discuss the demand for that here. In our case it would have helped a lot.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anybody created an issue. See original summary.

Anybody’s picture

Title: Make link title editable » Make link title editable / add token support / more options
lolcode’s picture

I took a pass at this using the approach of adding a theme function that receives the media object.
This makes it possible for site owners to add in whatever variables they like from the media object.

Anybody’s picture

Hi @lolcode,

great work! That makes it a lot more flexible already, at least for theme developers. While I still think tokens would be additionally helpful for other cases and administrators, we should first focus on your approach.

That patch looks allright! Of course it would be best to add a test to ensure this patch doesn't change the markup for existing sites.
Thanks a lot so far.

What do you think about token support for the link title in the patch?

lolcode’s picture

I could attempt to add the code for some of the field formatter options as well.

One reason I did not is that it would clash with settings changes pending in this issue: https://www.drupal.org/project/media_entity_download/issues/3008834 while a theme function can be added independently.

I also realised that I forgot to add cache tags to the theme function. I have update the patch to include them.

The markup test is a good idea but would have to exist before this patch is applied therefore it would need to be a separate issue, merged first.

almunnings’s picture

Patch super useful for document downloads.

{{ link|merge({'#title': media_entity.name.value }) }} in a twig and happy!

lolcode’s picture

Sorry, I realise that I missed adding the staged files in the patch in #5. Here is one with the cache tags added that has all the files.

Berdir’s picture

Status: Needs review » Needs work

Needs a reroll and a complete, combined patch.

capysara made their first commit to this issue’s fork.

capysara’s picture

capysara’s picture

Status: Needs work » Needs review
wrd’s picture

This is working like a charm for me.

elgandoz’s picture

Status: Needs review » Reviewed & tested by the community

#11 works great on 8.x-2.1, thanks @capysara!
For reference I altered the button like this:

{% set download_title %}
  <span class="material-icons" focusable="false" aria-hidden="true">download</span>Download {{ media_entity.name.value }}
{% endset %}

{% block media_entity_download_link %}
  {{ link(download_title, url, { 'class':['button', 'button--dark', 'media-download']}) }}
{% endblock media_entity_download_link %}
elgandoz’s picture

I'm unsure if this is off-topic, but the default media formatter for "documents" also prints the filetype and filesize alongside the title, eg. Media name (PDF, 12.3KB).
I added here a patch that adds those 2 variables in the template, since it could be a fairly common requirement (and also a good practice).

For reference: alternatively, this can also be done in the in your .theme file using a preprocess hook, without my attached patch:

/**
 * Implements hook_preprocess_HOOK().
 *
 * HOOK: 'media_entity_download_link'.
 *
 * Provides the filetype and the formatted filesize for the media entity download link.
 */
function mytheme_preprocess_media_entity_download_link(array &$variables): void {
  if (!empty($media = $variables['media_entity']) && $media instanceof MediaInterface) {
    $file_field = $media->getSource()->getSourceFieldDefinition($media->get('bundle')->entity);
    if ($file_field && $media->hasField($file_field->getName()) && $media->get($file_field->getName())->entity instanceof FileInterface) {
      $file_entity = $media->get($file_field->getName())->entity;
      $variables['filesize'] = format_size($file_entity->getSize());
      $variables['filetype'] = strtoupper(pathinfo($file_entity->getFilename(), PATHINFO_EXTENSION));
    }
  }
}
robcarr’s picture

Status: Needs review » Reviewed & tested by the community

The patch at 11 works fine.
The patch at 15 is really helpful, but not necessarily relevant to this issue, and should be added to a fresh issue as a separate feature request.

bramvandenbulcke’s picture

Patch #11 is working well. I'm using Drupal 10.2.5 at the moment.

Steps taken:

  • Make sure download link is set for Document under /admin/structure/media/manage/document/display. And for my use case: add Name to the form display
  • Add patch #11 to composer.patches.json and run composer install
  • Copy the media-entity-download-link.html.twig template from /modules/contrib/media_entity_download/templates to the theme folder
  • Apply the changes in the markup. I'm using the Name field as the link text. This makes the most sense (the description field is not available anyway). This code inside the Twig block: {{ link|merge({'#title': media_entity.name.value }) }}