summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-03-24 23:50:56 +0000
committerAlex Pott2015-03-24 23:50:56 +0000
commitfd3a981af7a5258fd5377ddccb57d25cbe5314c3 (patch)
tree13edc05819737ecc7c9c96de785ecde6b16a6a20
parentb568a4d58af34f4f9c53b7769623b67cff4d2dec (diff)
Issue #2458413 by Wim Leers: BlockViewBuilder should specify cache contexts even for uncacheable blocks8.0.0-beta8
-rw-r--r--core/lib/Drupal/Core/Cache/AccountPermissionsCacheContext.php0
-rw-r--r--core/modules/block/src/BlockViewBuilder.php30
-rw-r--r--core/modules/block/src/Tests/BlockViewBuilderTest.php2
3 files changed, 16 insertions, 16 deletions
diff --git a/core/lib/Drupal/Core/Cache/AccountPermissionsCacheContext.php b/core/lib/Drupal/Core/Cache/AccountPermissionsCacheContext.php
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/core/lib/Drupal/Core/Cache/AccountPermissionsCacheContext.php
diff --git a/core/modules/block/src/BlockViewBuilder.php b/core/modules/block/src/BlockViewBuilder.php
index d527d6d..b904686 100644
--- a/core/modules/block/src/BlockViewBuilder.php
+++ b/core/modules/block/src/BlockViewBuilder.php
@@ -36,6 +36,12 @@ class BlockViewBuilder extends EntityViewBuilder {
* {@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 @@ class BlockViewBuilder extends EntityViewBuilder {
'#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 @@ class BlockViewBuilder extends EntityViewBuilder {
'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 e0ee522..ff2f655 100644
--- a/core/modules/block/src/Tests/BlockViewBuilderTest.php
+++ b/core/modules/block/src/Tests/BlockViewBuilderTest.php
@@ -151,7 +151,7 @@ class BlockViewBuilderTest extends KernelTestBase {
// 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(