1

I have a page of field images which are coming out quite weirdly when I kint their values. I can't seem to get the image url and the alt tag.

The code in the twig file looks like this:

field--field-staff-gallery.html.twig

{% for item in items %}
    {{ kint(item) }}
{% endfor %}

It's output looks like this: enter image description here

Now I can easily get to the url by doing:

{% for item in items %}
    {{ file_url(item.content['#markup']) }}
{% endfor %}

So how can I get the alt tag?

3

You can get the alt of image with:

item.content['#item'].value.alt
  • as you can see from the output above I don't seem to have the #item output on this field – KAT Nov 14 at 14:57
  • In the field twig there is two foreach one for multiple and other for no multiple fields, and also check your field display settings from admin/structure/types/manage/YourContentType/display i have Image as formatter not url to image, what you have ? – berramou Nov 14 at 15:00
  • ah, ok so that worked. i changed it to image. it was on url to image. not to be a bother, but how do I then get the url from this? item.content['#url'] is NULL – KAT Nov 14 at 15:06
  • The single field #item is only added by the image field formatter, but the field items element['#items'] are added for all field formatters. For a single value field it doesn't matter and you can use both, for a multi value field you need to add a key to the loop to access each item. – 4k4 Nov 14 at 20:40

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.