Presently, the Download Link title is set to the filename for which the Media Entity Download Link is created.
So, when a file referenced from the Media is updated and the download link is maintained by this module, it will display the new file name. Which, in some cases is with a suffix _0, _1 and so forth.

A better solution is to have an ability to display Media Name instead of the filename.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

justy created an issue. See original summary.

justy’s picture

Here is the patch which changes the "Download Link" title from filename to Media Name.

webflo’s picture

Status: Active » Needs review
Anybody’s picture

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.

webflo’s picture

Tokens can be tricky because a entity reference field has multiple deltas. The token would have to be dynamic. I think having two options, media title and filename are a good start. I we could maintain BC relatively easy.

Anybody’s picture

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.

Anybody’s picture

I just added description in #6 as option because I saw the Core file formatter has this setting for files. So we should offer the same.

osopolar’s picture

On 8.x-1.x I get the following error:

Error: Call to undefined method Drupal\media_entity\Entity\Media::getName() in Drupal\media_entity_download\Plugin\Field\FieldFormatter\DownloadLinkFieldFormatter->viewElements() (line 53 of modules/contrib/media_entity_download/src/Plugin/Field/FieldFormatter/DownloadLinkFieldFormatter.php).

load() seems to return an object of EntityInterface, which does not has the getName() method, but label(). I changed that for 8.x-1.x.

BTW: Last week I read Larry's article from 2013 On empty return values, and was wondering if we can chain ->load($route_parameters['media'])->label(); as load() may return null?

Status: Needs review » Needs work
vuil’s picture

@osopolar Your error is seems to be similar as this issue fix: https://www.drupal.org/project/media_entity_download/issues/3014057

Anybody’s picture

Thank you for your patch @osopolar!

For general information #8 is just a reroll of #2, but doen't contain the proposed options from #6 yet.
Hope that me or someone else will find the time to create a patch containing the options from #6 soon, busy times...

osopolar’s picture

Reviewing this issue I found, that the patch in #8 contains the line:

+use function kint;

I guess this line was necessary for debugging but should be removed. At the moment I have no resources to do test that.

vuil’s picture

Status: Needs work » Fixed
vuil’s picture

Status: Fixed » Closed (fixed)
Anybody’s picture

ChrisSnyder’s picture

For those that are trying to update to the 2.0 version of this module and were relying on the patch from #8, I have rerolled that patch to allow you to keep using the media name. This patch may be necessary until #3059830: Make link title editable / add token support / more options is completed.

vuil’s picture

juliekramer’s picture

I rerolled the patch from #16 to be able to patch version 2.1.0 of media_entity_download

fskreuz’s picture

Layout Builder builds placeholder media objects for live preview, which aren't real media, don't have IDs, and don't exist in the database. Tweaked the patch to guard against these objects. Also grabbing the name of the media from the parent entity instead of using the route and pulling it from the DB.