diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index cd5d3aeda57ccf6188fc9ba6f93cf7911b753e22..b8ce26a7b9b31bd255b8625393590d84b17b9888 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 0ab1298a7a132d65e4fcff3107d7c05317e54874..38223ab53cf60769739d39d991655324fec66216 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 e3baf8870c1688cc275ed2fe56c317776ff9fb2b..908c8313ac2ab13ae240b1688f4788702ed9fb46 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");