Skip to content

Commit f438ecf

Browse files
committed
Issue #3007439 by tim.plunkett, Wim Leers, xopoc: Layout builder renders Book navigation block on non-book pages
(cherry picked from commit 8bf6ee3)
1 parent 4a2f2da commit f438ecf

File tree

4 files changed

+111
-4
lines changed

4 files changed

+111
-4
lines changed

modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
9090
if ($access->isAllowed()) {
9191
$event->addCacheableDependency($block);
9292

93+
$content = $block->build();
94+
$is_content_empty = Element::isEmpty($content);
95+
$is_placeholder_ready = $event->inPreview() && $block instanceof PreviewFallbackInterface;
96+
// If the content is empty and no placeholder is available, return.
97+
if ($is_content_empty && !$is_placeholder_ready) {
98+
return;
99+
}
100+
93101
$build = [
94102
// @todo Move this to BlockBase in https://www.drupal.org/node/2931040.
95103
'#theme' => 'block',
@@ -98,9 +106,9 @@ public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
98106
'#base_plugin_id' => $block->getBaseId(),
99107
'#derivative_plugin_id' => $block->getDerivativeId(),
100108
'#weight' => $event->getComponent()->getWeight(),
101-
'content' => $block->build(),
109+
'content' => $content,
102110
];
103-
if ($event->inPreview() && Element::isEmpty($build['content']) && $block instanceof PreviewFallbackInterface) {
111+
if ($is_content_empty && $is_placeholder_ready) {
104112
$build['content']['#markup'] = $block->getPreviewFallbackString();
105113
}
106114
$event->setBuild($build);

modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ public function testPluginDependencies() {
317317
$page->fillField('id', 'myothermenu');
318318
$page->pressButton('Save');
319319

320+
$page->clickLink('Add link');
321+
$page->fillField('title[0][value]', 'My link');
322+
$page->fillField('link[0][uri]', '/');
323+
$page->pressButton('Save');
324+
320325
$this->drupalPostForm('admin/structure/types/manage/bundle_with_section_field/display', ['layout[enabled]' => TRUE], 'Save');
321326
$assert_session->linkExists('Manage layout');
322327
$this->clickLink('Manage layout');

modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Drupal\Core\DependencyInjection\ContainerBuilder;
1212
use Drupal\Core\Entity\EntityInterface;
1313
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
14+
use Drupal\Core\Render\PreviewFallbackInterface;
1415
use Drupal\Core\Session\AccountInterface;
1516
use Drupal\layout_builder\Access\LayoutPreviewAccessAllowed;
1617
use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
@@ -252,6 +253,98 @@ public function testOnBuildRenderInPreview($refinable_dependent_access) {
252253
$this->assertEquals($expected_cache, $result);
253254
}
254255

256+
/**
257+
* @covers ::onBuildRender
258+
*/
259+
public function testOnBuildRenderInPreviewEmptyBuild() {
260+
$block = $this->prophesize(BlockPluginInterface::class)->willImplement(PreviewFallbackInterface::class);
261+
262+
$block->access($this->account->reveal(), TRUE)->shouldNotBeCalled();
263+
$block->getCacheContexts()->willReturn([]);
264+
$block->getCacheTags()->willReturn(['test']);
265+
$block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
266+
$block->getConfiguration()->willReturn([]);
267+
$block->getPluginId()->willReturn('block_plugin_id');
268+
$block->getBaseId()->willReturn('block_plugin_id');
269+
$block->getDerivativeId()->willReturn(NULL);
270+
$placeholder_string = 'The placeholder string';
271+
$block->getPreviewFallbackString()->willReturn($placeholder_string);
272+
273+
$block_content = [];
274+
$block->build()->willReturn($block_content);
275+
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
276+
277+
$component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
278+
$event = new SectionComponentBuildRenderArrayEvent($component, [], TRUE);
279+
280+
$subscriber = new BlockComponentRenderArray($this->account->reveal());
281+
282+
$expected_build = [
283+
'#theme' => 'block',
284+
'#weight' => 0,
285+
'#configuration' => [],
286+
'#plugin_id' => 'block_plugin_id',
287+
'#base_plugin_id' => 'block_plugin_id',
288+
'#derivative_plugin_id' => NULL,
289+
'content' => $block_content,
290+
];
291+
$expected_build['content']['#markup'] = $placeholder_string;
292+
293+
$expected_cache = $expected_build + [
294+
'#cache' => [
295+
'contexts' => [],
296+
'tags' => ['test'],
297+
'max-age' => 0,
298+
],
299+
];
300+
301+
$subscriber->onBuildRender($event);
302+
$result = $event->getBuild();
303+
$this->assertEquals($expected_build, $result);
304+
$event->getCacheableMetadata()->applyTo($result);
305+
$this->assertEquals($expected_cache, $result);
306+
}
307+
308+
/**
309+
* @covers ::onBuildRender
310+
*/
311+
public function testOnBuildRenderEmptyBuild() {
312+
$block = $this->prophesize(BlockPluginInterface::class);
313+
$access_result = AccessResult::allowed();
314+
$block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled();
315+
$block->getCacheContexts()->willReturn([]);
316+
$block->getCacheTags()->willReturn(['test']);
317+
$block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
318+
$block->getConfiguration()->willReturn([]);
319+
$block->getPluginId()->willReturn('block_plugin_id');
320+
$block->getBaseId()->willReturn('block_plugin_id');
321+
$block->getDerivativeId()->willReturn(NULL);
322+
323+
$block->build()->willReturn([]);
324+
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
325+
326+
$component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
327+
$event = new SectionComponentBuildRenderArrayEvent($component, [], FALSE);
328+
329+
$subscriber = new BlockComponentRenderArray($this->account->reveal());
330+
331+
$expected_build = [];
332+
333+
$expected_cache = $expected_build + [
334+
'#cache' => [
335+
'contexts' => [],
336+
'tags' => ['test'],
337+
'max-age' => -1,
338+
],
339+
];
340+
341+
$subscriber->onBuildRender($event);
342+
$result = $event->getBuild();
343+
$this->assertEquals($expected_build, $result);
344+
$event->getCacheableMetadata()->applyTo($result);
345+
$this->assertEquals($expected_cache, $result);
346+
}
347+
255348
/**
256349
* @covers ::onBuildRender
257350
*/

modules/layout_builder/tests/src/Unit/SectionRenderTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,15 @@ public function testToRenderArrayEmpty() {
231231
* @covers ::toRenderArray
232232
*/
233233
public function testContextAwareBlock() {
234+
$block_content = ['#markup' => 'The block content.'];
234235
$render_array = [
235236
'#theme' => 'block',
236237
'#weight' => 0,
237238
'#configuration' => [],
238239
'#plugin_id' => 'block_plugin_id',
239240
'#base_plugin_id' => 'block_plugin_id',
240241
'#derivative_plugin_id' => NULL,
241-
'content' => [],
242+
'content' => $block_content,
242243
'#cache' => [
243244
'contexts' => [],
244245
'tags' => [],
@@ -251,7 +252,7 @@ public function testContextAwareBlock() {
251252

252253
$access_result = AccessResult::allowed();
253254
$block->access($this->account->reveal(), TRUE)->willReturn($access_result);
254-
$block->build()->willReturn([]);
255+
$block->build()->willReturn($block_content);
255256
$block->getCacheContexts()->willReturn([]);
256257
$block->getCacheTags()->willReturn([]);
257258
$block->getCacheMaxAge()->willReturn(Cache::PERMANENT);

0 commit comments

Comments
 (0)