Hi,

I have a node with an image field.

On my node--name.html.twig I can print my image with {{ content.field_image }}.

But I want to add a class on my <img>. Is there a way to do it in my template ?
I think so, because when I print the keys on my content.field_image.0 I have #image_style and #item_attributes.

Thanks.

Comments

Keenegan created an issue. See original summary.

Keenegan’s picture

Issue summary: View changes
idebr’s picture

Hi Keenegan,

Have a look at the contributed module Twig Field Value: https://www.drupal.org/project/twig_field_value

It allows the following code:

<img src={{ file_url(content.field_image|field_target_entity.uri.value) }} alt={{ content.field_image|field_raw('alt') }} />
Keenegan’s picture

Status: Active » Closed (works as designed)

Thanks for the module !

I was just wondering if I could do this without any contrib module, but after researching I think it's not possible.

joelpittet’s picture

It's possible without contrib, instead of content look at the values in node IIRC, then you have access to the field without the render information.