Loading core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php +7 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ use Drupal\block_content\Access\RefinableDependentAccessInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Render\Element; use Drupal\Core\Render\PreviewFallbackInterface; use Drupal\Core\Session\AccountInterface; Loading Loading @@ -102,6 +103,12 @@ public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) { } $content = $block->build(); // We don't output the block render data if there are no render elements // found, but we want to capture the cache metadata from the block // regardless. $event->addCacheableDependency(CacheableMetadata::createFromRenderArray($content)); $is_content_empty = Element::isEmpty($content); $is_placeholder_ready = $event->inPreview() && $block instanceof PreviewFallbackInterface; // If the content is empty and no placeholder is available, return. Loading core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php +15 −4 Original line number Diff line number Diff line Loading @@ -99,7 +99,10 @@ public function testOnBuildRender($refinable_dependent_access) { $placeholder_label = 'Placeholder Label'; $block->getPreviewFallbackString()->willReturn($placeholder_label); $block_content = ['#markup' => 'The block content.']; $block_content = [ '#markup' => 'The block content.', '#cache' => ['tags' => ['build-tag']], ]; $block->build()->willReturn($block_content); $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal()); Loading @@ -122,7 +125,10 @@ public function testOnBuildRender($refinable_dependent_access) { $expected_cache = $expected_build + [ '#cache' => [ 'contexts' => [], 'tags' => ['test'], 'tags' => [ 'build-tag', 'test', ], 'max-age' => -1, ], ]; Loading Loading @@ -410,7 +416,9 @@ public function testOnBuildRenderEmptyBuild() { $block->getBaseId()->willReturn('block_plugin_id'); $block->getDerivativeId()->willReturn(NULL); $block->build()->willReturn([]); $block->build()->willReturn([ '#cache' => ['tags' => ['build-tag']], ]); $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal()); $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']); Loading @@ -423,7 +431,10 @@ public function testOnBuildRenderEmptyBuild() { $expected_cache = $expected_build + [ '#cache' => [ 'contexts' => [], 'tags' => ['test'], 'tags' => [ 'build-tag', 'test', ], 'max-age' => -1, ], ]; Loading Loading
core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php +7 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ use Drupal\block_content\Access\RefinableDependentAccessInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Render\Element; use Drupal\Core\Render\PreviewFallbackInterface; use Drupal\Core\Session\AccountInterface; Loading Loading @@ -102,6 +103,12 @@ public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) { } $content = $block->build(); // We don't output the block render data if there are no render elements // found, but we want to capture the cache metadata from the block // regardless. $event->addCacheableDependency(CacheableMetadata::createFromRenderArray($content)); $is_content_empty = Element::isEmpty($content); $is_placeholder_ready = $event->inPreview() && $block instanceof PreviewFallbackInterface; // If the content is empty and no placeholder is available, return. Loading
core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php +15 −4 Original line number Diff line number Diff line Loading @@ -99,7 +99,10 @@ public function testOnBuildRender($refinable_dependent_access) { $placeholder_label = 'Placeholder Label'; $block->getPreviewFallbackString()->willReturn($placeholder_label); $block_content = ['#markup' => 'The block content.']; $block_content = [ '#markup' => 'The block content.', '#cache' => ['tags' => ['build-tag']], ]; $block->build()->willReturn($block_content); $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal()); Loading @@ -122,7 +125,10 @@ public function testOnBuildRender($refinable_dependent_access) { $expected_cache = $expected_build + [ '#cache' => [ 'contexts' => [], 'tags' => ['test'], 'tags' => [ 'build-tag', 'test', ], 'max-age' => -1, ], ]; Loading Loading @@ -410,7 +416,9 @@ public function testOnBuildRenderEmptyBuild() { $block->getBaseId()->willReturn('block_plugin_id'); $block->getDerivativeId()->willReturn(NULL); $block->build()->willReturn([]); $block->build()->willReturn([ '#cache' => ['tags' => ['build-tag']], ]); $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal()); $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']); Loading @@ -423,7 +431,10 @@ public function testOnBuildRenderEmptyBuild() { $expected_cache = $expected_build + [ '#cache' => [ 'contexts' => [], 'tags' => ['test'], 'tags' => [ 'build-tag', 'test', ], 'max-age' => -1, ], ]; Loading