I have a "Document" media bundle, and I have a custom view mode called "Embed".

In that view mode, I have removed everything except the media name property and my document file field.

For media name the only format available is plain text, with an option to "Link to Media". This provides a link like media/2, where you can click on a link to download the file.

For my document field, I get Generic file (the file name with a link to it), RSS enclosure, Table of files, and URL to file.

Does anyone have a simple way to generate a link like Generic file but show the media name instead of the file name? e.g. Handbook vs handbook__2017_fin_updated_some_other_rubish_26.pdf

6 Answers 6


This is not possible with just the formatter configuration, someone would need to create a "Link to file with entity label" formatter, which would also not be that complicated.

But you can do it quite easily directly in a media template, the name would be something like media--document--embed.html.twig and the content then looks like this:

<div{{ attributes }}>
  <a class="document-link" title="{{ name }}" href="{{ file_url(elements['#media'].field_document.entity.uri.value) }}" target="_blank" rel="noopener">{{ name }}</a>

I also included a target and noopener rel here, but that's of course up to you.

  • That's pretty much what I'm finding. But this doesn't feel like something that should be done in the theme. Seems like pretty standard need, unless I'm missing something… You can achieve this with the field_group_link, but that feels like overkill. Sep 20, 2017 at 8:37
  • Patches are always welcome to add new features to contrib/core modules (media is now in core, so new features won't be added to media_entity). But it's not as straight-forward as you might think, someone else might want to use a separate field for the displayed name, or the description of the file field, or use an icon (in my example I actually removed a fontawesome icon within the link) and so on, so I see nothing wrong with doing this in the theme
    – Berdir
    Sep 20, 2017 at 10:36
function template_preprocess_media(array &$variables) {
  $media = $variables['media'];
  $name = $media->getName();
  if (!empty($name)) {
    $mediaArray = $media->toArray();
    if (!empty($mediaArray['field_media_file'][0]['target_id'])) {
      $fileId = $mediaArray['field_media_file'][0]['target_id'];
      $file = \Drupal\file\Entity\File::load($fileId);
  • 1
    Still working for Drupal 9, just had to replace 'field_media_file' with 'field_media_document'
    – Shawn W
    Dec 21, 2022 at 5:40

How I did this

As per @Berdir's suggestion, I have done this in the theme. However, instead of hijacking the output in the media (which is perfect for control based on the media display mode), I have chosen to set the filename.

Unfortunately, I couldn't find a way to get the media entity from the file, so I use template_preprocess_media which has both the media entity and the file entity, and then the setFilename() method.


file = $variables['elements']['#media']->field_file->entity;

For improved accessibility, you should "show" the file size and type, and while this available here, it felt cleaner to do this in file-link.html.twig where a bunch of other nice things are added to the attributes for you.

  • Coming in a bit late, but here's an example of a custom file display formatter which uses the media entity "name" field as the link text: drupal.org/sandbox/imclean/3051849
    – imclean
    May 1, 2019 at 6:57

The best way to implement this is not to set filename. Instead, you should set the #description of the file. In the following example, I have a media of a bundle called "file" and in it, I have a file field called "field_media_file". Then I set the description of each file to be the media name.

if ($variables['media']->bundle() == 'file' && !empty($variables['content']['field_media_file'])) {
    foreach (Element::children($variables['content']['field_media_file']) as $delta) {
      $variables['content']['field_media_file'][$delta]['#description'] = $variables['name'];
  • Interesting! I'm not working on anything that would use this now, but I'll give this a go next time! Apr 4, 2019 at 12:07
  • The problem with this approach is that file description is not mandatory and leaves open the likelihood that editors will not supply it. There is an old issue about this which is not getting anywhere fast. See drupal.org/project/drupal/issues/2320877 Nov 27, 2020 at 17:39

This module seems like it does what you are asking (BTW had the same question which led me here): https://www.drupal.org/project/link_plain_text_formatter

  • Thanks, I had seen Drupal 7 at the top of the description and dismissed this :/ However, it doesn't appear to help here. Nov 15, 2017 at 13:56

You can also do a file link preprocess function that can look up the media entity for the file and and then set the link text accordingly. Replace "field_media_file" with your media entity's field name. If you want to do this for more than one media type, you'll have to expand it accordingly as well.

function template_preprocess_file_link(array &$variables) {
  $file = $variables['file'];
  $result = \Drupal::entityQuery('media')
    ->condition('bundle', 'document')
    ->condition('field_media_file.target_id', $file->id())
  if (!empty($result)) {
    $media = \Drupal::entityTypeManager()->getStorage('media')->load(reset($result));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.