I have a content type with a field field_file_attachment that allows uploading multiple files with txt, pdf, gif and img extensions.

I would like to get links, descriptions and extensions of all these files printed in my Twig file node--page--full.html.twig.

Is this possible? Should I create a preprocess function for page?


You should be able to do something like this,

{% for file in node.field_file['#items'].entity %}
  {{ file.uri.value }}
  {{ file.name.value }}
{% endfor %}
  • Unfortunately it doesnt work for me. Oct 17 '18 at 11:26
  • I updated my answer
    – user72672
    Oct 17 '18 at 11:39
  • Ah I didn’t see that you posted and answer yourself
    – user72672
    Oct 17 '18 at 11:39
  • I think the best answer is a combination of both, loop over the field in the node {% for file_item in node.field_file %} and get the file name {{ file_item.entity.name.value }} and other file values.
    – 4k4
    Oct 17 '18 at 11:47

Maybe it will be helpful for other, I use:

{% for file in content.field_file['#items'] %}
  {{ devel_dump(file.entity.name.value)}}
{% endfor %}

It gives me acces to all values from file field.


You need to pass your file or image field uri value with file_url() function, Read the Drupal Docs, Then you will have field's exact value, not rendered

{{ file_url(node.field_policy_pdf.entity.uri.value) }} - if it has limited (1) value.

Else you need to use twig {% for %} loop

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.