From 3f9bc84fe60f44c1a753088bd0cbaf1be1079d3f Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Tue, 4 Oct 2022 10:39:33 +0100 Subject: [PATCH] Issue #3111192 by tim.plunkett, larowlan, mohit_aghera, mstrelan, lauriii, jibran: Themes have no context of the entity being rendered in preprocessing a layout when using Layout builder --- core/modules/layout_builder/src/Section.php | 8 ++++++- .../layout_builder_test.module | 24 +++++++++++++++++++ .../src/Functional/LayoutBuilderTest.php | 5 ++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index cd5d3aeda57c..b8ce26a7b9b3 100644 --- a/core/modules/layout_builder/src/Section.php +++ b/core/modules/layout_builder/src/Section.php @@ -4,6 +4,7 @@ use Drupal\Core\Config\Entity\ThirdPartySettingsInterface; use Drupal\Core\Plugin\PreviewAwarePluginInterface; +use Drupal\Core\Render\Element; /** * Provides a domain object for layout sections. @@ -94,7 +95,12 @@ public function toRenderArray(array $contexts = [], $in_preview = FALSE) { $layout->setInPreview($in_preview); } - return $layout->build($regions); + $build = $layout->build($regions); + // If an entity was used to build the layout, store it on the build. + if (!Element::isEmpty($build) && isset($contexts['layout_builder.entity'])) { + $build['#entity'] = $contexts['layout_builder.entity']->getContextValue(); + } + return $build; } /** diff --git a/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module b/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module index 0ab1298a7a13..38223ab53cf6 100644 --- a/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module +++ b/core/modules/layout_builder/tests/modules/layout_builder_test/layout_builder_test.module @@ -111,6 +111,30 @@ function layout_builder_entity_form_display_alter(EntityFormDisplayInterface $fo } } +/** + * Implements hook_preprocess_HOOK() for one-column layout template. + */ +function layout_builder_test_preprocess_layout__onecol(&$vars) { + if (!empty($vars['content']['#entity'])) { + $vars['content']['content'][\Drupal::service('uuid')->generate()] = [ + '#type' => 'markup', + '#markup' => sprintf('Yes, I can access the %s', $vars['content']['#entity']->label()), + ]; + } +} + +/** + * Implements hook_preprocess_HOOK() for two-column layout template. + */ +function layout_builder_test_preprocess_layout__twocol_section(&$vars) { + if (!empty($vars['content']['#entity'])) { + $vars['content']['first'][\Drupal::service('uuid')->generate()] = [ + '#type' => 'markup', + '#markup' => sprintf('Yes, I can access the entity %s in two column', $vars['content']['#entity']->label()), + ]; + } +} + /** * Implements hook_system_breadcrumb_alter(). */ diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php index e3baf8870c16..908c8313ac2a 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php @@ -351,6 +351,7 @@ public function testLayoutBuilderUi() { $assert_session->pageTextContains('Extra, Extra read all about it.'); $assert_session->pageTextNotContains('Placeholder for the "Extra label" field'); $assert_session->linkNotExists('Layout'); + $assert_session->pageTextContains(sprintf('Yes, I can access the %s', Node::load(1)->label())); // Enable overrides. $this->drupalGet("{$field_ui_prefix}/display/default"); @@ -377,6 +378,7 @@ public function testLayoutBuilderUi() { $assert_session->pageTextNotContains('Powered by Drupal'); $assert_session->pageTextNotContains('Extra, Extra read all about it.'); $assert_session->pageTextNotContains('Placeholder for the "Extra label" field'); + $assert_session->pageTextContains(sprintf('Yes, I can access the entity %s in two column', Node::load(1)->label())); // Assert that overrides cannot be turned off while overrides exist. $this->drupalGet("$field_ui_prefix/display/default"); @@ -401,6 +403,7 @@ public function testLayoutBuilderUi() { $assert_session->pageTextContains('Powered by Drupal'); $assert_session->pageTextContains('Extra, Extra read all about it.'); $assert_session->pageTextNotContains('Placeholder for the "Extra label" field'); + $assert_session->pageTextContains(sprintf('Yes, I can access the %s', Node::load(2)->label())); // The overridden node does not pick up the changes to defaults. $this->drupalGet('node/1'); @@ -431,6 +434,8 @@ public function testLayoutBuilderUi() { $assert_session->pageTextContains('The first node body'); $assert_session->pageTextContains('Powered by Drupal'); $assert_session->pageTextContains('Extra, Extra read all about it.'); + $assert_session->pageTextNotContains(sprintf('Yes, I can access the entity %s in two column', Node::load(1)->label())); + $assert_session->pageTextContains(sprintf('Yes, I can access the %s', Node::load(1)->label())); // Assert that overrides can be turned off now that all overrides are gone. $this->drupalGet("{$field_ui_prefix}/display/default"); -- GitLab