How to access image alt value in twig?

Created on 10 August 2024, 5 months ago
Updated 12 August 2024, 5 months ago

Problem/Motivation

How do you access the media image alt value in twig e.g. paragraphs.html.twig
No luck with:

{{ content.field_testmedia.alt }}"

or

{{ content.field_testmedia['#items'].alt }}

...need it for Accessibilty

Thanks

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Active

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States hcanning

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @hcanning
  • Hi @hcanning,
    I'm not sure from where you are trying to acess the media image alt value. If you could clarify that it would be great!
    I have installed the module and created a paragragraph with a reference field media type image. After that configured the formatter to Thumbnail URL in 'Manage Dispaly' for the paragraph type. Created an entity and referenced the paragraph type. Accessed the media image value using the twig template.
    You can try out this, it worked for me.
    {{ paragraph.field_test_image.entity.field_media_image[0].alt }}
    Here field_test_image - it's the image field name within the paragraph,
    field_media_image - it's the default media type image field name.
    In case, you still can't access the media alt value in paragraph make sure that you have used media referenced in the paragraph type.
    Thanks

Production build 0.71.5 2024