I have created a content type with a number of fields including file fields. In the node template for that CT (node--unit.html.twig) I am able to get the details with the following code:

{% if content.field_files.0 %}
   {% for key, value in content.field_files if key|first != '#' %}
      <a href="{{ file_url(content.field_files[key]['#file'].uri.value) }}">{{ content.field_files[key]['#description']}}</a>
   {% endfor %}
{% endif %}

Now I'm trying to get those same details for the field inside a view that lists nodes of that CT but the above code doesn't work. In fact all I can get from the field is the 'content' using the following code inside the views-view-field template (views-view-fields--units-e-resource-sheets.html.twig):

{{ fields.field_e_resources.content}}

All I can find using kint and other tools is the target_id attribute. Do I have to use this to retrieve those other details in the view?

1 Answer 1


Well with some pointers from colleagues I have found out what I was doing wrong!

In the node you have all that info already there but views and their displays are more stripped back to keep them 'lean' so you have to be explicit that you need that info...

Step 1 - add a 'relationship' to the field

In the settings for the display of the view, I added a relationship to the file from the field under the 'advanced' settings using the 'add' button and it shows like this:

screenshot of the advanced settings for the view display

Step 2 - add fields for the stuff from the relationship

Under the fields for the view display I can now add fields for the relationship I just created. I added one for the URI and one for the filesize using the 'add' button and they show like this:

Image of the fields in a view display

Step 3 - build the template

Now you can use those just like any regular field in the template. Here is the code I'm using to build a link with the file extension, filesize and a link to download the file:

<a href="{{ file_url(fields.uri.content|striptags|trim|convert_encoding('UTF-8', 'HTML-ENTITIES')) }}" target="_blank">Download
  (.{{ fields.field_e_resources.content|striptags|split('.')[1] }})({{ fields.filesize.content }})


I couldn't access the details for the uploaded file because they aren't included by default so by building the relationships as required you can add to the available fields and get the job done!

  • 1
    Instead of displaying fields and templating your way through this (which both can become a hell to maintain in the long run) use a view mode and field formatters for the node type and then let the view display the nodes with this view mode.
    – leymannx
    Feb 5, 2020 at 2:43
  • Do you think that is less complex when you have a very specific layout to build? I agree that templating is a bit hellish... But I like that everything is in one or a small number of places. If I understand your suggestion, each field would need to have mark-up added through the admin for each situation so the amount of mark-up would be the same but just in different places no? Feb 6, 2020 at 8:52

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.