core/lib/Drupal/Core/Render/Renderer.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php index 40a2a9e..9205f60 100644 --- a/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -174,13 +174,19 @@ protected function doRender(&$elements, $is_root_call = FALSE) { } static::$stack->push(new BubbleableMetadata()); - // Bubbleable rendering metadata that has configurable defaults. - $required_cache_contexts = $this->rendererConfig['required_cache_contexts']; - if (isset($elements['#cache']['contexts'])) { - $elements['#cache']['contexts'] = Cache::mergeContexts($elements['#cache']['contexts'], $required_cache_contexts); - } - else { - $elements['#cache']['contexts'] = $required_cache_contexts; + // Set the bubbleable rendering metadata that has configurable defaults, if: + // - this is the root call, to ensure that the final render array definitely + // has these configurable defaults, even when no subtree is render cached. + // - this is a render cacheable subtree, to ensure that the cached data has + // the configurable defaults (which may affect the ID and invalidation). + if ($is_root_call || isset($elements['#cache']['keys'])) { + $required_cache_contexts = $this->rendererConfig['required_cache_contexts']; + if (isset($elements['#cache']['contexts'])) { + $elements['#cache']['contexts'] = Cache::mergeContexts($elements['#cache']['contexts'], $required_cache_contexts); + } + else { + $elements['#cache']['contexts'] = $required_cache_contexts; + } } // Try to fetch the prerendered element from cache, run any