[PP-1] drupal_image() should include the cache tags for the image style

Created on 14 March 2023, over 1 year ago
Updated 18 April 2024, 7 months ago

Problem/Motivation

When constructing an image in Twig using drupal_image() with the $style argument, ImageViewBuilder::build does not include the cache tags for the image style on the returned render array. In certain scenarios (for example, when using S3fs), this can result in image derivatives not being rebuilt after an image style is flushed, because the page render cache isn't invalidated when the style's tag is cleared.

Steps to reproduce

  1. Install the Twig Tweak and S3fs modules.
  2. Configure S3fs to take over the public file store.
  3. Configure the site to enable the page cache.
  4. Use drupal_image() in a Twig template render an image URI using an image style or responsive image style.
  5. Visit a page that uses that Twig template. Confirm that the image derivative is generated and offloaded to S3 (i.e., the image src points to an S3 URI, not to the internal style generation endpoint). This may require a cache clear and refresh to fully generate.
  6. Edit the config for the chosen image style. This causes the image derivative to be flushed and removed from S3.
  7. Refresh the page from above.

Expected result: The page cache has been automatically cleared, so the image derivative is regenerated and displays normally.

Actual result: The page cache has not been cleared, so the image src continues to point to the (now deleted) image URI on S3, resulting in a broken image. Manually clearing the cache allows the image to be regenerated.

Proposed resolution

Add the cache tags for the image style(s) and responsive image style (if chosen) to the render array returned by ImageViewBuilder::build. See \Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter::viewElements for an example.

๐Ÿ› Bug report
Status

Needs review

Version

3.0

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States muriqui

Live updates comments and jobs are added and updated live.
  • Needs reroll

    The patch will have to be re-rolled with new suggestions/changes described in the comments in the issue.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024