summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorAlex Pott2018-12-15 22:34:30 (GMT)
committerAlex Pott2018-12-15 22:34:37 (GMT)
commit79b59157c5e2e298b2a2014804e2994f12d56e4e (patch)
treeb33d7b0e0975d505d965cb63c22a5b9017f93f9a /core
parentc0465bbfc282a4c08d84eaa34d5939303b4be980 (diff)
Issue #3007439 by tim.plunkett, Wim Leers, xopoc: Layout builder renders Book navigation block on non-book pages
(cherry picked from commit 8bf6ee387055afaf8cd0a0bdd41eae1b7924a5eb)
Diffstat (limited to 'core')
-rw-r--r--core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php12
-rw-r--r--core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php5
-rw-r--r--core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php93
-rw-r--r--core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php5
4 files changed, 111 insertions, 4 deletions
diff --git a/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php b/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php
index 276e680..8b445bd 100644
--- a/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php
+++ b/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php
@@ -90,6 +90,14 @@ class BlockComponentRenderArray implements EventSubscriberInterface {
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 @@ class BlockComponentRenderArray implements EventSubscriberInterface {
'#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);
diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
index 5f7df45..be04c32 100644
--- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
+++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
@@ -317,6 +317,11 @@ class LayoutBuilderTest extends BrowserTestBase {
$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');
diff --git a/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php b/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php
index 3082084..2ac7994 100644
--- a/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php
+++ b/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php
@@ -11,6 +11,7 @@ use Drupal\Core\Cache\Cache;
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;
@@ -255,6 +256,98 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
/**
* @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
+ */
public function testOnBuildRenderNoBlock() {
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn(NULL);
diff --git a/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php b/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php
index 079a220..ad94e14 100644
--- a/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php
+++ b/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php
@@ -231,6 +231,7 @@ class SectionRenderTest extends UnitTestCase {
* @covers ::toRenderArray
*/
public function testContextAwareBlock() {
+ $block_content = ['#markup' => 'The block content.'];
$render_array = [
'#theme' => 'block',
'#weight' => 0,
@@ -238,7 +239,7 @@ class SectionRenderTest extends UnitTestCase {
'#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 @@ class SectionRenderTest extends UnitTestCase {
$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);