6

I want to print alt or title field image file in node twig template Drupal 8. I tried use {{ node.field_images.entity }} to debug get value title or alt but in class \Drupal\file\Entity\File not function get those ! I saw {{node.field_images.entity.fileuri}} in link

So how to used {{node.field_images.entity}} to get value alt or title file images ?

Thanks !

3 Answers 3

19

The alt and title properties are stored in the image field, not in the file entity. You can access them by replacing the entity property with alt or title:

{{ node.field_image.title }}
{{ node.field_image.alt }}
0
9

In the field template (field.html.twig) this will work.

{% for item in items %}
    {{ item.content['#item'].alt }}
{% endfor %}

In node.html.twig or paragraph.html.twig, this will work.

{{ content.field_image['#items'].alt }}
0

In the node template:

{{ content.field_image[0]['#attributes']['alt'] }}

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