Add cache metadata automatically

Created on 15 May 2024, about 1 month ago
Updated 27 May 2024, 29 days ago

Problem/Motivation

Use of a global variable does not automatically add the necessary cache metadata to the output. You still need a preprocess function for that.

<!--break-->

Example: something like {{ global_variables.current_path }}<code> in a template is not sufficient, you still need to add the <code>url cache context in PHP somewhere, usually in a preprocess function.

Steps to reproduce

  1. Ensure render caching is on.
  2. Place a block somewhere on the page. Let's take the powered by Drupal block as an example, because it's trivial.
  3. Override the template for that block. Add {{ global_variables.current_path }} somewhere in the template.
  4. Visit a page. The path should be correct. The rendered output of the block will be cached.
  5. Visit a different page. The cached block output is used, and the path is wrong.

Proposed resolution

Wrap the string value in an object that also holds the necessary cache metadata. In this example that's the url cache context.

Remaining tasks

Make a patch for the CurrentPath plugin as a proof of concept.

Review this approach. If it's good then path the other affected plugins.

User interface changes

None.

API changes

Maybe. The value returned is no longer a string, although it can implement \Stringable and __toString(). That's fine for most cases, but more obscure usage like {{ global_variables.path|upper }} might be a problem.

Data model changes

None.

✨ Feature request
Status

Needs work

Version

2.2

Component

Code

Created by

πŸ‡ΈπŸ‡ͺSweden erik.erskine

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

Merge Requests

Comments & Activities

Production build 0.69.0 2024