Unverified Commit 79b59157 authored by alexpott's avatar alexpott

Issue #3007439 by tim.plunkett, Wim Leers, xopoc: Layout builder renders Book...

Issue #3007439 by tim.plunkett, Wim Leers, xopoc: Layout builder renders Book navigation block on non-book pages

(cherry picked from commit 8bf6ee38)
parent c0465bbf
......@@ -90,6 +90,14 @@ public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
if ($access->isAllowed()) {
$event->addCacheableDependency($block);
$content = $block->build();
$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.
if ($is_content_empty && !$is_placeholder_ready) {
return;
}
$build = [
// @todo Move this to BlockBase in https://www.drupal.org/node/2931040.
'#theme' => 'block',
......@@ -98,9 +106,9 @@ public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
'#base_plugin_id' => $block->getBaseId(),
'#derivative_plugin_id' => $block->getDerivativeId(),
'#weight' => $event->getComponent()->getWeight(),
'content' => $block->build(),
'content' => $content,
];
if ($event->inPreview() && Element::isEmpty($build['content']) && $block instanceof PreviewFallbackInterface) {
if ($is_content_empty && $is_placeholder_ready) {
$build['content']['#markup'] = $block->getPreviewFallbackString();
}
$event->setBuild($build);
......
......@@ -317,6 +317,11 @@ public function testPluginDependencies() {
$page->fillField('id', 'myothermenu');
$page->pressButton('Save');
$page->clickLink('Add link');
$page->fillField('title[0][value]', 'My link');
$page->fillField('link[0][uri]', '/');
$page->pressButton('Save');
$this->drupalPostForm('admin/structure/types/manage/bundle_with_section_field/display', ['layout[enabled]' => TRUE], 'Save');
$assert_session->linkExists('Manage layout');
$this->clickLink('Manage layout');
......
......@@ -11,6 +11,7 @@
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Render\PreviewFallbackInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\layout_builder\Access\LayoutPreviewAccessAllowed;
use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
......@@ -252,6 +253,98 @@ public function testOnBuildRenderInPreview($refinable_dependent_access) {
$this->assertEquals($expected_cache, $result);
}
/**
* @covers ::onBuildRender
*/
public function testOnBuildRenderInPreviewEmptyBuild() {
$block = $this->prophesize(BlockPluginInterface::class)->willImplement(PreviewFallbackInterface::class);
$block->access($this->account->reveal(), TRUE)->shouldNotBeCalled();
$block->getCacheContexts()->willReturn([]);
$block->getCacheTags()->willReturn(['test']);
$block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$block->getConfiguration()->willReturn([]);
$block->getPluginId()->willReturn('block_plugin_id');
$block->getBaseId()->willReturn('block_plugin_id');
$block->getDerivativeId()->willReturn(NULL);
$placeholder_string = 'The placeholder string';
$block->getPreviewFallbackString()->willReturn($placeholder_string);
$block_content = [];
$block->build()->willReturn($block_content);
$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, [], TRUE);
$subscriber = new BlockComponentRenderArray($this->account->reveal());
$expected_build = [
'#theme' => 'block',
'#weight' => 0,
'#configuration' => [],
'#plugin_id' => 'block_plugin_id',
'#base_plugin_id' => 'block_plugin_id',
'#derivative_plugin_id' => NULL,
'content' => $block_content,
];
$expected_build['content']['#markup'] = $placeholder_string;
$expected_cache = $expected_build + [
'#cache' => [
'contexts' => [],
'tags' => ['test'],
'max-age' => 0,
],
];
$subscriber->onBuildRender($event);
$result = $event->getBuild();
$this->assertEquals($expected_build, $result);
$event->getCacheableMetadata()->applyTo($result);
$this->assertEquals($expected_cache, $result);
}
/**
* @covers ::onBuildRender
*/
public function testOnBuildRenderEmptyBuild() {
$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->getConfiguration()->willReturn([]);
$block->getPluginId()->willReturn('block_plugin_id');
$block->getBaseId()->willReturn('block_plugin_id');
$block->getDerivativeId()->willReturn(NULL);
$block->build()->willReturn([]);
$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());
$expected_build = [];
$expected_cache = $expected_build + [
'#cache' => [
'contexts' => [],
'tags' => ['test'],
'max-age' => -1,
],
];
$subscriber->onBuildRender($event);
$result = $event->getBuild();
$this->assertEquals($expected_build, $result);
$event->getCacheableMetadata()->applyTo($result);
$this->assertEquals($expected_cache, $result);
}
/**
* @covers ::onBuildRender
*/
......
......@@ -231,6 +231,7 @@ public function testToRenderArrayEmpty() {
* @covers ::toRenderArray
*/
public function testContextAwareBlock() {
$block_content = ['#markup' => 'The block content.'];
$render_array = [
'#theme' => 'block',
'#weight' => 0,
......@@ -238,7 +239,7 @@ public function testContextAwareBlock() {
'#plugin_id' => 'block_plugin_id',
'#base_plugin_id' => 'block_plugin_id',
'#derivative_plugin_id' => NULL,
'content' => [],
'content' => $block_content,
'#cache' => [
'contexts' => [],
'tags' => [],
......@@ -251,7 +252,7 @@ public function testContextAwareBlock() {
$access_result = AccessResult::allowed();
$block->access($this->account->reveal(), TRUE)->willReturn($access_result);
$block->build()->willReturn([]);
$block->build()->willReturn($block_content);
$block->getCacheContexts()->willReturn([]);
$block->getCacheTags()->willReturn([]);
$block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment