diff --git a/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestEmptyBlock.php b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestEmptyBlock.php new file mode 100644 index 0000000000000000000000000000000000000000..a17e6268b0d0e010cf33e96a041fcec9943df4de --- /dev/null +++ b/core/modules/block/tests/modules/block_test/src/Plugin/Block/TestEmptyBlock.php @@ -0,0 +1,27 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\block_test\Plugin\Block; + +use Drupal\Core\Block\Attribute\Block; +use Drupal\Core\Block\BlockBase; +use Drupal\Core\StringTranslation\TranslatableMarkup; + +/** + * Provides a block that returns an empty array. + */ +#[Block( + id: "test_empty", + admin_label: new TranslatableMarkup("Test Empty block"), +)] +class TestEmptyBlock extends BlockBase { + + /** + * {@inheritdoc} + */ + public function build() { + return []; + } + +} diff --git a/core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php b/core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php index 8b2d518b12b1132264d1508ed2f3ff1451577ab6..d93e1f819eab13744fd830872e24d702fb1cb603 100644 --- a/core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php +++ b/core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php @@ -69,6 +69,27 @@ protected function setUp(): void { $this->renderer = $this->container->get('renderer'); } + /** + * Tests rendering a block plugin that returns an empty array. + */ + public function testEmptyRender(): void { + \Drupal::keyValue('block_test')->set('content', ''); + + $entity = $this->controller->create([ + 'id' => 'test_block1', + 'theme' => 'stark', + 'plugin' => 'test_empty', + ]); + $entity->save(); + + // Test the rendering of a block. + $entity = Block::load('test_block1'); + $builder = \Drupal::entityTypeManager()->getViewBuilder('block'); + $output = $builder->view($entity, 'block'); + $expected_output = ''; + $this->assertSame($expected_output, (string) $this->renderer->renderRoot($output)); + } + /** * Tests the rendering of blocks. */ diff --git a/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php b/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php index 7bedf79a5ca40936946d06bccccc5ff92288ee16..786fa3d786af6aa5ab5a6b2813f1a520ef1d6cdc 100644 --- a/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php +++ b/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php @@ -104,6 +104,10 @@ public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) { } $content = $block->build(); + // @todo Remove when https://www.drupal.org/node/3164389 is resolved. + if (!is_array($content)) { + throw new \UnexpectedValueException(sprintf('The block "%s" did not return an array', get_class($block))); + } // 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 diff --git a/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php b/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php index 464880254b10a4c79a4d4f49afd0ecaa10d5dc8a..530feea461dabfd491e83262b002b5e78bccd43b 100644 --- a/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php +++ b/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php @@ -499,6 +499,29 @@ public function testOnBuildRenderEmptyBuildWithCacheTags(): void { $this->assertEqualsCanonicalizing($expected_cache, $result); } + /** + * @covers ::onBuildRender + */ + public function testOnBuildRenderNullBuild(): void { + $block = $this->prophesize(BlockPluginInterface::class); + + $access_result = AccessResult::allowed(); + $block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled(); + $block->getCacheContexts()->willReturn([]); + $block->getCacheTags()->willReturn(['test']); + $block->getCacheMaxAge()->willReturn(Cache::PERMANENT); + + $block->build()->willReturn(NULL); + $this->expectException(\UnexpectedValueException::class); + $this->expectExceptionMessage(sprintf('The block "%s" did not return an array', get_class($block->reveal()))); + $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal()); + + $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']); + $event = new SectionComponentBuildRenderArrayEvent($component, [], FALSE); + $subscriber = new BlockComponentRenderArray($this->account->reveal()); + $subscriber->onBuildRender($event); + } + /** * @covers ::onBuildRender */