core/modules/block/lib/Drupal/block/BlockViewBuilder.php | 10 ++++++++-- .../lib/Drupal/system/Plugin/Block/SystemMenuBlock.php | 13 +++++++++++++ .../system/Tests/Cache/PageCacheTagsIntegrationTest.php | 6 ++++++ .../Drupal/system/Tests/Entity/EntityViewBuilderTest.php | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/core/modules/block/lib/Drupal/block/BlockViewBuilder.php b/core/modules/block/lib/Drupal/block/BlockViewBuilder.php index 4539902..4ccfcf4 100644 --- a/core/modules/block/lib/Drupal/block/BlockViewBuilder.php +++ b/core/modules/block/lib/Drupal/block/BlockViewBuilder.php @@ -145,8 +145,14 @@ public function buildBlock($build) { return $build; } else { - // Abort rendering. - return array('#access' => FALSE, '#printed' => TRUE); + // Abort rendering: render as the empty string and ensure this block is + // render cached, so we can avoid the work of having to repeatedly + // determine whether the block is empty. E.g. modifying or adding entities + // could cause the block to no longer be empty. + return array( + '#markup' => '', + '#cache' => $build['#cache'], + ); } return $build; } diff --git a/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php b/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php index 904e0ad..c3b5463 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php +++ b/core/modules/system/lib/Drupal/system/Plugin/Block/SystemMenuBlock.php @@ -7,6 +7,7 @@ namespace Drupal\system\Plugin\Block; +use Drupal\Component\Utility\NestedArray; use Drupal\block\BlockBase; /** @@ -73,6 +74,18 @@ public function getCacheKeys() { } /** + * {@inheritdoc} + */ + public function getCacheTags() { + // Even when the menu block renders to the empty string for a user, we want + // the cache tag for this menu to be set: whenever the menu is changed, this + // menu block must also be re-rendered for that user, because maybe a menu + // link that is accessible for that user has been added. + $tags = array('menu' => array($this->getDerivativeId())); + return NestedArray::mergeDeep(parent::getCacheTags(), $tags); + } + + /** * Returns the cache contexts required for this block. * * Two cache contexts are required: cache by URL and by user's roles. diff --git a/core/modules/system/lib/Drupal/system/Tests/Cache/PageCacheTagsIntegrationTest.php b/core/modules/system/lib/Drupal/system/Tests/Cache/PageCacheTagsIntegrationTest.php index 9c2232a..cc584a3 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Cache/PageCacheTagsIntegrationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Cache/PageCacheTagsIntegrationTest.php @@ -79,10 +79,12 @@ function testPageCacheTags() { 'content:1', 'block_view:1', 'block:bartik_content', + 'block:bartik_tools', 'block:bartik_login', 'block:bartik_footer', 'block:bartik_powered', 'block_plugin:system_main_block', + 'block_plugin:system_menu_block__tools', 'block_plugin:user_login_block', 'block_plugin:system_menu_block__footer', 'block_plugin:system_powered_by_block', @@ -90,6 +92,7 @@ function testPageCacheTags() { 'node:' . $node_1->id(), 'user:' . $author_1->id(), 'filter_format:basic_html', + 'menu:tools', 'menu:footer', 'menu:main', )); @@ -99,11 +102,13 @@ function testPageCacheTags() { 'content:1', 'block_view:1', 'block:bartik_content', + 'block:bartik_tools', 'block:bartik_login', 'block:' . $block->id(), 'block:bartik_footer', 'block:bartik_powered', 'block_plugin:system_main_block', + 'block_plugin:system_menu_block__tools', 'block_plugin:user_login_block', 'block_plugin:views_block__comments_recent-block_1', 'block_plugin:system_menu_block__footer', @@ -112,6 +117,7 @@ function testPageCacheTags() { 'node:' . $node_2->id(), 'user:' . $author_2->id(), 'filter_format:full_html', + 'menu:tools', 'menu:footer', 'menu:main', )); diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityViewBuilderTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityViewBuilderTest.php index 2d0369b..0379aa7 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityViewBuilderTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityViewBuilderTest.php @@ -144,7 +144,7 @@ public function testEntityViewBuilderCacheToggling() { // Test a view mode in default conditions: render caching is enabled for // the entity type and the view mode. $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'full'); - $this->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == array('tags', 'keys', 'granularity', 'bin') , 'A view mode with render cache enabled has the correct output (cache tags, keys, granularity and bin).'); + $this->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == array('tags', 'keys', 'bin') , 'A view mode with render cache enabled has the correct output (cache tags, keys and bin).'); // Test that a view mode can opt out of render caching. $build = $this->container->get('entity.manager')->getViewBuilder('entity_test')->view($entity_test, 'test');