diff --git a/core/modules/layout_builder/src/SectionComponent.php b/core/modules/layout_builder/src/SectionComponent.php index 7af03af132b21f9b15074ffcb86ab392a2d3730d..6c08fb3aeb7df5a1bd0fe4d1a96ec856056f2dfe 100644 --- a/core/modules/layout_builder/src/SectionComponent.php +++ b/core/modules/layout_builder/src/SectionComponent.php @@ -3,6 +3,7 @@ namespace Drupal\layout_builder; use Drupal\Component\Plugin\Exception\PluginException; +use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Plugin\ContextAwarePluginInterface; @@ -103,11 +104,18 @@ public function toRenderArray(array $contexts = [], $in_preview = FALSE) { // @todo Figure out the best way to unify fields and blocks and components // in https://www.drupal.org/node/1875974. if ($plugin instanceof BlockPluginInterface) { - $access = $plugin->access($this->currentUser(), TRUE); - $cacheability = CacheableMetadata::createFromObject($access); + $cacheability = CacheableMetadata::createFromObject($plugin); - if ($in_preview || $access->isAllowed()) { - $cacheability->addCacheableDependency($plugin); + // Only check access if the component is not being previewed. + if ($in_preview) { + $access = AccessResult::allowed()->setCacheMaxAge(0); + } + else { + $access = $plugin->access($this->currentUser(), TRUE); + } + + $cacheability->addCacheableDependency($access); + if ($access->isAllowed()) { // @todo Move this to BlockBase in https://www.drupal.org/node/2931040. $output = [ '#theme' => 'block', diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php index 0437c1ca8538b198746a989ba22040d1763fde30..d5e0c7523c8905b8068ea3fa254b7828ba4df12b 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutSectionTest.php @@ -76,7 +76,6 @@ public function providerTestLayoutSectionFormatter() { ], [ 'foobar', - 'User context found', ], 'user', 'user:2', diff --git a/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php b/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php index 6dade754977d9b041a2520b332123f3192553b7b..ed7dfd202f9f58530128641e73e4f87cba5bdc42 100644 --- a/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php +++ b/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php @@ -136,6 +136,9 @@ public function testToRenderArrayAccessDenied() { $access_result = AccessResult::forbidden(); $block->access($this->account->reveal(), TRUE)->willReturn($access_result); $block->build()->shouldNotBeCalled(); + $block->getCacheContexts()->willReturn([]); + $block->getCacheTags()->willReturn([]); + $block->getCacheMaxAge()->willReturn(Cache::PERMANENT); $section = [ new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']), @@ -155,6 +158,50 @@ public function testToRenderArrayAccessDenied() { $this->assertEquals($expected, $result); } + /** + * @covers ::toRenderArray + */ + public function testToRenderArrayPreview() { + $block_content = ['#markup' => 'The block content.']; + $render_array = [ + '#theme' => 'block', + '#weight' => 0, + '#configuration' => [], + '#plugin_id' => 'block_plugin_id', + '#base_plugin_id' => 'block_plugin_id', + '#derivative_plugin_id' => NULL, + 'content' => $block_content, + '#cache' => [ + 'contexts' => [], + 'tags' => [], + 'max-age' => 0, + ], + ]; + $block = $this->prophesize(BlockPluginInterface::class); + $this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal()); + + $block->access($this->account->reveal(), TRUE)->shouldNotBeCalled(); + $block->build()->willReturn($block_content); + $block->getCacheContexts()->willReturn([]); + $block->getCacheTags()->willReturn([]); + $block->getCacheMaxAge()->willReturn(Cache::PERMANENT); + $block->getConfiguration()->willReturn([]); + $block->getPluginId()->willReturn('block_plugin_id'); + $block->getBaseId()->willReturn('block_plugin_id'); + $block->getDerivativeId()->willReturn(NULL); + + $section = [ + new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']), + ]; + $expected = [ + 'content' => [ + 'some_uuid' => $render_array, + ], + ]; + $result = (new Section('layout_onecol', [], $section))->toRenderArray([], TRUE); + $this->assertEquals($expected, $result); + } + /** * @covers ::toRenderArray */