set_attribute Twig filter doesn't work on responsive_image_formatter element

Created on 30 April 2025, 9 days ago

Problem/Motivation

The Twig set_attribute filter doesn't work on responsive_image_formatter elements, since it doesn't pass the #attributes down to the img element.

One example of a use case for wanting to call set_attribute on a responsive_image_formatter element is to mark the use of an image as decorative by setting alt="".

Steps to reproduce

  1. Install Drupal 11
  2. Enable Responsive Image
  3. Edit the Article content type, edit the default view mode, and set the image field to use the Responsive Image formatter
  4. Create a new Article and upload an image
  5. Create a template in your theme for field--field-image.html.twig
  6. Attempt to set an attribute on the responsive image by putting the following code in the template: {{ items.0.content|set_attribute('data-foo', 'bar') }}
  7. Inspect the rendered page HTML and notice there is no data-foo attribute on the image tag.

Proposed resolution

Override any attributes on the image tag with those passed via #attributes in the render array.

🐛 Bug report
Status

Active

Version

11.1 🔥

Component

responsive_image.module

Created by

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024