Commit fd3a981a authored by alexpott's avatar alexpott
Browse files

Issue #2458413 by Wim Leers: BlockViewBuilder should specify cache contexts...

Issue #2458413 by Wim Leers: BlockViewBuilder should specify cache contexts even for uncacheable blocks
parent b568a4d5
......@@ -36,6 +36,12 @@ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = N
* {@inheritdoc}
*/
public function viewMultiple(array $entities = array(), $view_mode = 'full', $langcode = NULL) {
// @todo Remove when https://www.drupal.org/node/2453059 lands.
$default_cache_contexts = [
'languages',
'theme',
];
/** @var \Drupal\block\BlockInterface[] $entities */
$build = array();
foreach ($entities as $entity) {
......@@ -63,21 +69,20 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
'#base_plugin_id' => $base_id,
'#derivative_plugin_id' => $derivative_id,
'#id' => $entity->id(),
'#cache' => [
'contexts' => Cache::mergeContexts($default_cache_contexts, $plugin->getCacheContexts()),
'tags' => Cache::mergeTags(
$this->getCacheTags(), // Block view builder cache tag.
$entity->getCacheTags(), // Block entity cache tag.
$plugin->getCacheTags() // Block plugin cache tags.
),
'max-age' => $plugin->getCacheMaxAge(),
],
// Add the entity so that it can be used in the #pre_render method.
'#block' => $entity,
);
$build[$entity_id]['#configuration']['label'] = String::checkPlain($configuration['label']);
// Set cache tags; these always need to be set, whether the block is
// cacheable or not, so that the page cache is correctly informed.
$build[$entity_id]['#cache']['tags'] = Cache::mergeTags(
$this->getCacheTags(), // Block view builder cache tag.
$entity->getCacheTags(), // Block entity cache tag.
$plugin->getCacheTags() // Block plugin cache tags.
);
$build[$entity_id]['#cache']['max-age'] = $plugin->getCacheMaxAge();
if ($plugin->isCacheable()) {
$build[$entity_id]['#pre_render'][] = array($this, 'buildBlock');
// Generic cache keys, with the block plugin's custom keys appended.
......@@ -86,14 +91,9 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
'block',
$entity->id(),
);
$default_cache_contexts = array(
'languages',
'theme',
);
$max_age = $plugin->getCacheMaxAge();
$build[$entity_id]['#cache'] += array(
'keys' => array_merge($default_cache_keys, $plugin->getCacheKeys()),
'contexts' => array_merge($default_cache_contexts, $plugin->getCacheContexts()),
'expire' => ($max_age === Cache::PERMANENT) ? Cache::PERMANENT : REQUEST_TIME + $max_age,
);
}
......
......@@ -151,7 +151,7 @@ protected function verifyRenderCacheHandling() {
// Test that entities with caching disabled do not generate a cache entry.
$build = $this->getBlockRenderArray();
$this->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == array('tags', 'max-age'), 'The render array element of uncacheable blocks is not cached, but does have cache tags & max-age set.');
$this->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == array('contexts', 'tags', 'max-age'), 'The render array element of uncacheable blocks is not cached, but does have cache contexts, tags & max-age set.');
// Enable block caching.
$this->setBlockCacheConfig(array(
......
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