Extend ViewsBlockBase to merge cache metadata from display handler

Created on 13 December 2024, 9 days ago

Problem/Motivation

Currently, the ViewsBlockBase class does not fully leverage the cache metadata (contexts, tags, max-age) provided by the associated view's display handler. This can lead to inconsistencies in cache behavior when using Views blocks, especially when the display handler defines specific cache settings.

For example:

  • Cache contexts from the view are not merged into the block's cache contexts.
  • Cache tags provided by the view's display handler are ignored in the block context.
  • Cache max-age of the block does not reflect the display handler's cache max-age.

This limits the ability to align the block's caching behavior with that of the View, potentially resulting in outdated content being served or inefficient cache invalidation.

Steps to reproduce

1. Create a View with specific cache metadata in its display handler (e.g., cache contexts or tags).

2. Add the View as a block on a page.

3. Observe that the cache behavior of the block does not reflect the settings in the display handler.

Proposed resolution

Introduce overrides in the ViewsBlockBase class for the following methods:

  • getCacheContexts()
  • getCacheTags()
  • getCacheMaxAge()

These methods will merge the cache metadata from the parent block class with the cache metadata from the view's display handler, ensuring consistent and efficient caching.

Remaining tasks

  • Write unit and kernel tests to ensure the cache metadata (contexts, tags, max-age) is correctly merged between the block and the view's display handler.
  • Test scenarios where the display handler provides:
    • Additional cache contexts.
    • Specific cache tags.
    • A finite or infinite cache max-age.
  • Verify that cache invalidation works as expected when display handler cache metadata changes.

User interface changes

None.

Introduced terminology

None.

API changes

No API changes. The modifications enhance the existing methods without altering their signatures or introducing breaking changes.

Data model changes

None.

Release notes snippet

The ViewsBlockBase class now merges cache metadata from the associated view's display handler for contexts, tags, and max-age. This ensures consistent cache behavior between Views blocks and their display handlers.

🐛 Bug report
Status

Active

Version

11.1 🔥

Component

views.module

Created by

🇫🇷France xavier.masson Haute-Normandie

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

Comments & Activities

Production build 0.71.5 2024