Commit 17d2e5cf authored by catch's avatar catch
Browse files

Issue #2459003 by Wim Leers: #cache[cid] breaks bubbling

parent 938e2f64
......@@ -358,7 +358,7 @@ protected function doRender(&$elements, $is_root_call = FALSE) {
// Cache the processed element if #cache is set, and the metadata necessary
// to generate a cache ID is present.
if (isset($elements['#cache']) && (isset($elements['#cache']['keys']) || isset($elements['#cache']['cid']))) {
if (isset($elements['#cache']) && isset($elements['#cache']['keys'])) {
$this->cacheSet($elements, $pre_bubbling_cid);
}
......@@ -713,9 +713,7 @@ protected function cacheSet(array &$elements, $pre_bubbling_cid) {
/**
* Creates the cache ID for a renderable element.
*
* This creates the cache ID string, either by returning the #cache['cid']
* property if present or by building the cache ID out of the #cache['keys'] +
* #cache['contexts'].
* Creates the cache ID string based on #cache['keys'] + #cache['contexts'].
*
* @param array $elements
* A renderable array.
......@@ -729,12 +727,9 @@ protected function createCacheID(array $elements) {
return FALSE;
}
if (isset($elements['#cache']['cid'])) {
return $elements['#cache']['cid'];
}
elseif (isset($elements['#cache']['keys'])) {
if (isset($elements['#cache']['keys'])) {
$cid_parts = $elements['#cache']['keys'];
if (isset($elements['#cache']['contexts'])) {
if (!empty($elements['#cache']['contexts'])) {
$contexts = $this->cacheContexts->convertTokensToKeys($elements['#cache']['contexts']);
$cid_parts = array_merge($cid_parts, $contexts);
}
......
......@@ -119,9 +119,6 @@ public function renderPlain(&$elements);
* mapped to the current user's ID.)
* - 'max-age': A time in seconds. Zero seconds means it is not cacheable.
* \Drupal\Core\Cache\Cache::PERMANENT means it is cacheable forever.
* - 'cid': Specify the cache ID directly. Either 'keys' or 'cid' is
* required. If 'cid' is set, 'keys' is ignored. Use only if you have
* special requirements.
* - 'bin': Specify a cache bin to cache the element in. Default is
* 'default'.
* When there is a render cache hit, there is no rendering work left to be
......
......@@ -96,33 +96,6 @@ public function testContextBubblingEdgeCases(array $element, array $expected_top
public function providerTestContextBubblingEdgeCases() {
$data = [];
// Bubbled cache contexts cannot override a cache ID set by #cache['cid'].
// But the cache context is bubbled nevertheless.
$test_element = [
'#cache' => [
'cid' => 'parent',
],
'#markup' => 'parent',
'child' => [
'#cache' => [
'contexts' => ['foo'],
],
],
];
$expected_cache_items = [
'parent' => [
'#attached' => [],
'#cache' => [
'contexts' => ['foo'],
'tags' => [],
'max-age' => Cache::PERMANENT,
],
'#post_render_cache' => [],
'#markup' => 'parent',
],
];
$data[] = [$test_element, ['foo'], $expected_cache_items];
// Cache contexts of inaccessible children aren't bubbled (because those
// children are not rendered at all).
$test_element = [
......@@ -582,11 +555,11 @@ public static function bubblingPreRender($elements) {
'#markup' => $placeholder,
],
'child_nested_pre_render_uncached' => [
'#cache' => ['cid' => 'uncached_nested'],
'#cache' => ['keys' => ['uncached_nested']],
'#pre_render' => [__CLASS__ . '::bubblingNestedPreRenderUncached'],
],
'child_nested_pre_render_cached' => [
'#cache' => ['cid' => 'cached_nested'],
'#cache' => ['keys' => ['cached_nested']],
'#pre_render' => [__CLASS__ . '::bubblingNestedPreRenderCached'],
],
];
......
......@@ -72,7 +72,7 @@ public function testPostRenderCacheWithColdCache() {
$this->setUpRequest('GET');
// GET request: #cache enabled, cache miss.
$element['#cache'] = ['cid' => 'post_render_cache_test_GET'];
$element['#cache'] = ['keys' => ['post_render_cache_test_GET']];
$element['#markup'] = '<p>#cache enabled, GET</p>';
$output = $this->renderer->renderRoot($element);
$this->assertSame($output, '<p>overridden</p>', 'Output is overridden.');
......@@ -100,7 +100,7 @@ public function testPostRenderCacheWithColdCache() {
// GET request: #cache enabled, cache hit.
$element = $test_element;
$element['#cache'] = ['cid' => 'post_render_cache_test_GET'];
$element['#cache'] = ['keys' => ['post_render_cache_test_GET']];
$element['#markup'] = '<p>#cache enabled, GET</p>';
$output = $this->renderer->renderRoot($element);
$this->assertSame($output, '<p>overridden</p>', 'Output is overridden.');
......@@ -129,7 +129,7 @@ public function testPostRenderCacheWithPostRequest() {
// POST request: #cache enabled, cache miss.
$element = $test_element;
$element['#cache'] = ['cid' => 'post_render_cache_test_POST'];
$element['#cache'] = ['keys' => ['post_render_cache_test_POST']];
$element['#markup'] = '<p>#cache enabled, POST</p>';
$output = $this->renderer->renderRoot($element);
$this->assertSame($output, '<p>overridden</p>', 'Output is overridden.');
......@@ -435,7 +435,7 @@ public function testPlaceholder() {
// GET request: #cache enabled, cache miss.
$this->setUpRequest();
$element = $test_element;
$element['#cache'] = ['cid' => 'render_cache_placeholder_test_GET'];
$element['#cache'] = ['keys' => ['render_cache_placeholder_test_GET']];
$output = $this->renderer->renderRoot($element);
$this->assertSame($output, $expected_output, 'Placeholder was replaced in output');
$this->assertTrue(isset($element['#printed']), 'No cache hit');
......@@ -475,7 +475,7 @@ public function testPlaceholder() {
// GET request: #cache enabled, cache hit.
$element = $test_element;
$element['#cache'] = ['cid' => 'render_cache_placeholder_test_GET'];
$element['#cache'] = ['keys' => ['render_cache_placeholder_test_GET']];
$output = $this->renderer->renderRoot($element);
$this->assertSame($output, $expected_output, 'Placeholder was replaced in output');
$this->assertFalse(isset($element['#printed']), 'Cache hit');
......@@ -533,8 +533,8 @@ public function testChildElementPlaceholder() {
// GET request: #cache enabled, cache miss.
$this->setUpRequest();
$element = $test_element;
$element['#cache'] = ['cid' => 'render_cache_placeholder_test_GET'];
$element['foo']['#cache'] = ['cid' => 'render_cache_placeholder_test_child_GET'];
$element['#cache'] = ['keys' => ['render_cache_placeholder_test_GET']];
$element['foo']['#cache'] = ['keys' => ['render_cache_placeholder_test_child_GET']];
// Render, which will use the common-test-render-element.html.twig template.
$output = $this->renderer->renderRoot($element);
$this->assertSame($output, $expected_output, 'Placeholder was replaced in output');
......@@ -635,7 +635,7 @@ public function testChildElementPlaceholder() {
// GET request: #cache enabled, cache hit.
$element = $test_element;
$element['#cache'] = ['cid' => 'render_cache_placeholder_test_GET'];
$element['#cache'] = ['keys' => ['render_cache_placeholder_test_GET']];
// Render, which will use the common-test-render-element.html.twig template.
$output = $this->renderer->renderRoot($element);
$this->assertSame($output, $expected_output, 'Placeholder was replaced in output');
......
......@@ -538,13 +538,13 @@ public function testRenderCache() {
// Create an empty element.
$test_element = [
'#cache' => [
'cid' => 'render_cache_test',
'keys' => ['render_cache_test'],
'tags' => ['render_cache_tag'],
],
'#markup' => '',
'child' => [
'#cache' => [
'cid' => 'render_cache_test_child',
'keys' => ['render_cache_test_child'],
'tags' => ['render_cache_tag_child:1', 'render_cache_tag_child:2'],
],
'#markup' => '',
......@@ -592,7 +592,7 @@ public function testRenderCacheMaxAge($max_age, $is_render_cached, $render_cache
$element = [
'#cache' => [
'cid' => 'render_cache_test',
'keys' => ['render_cache_test'],
'max-age' => $max_age,
],
'#markup' => '',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment