3

I'm trying to figure how I can obtain or print the file description that is uploaded on my node and display on my twig template

So far I have tried {{ file.entity.label }} which only prints the name of the file. I even tried {{ file.entity.value }} or {{ file.entity.title }}.

Here is a sample of the code I have been working on

{% for file in object.entity.field_file %}

    <li>{{ file.entity.label }}  {{ file_url(file.entity.uri.value) }}</li>

{% endfor %}

enter image description here

enter image description here

enter image description here

9
  • 2
    Rather than playing the guessing game, how about debugging it? Get good, A: What is easiest way to debug variables in nodes, views, blocks and page twig templates?
    – No Sssweat
    Jun 21, 2018 at 6:34
  • Already did tried using kint to debug the issue. Managed to search and look at the table in order for me to fetch the values but It seems I'm doing it incorrectly. I tried to fetch it using the following code {{ item.entity.description.value }}
    – clestcruz
    Jun 24, 2018 at 13:24
  • What template are you currently in?
    – leymannx
    Jun 26, 2018 at 1:25
  • @leymannx page.html.twig
    – clestcruz
    Jun 26, 2018 at 1:34
  • Really, page.html.twig shouldn't be the template to print field content like that. A new custom field formatter would be the way to go. But nevertheless what makes me really wonder is why as in your screenshot the description seems to be just empty "".
    – leymannx
    Jun 26, 2018 at 8:34

1 Answer 1

8
+50

The node variable is available in both page.html.twig and node.html.twig, so you can get your file description with

{% for file in node.field_myfile %}
  {{ file.description }}
{% endfor %}

Replace field_myfile with the machine name of your file field.

1
  • this is not working for me, but another thing works {{ file._referringItem.description }} Mar 27, 2019 at 13:09

Your Answer

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

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