1

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

3

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>
</div>

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. – mediaashley Sep 20 '17 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 '17 at 10:36
1

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.

Loosely:

file = $variables['elements']['#media']->field_file->entity;
$file->setFilename($variables['name']);

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.

1

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! – mediaashley Apr 4 at 12:07
0

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. – mediaashley Nov 15 '17 at 13:56
0
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);
      $file->setFilename($name);
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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