diff --git a/core/lib/Drupal/Core/Cache/AccountPermissionsCacheContext.php b/core/lib/Drupal/Core/Cache/AccountPermissionsCacheContext.php new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/core/modules/block/src/BlockViewBuilder.php b/core/modules/block/src/BlockViewBuilder.php index d527d6d2b8affec0af63bdadb8c10e6a9c1c178e..b90468661ea41ff36a53a83d1b64e5cd278a03f9 100644 --- a/core/modules/block/src/BlockViewBuilder.php +++ b/core/modules/block/src/BlockViewBuilder.php @@ -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, ); } diff --git a/core/modules/block/src/Tests/BlockViewBuilderTest.php b/core/modules/block/src/Tests/BlockViewBuilderTest.php index e0ee522e98f059f91c663cfbfedeb930004cf372..ff2f655a55c90f2b8fc6324f4913421022430530 100644 --- a/core/modules/block/src/Tests/BlockViewBuilderTest.php +++ b/core/modules/block/src/Tests/BlockViewBuilderTest.php @@ -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(