core/modules/block/src/BlockViewBuilder.php | 27 +++++++++++----------- .../src/Tests/PageCacheTagsIntegrationTest.php | 1 - .../Menu/DefaultMenuLinkTreeManipulatorsTest.php | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/core/modules/block/src/BlockViewBuilder.php b/core/modules/block/src/BlockViewBuilder.php index 80085ac..c718dc0 100644 --- a/core/modules/block/src/BlockViewBuilder.php +++ b/core/modules/block/src/BlockViewBuilder.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Entity\EntityViewBuilder; use Drupal\Core\Entity\EntityViewBuilderInterface; use Drupal\Core\Entity\EntityInterface; @@ -104,19 +105,7 @@ public function buildBlock($build) { // Remove the block entity from the render array, to ensure that blocks // can be rendered without the block config entity. unset($build['#block']); - if (!empty($content)) { - // If the content consists only of cacheability metadata, then don't - // render the block wrapper, but an empty string with that cacheability - // metadata. - // This allows blocks to be empty, yet still bubble cacheability metadata, - // to indicate *why* it is empty. - if (count($content) === 1 && array_keys($content) === ['#cache']) { - return [ - '#markup' => '', - '#cache' => $content['#cache'], - ]; - } - + if (!empty($content) && !(count($content) === 1 && array_keys($content) === ['#cache'])) { // Place the $content returned by the block plugin into a 'content' child // element, as a way to allow the plugin to have complete control of its // properties and rendering (e.g., its own #theme) without conflicting @@ -135,6 +124,8 @@ public function buildBlock($build) { } $build['content'] = $content; } + // Either the block's content is completely empty, or it consists only of + // cacheability metadata. else { // Abort rendering: render as the empty string and ensure this block is // render cached, so we can avoid the work of having to repeatedly @@ -143,7 +134,17 @@ public function buildBlock($build) { $build = array( '#markup' => '', '#cache' => $build['#cache'], + '#printed' => TRUE, ); + // If $content is not empty, then it contains cacheability metadata, and + // we must merge it with the existing cacheability metadata. This allows + // blocks to be empty, yet still bubble cacheability metadata, to indicate + // *why* they are empty. + if (!empty($content)) { + CacheableMetadata::createFromRenderArray($build) + ->merge(CacheableMetadata::createFromRenderArray($content)) + ->applyTo($build); + } } return $build; } diff --git a/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php b/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php index c6af0ac..dff09fe 100644 --- a/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php +++ b/core/modules/page_cache/src/Tests/PageCacheTagsIntegrationTest.php @@ -78,7 +78,6 @@ function testPageCacheTags() { 'theme', 'timezone', 'user.permissions', - 'user.roles', ]; // Full node page 1. diff --git a/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php b/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php index 83fd3eb..588dd87 100644 --- a/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php @@ -270,7 +270,7 @@ public function testCheckNodeAccess() { ->with('node') ->willReturn($query); - $node_access_result = AccessResult::allowed()->addCacheContexts(['user.node_grants:view']); + $node_access_result = AccessResult::allowed()->cachePerPermissions()->addCacheContexts(['user.node_grants:view']); $tree = $this->defaultMenuTreeManipulators->checkNodeAccess($tree); $this->assertEquals($node_access_result, $tree[1]->access);