0

I have a content type with two fields, one pdf field which will be downloaded using the Download File module which provides a formatter (file download) to automatically download the pdf file with one click and one image field linked to the pdf field, using the Linked Field module. This works in the node without problems. The problem is when trying to rewrite the results of the image field in Views. I set the file field with the "File download fomatter" and exclude it from display. Then, I try to rewrite results of the image field like this

<a href="{{ file_download_field | striptags}}">{{ image_field }}</a>

or

<a href="{{ file_download_field.url }}">{{ image_field }}</a>

but this is not working.

The files are stored in the folder

[root]/file-download/download/public[node:field_file_download:entity:fid]

What am I missing?

  • What does the File field display like if you don't exclude it from Display? You only want to use the URL then you should set the Formatter of that field accordingly. – prkos Sep 30 '18 at 23:07
  • I just want the pdf file to download with one click, if you show it, it will open in a browser tab using the default reader in the browser – Jorge Montoya Oct 1 '18 at 13:15
1

The "File download fomatter" you're using doesn't output just the path to the file, it's a more complex formatter and you can't use it in your case as a path to download the file.

In Drupal 8 there is a separate Field in File Views called URI that you need to use.

File URI settings in Drupal 8

Depending on which Drupal 8 version you're using you'll see only the formatter File URI and you need to tick the option Display the file download URI to get the link you can use in an anchor href attribute later when overriding fields.

Do not enable Link this field to the file download URL, this outputs complex anchor HTML, not just the path that you're after.

You can exclude it from display, position it above the image field and Rewrite results for the image:

<a href="{{ uri }}">{{ image_field }}</a>

or let Drupal link it:

Rewrite results Views Field output Link

| improve this answer | |
  • Thanks for your answer prkos. I don't have the file URI field available in my Drupal 8.6.1 installation. Do you know what's the reason for that? I have: 'Table of files, Video, Generic file, RSS enclosure, URL to file and Audio' as formatter options in my view. URL to file did the trick, outputting it as a custom link. – Jorge Montoya Oct 2 '18 at 0:22
  • I'm also checking on D8.6.1. Which Field are you using? It's a bit confusing I guess because the Field is called URI from the File Category, and then within the field the Formatter has a similar name File URI. I see more options under Formatter for that field on my D8.4.4 instance but I probably have different field configuration there too. Are you using a Field from Content Category? That is pulling information from the node, and not directly from the Files table in the database. – prkos Oct 2 '18 at 11:10
  • Yes, I'm using a field that I'm pulling from the content. Views does not any 'File-related' fields available – Jorge Montoya Oct 2 '18 at 21:04
  • 1
    Is it File type or Content type Views? If it's Content then you'd have to introduce a Relationship to File data to be able to see that additional File category with Fields from Files database table. – prkos Oct 2 '18 at 23:28
  • 1
    Yes, of course, viewmodes are very useful, and can be used in Views. But you either have some bugs in your site or maybe we're not talking about the same thing. You have to separate two different things, Field name, and Formatter. In this particular case they are named very similar, Field called URI from Category File, and Formatter within that field called File URI. There may be other Fields related to files that have that Formatter, but what I'm describing is for the particular Field called URI. Try and find that Field and add it to your Views, and investigate why the Broken handler. – prkos Oct 6 '18 at 11:36

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.