diff --git a/core/modules/layout_builder/src/SectionComponent.php b/core/modules/layout_builder/src/SectionComponent.php index 7af03af132b..6c08fb3aeb7 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 @@ class SectionComponent { // @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 0437c1ca853..d5e0c7523c8 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 @@ class LayoutSectionTest extends BrowserTestBase { ], [ '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 6dade754977..ed7dfd202f9 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 @@ class SectionRenderTest extends UnitTestCase { $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 @@ class SectionRenderTest extends UnitTestCase { $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 */