Cache tags added by hook_block_view_BASE_BLOCK_ID_alter are ignored on blocks that implement MainContentBlockPluginInterface or TitleBlockPluginInterface

Created on 27 March 2025, 16 days ago

Problem/Motivation

We noticed that cache tags added by a hook_block_view_BASE_BLOCK_ID_alter() was not bubbled correctly on some of our blocks. It was blocks from the block_group module and these blocks implement MainContentBlockPluginInterface.

It seems to be caused by this code: https://git.drupalcode.org/project/drupal/-/blob/d81484de177f105e751a288...
viewMultiple() calls buildPreRenderableBlock() which triggers the hook_block_view_BASE_BLOCK_ID_alter hooks.
But the result of buildPreRenderableBlock() is merged with += which means the returned #cache will be ignored and it will keep the one it already has.

Steps to reproduce

Add a hook like this:

/**
 * Implements hook_block_view_BASE_BLOCK_ID_alter().
 */
function foo_block_view_page_title_block_alter(array &$build) {
  $build['#cache']['tags'][] = 'foo';
}

Inspect the X-Drupal-Cache-Tags header of the response, it does not contain this cache tag.

Proposed resolution

BlockViewBuilder::viewMultiple() should merge the cache metadata returned by BlockViewBuilder::buildPreRenderableBlock().

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

cache system

Created by

🇫🇷France prudloff Lille

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