0

I want to a render array in my page to be cached, and I know that it's the same for every user in every circumstance.

How do I specify that its cache context is universal, rather than anything else?

4
  • 3
    The is no universal cache context. This is the default if you don't specify any cache context.
    – 4uk4
    Commented Nov 30, 2020 at 8:26
  • Cache contexts are used to vary the content - if there's no variance, there's no context to apply
    – Clive
    Commented Nov 30, 2020 at 10:31
  • I'm within a render context that's per-user, so presumably I need to specify a cache context to make the change of context?
    – joachim
    Commented Dec 1, 2020 at 1:19
  • Also, I thought the default for render arrays was user role?
    – joachim
    Commented Dec 1, 2020 at 1:19

1 Answer 1

3

There is no universal cache context. This is the default for a render array if you don't specify any cache context.

Render contexts are merged upstream. Additionally you can configure required cache contexts which are added to all render arrays before they are rendered.

Out of the box Drupal requires these three contexts:

core.services.yml

parameters:
  renderer.config:
    required_cache_contexts: ['languages:language_interface', 'theme', 'user.permissions']

You can override the parameter in /sites/default/services.yml (use default.services.yml as template).

2
  • If I don't specify contexts, then in cache_render, the key has [languages:language_interface]=en:[theme]=bartik:[user.permissions]=e26039e109d3f06cf28210caa84de1e43c3b0150b9feea8398758e278b3d7e7e so clearly it's varying by language, theme, and user permissions. That's not universal!
    – joachim
    Commented Dec 1, 2020 at 7:57
  • For a minimal install you could remove all of the three required contexts and decide for each render array your own set of cache contexts. This is not recommended for normal use cases, though, this might have security implications.
    – 4uk4
    Commented Dec 1, 2020 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.