1

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

  • 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 '18 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 '18 at 13:24
  • What template are you currently in? – leymannx Jun 26 '18 at 1:25
  • @leymannx page.html.twig – clestcruz Jun 26 '18 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 '18 at 8:34
5
+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.

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

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.