diff --git a/core/modules/layout_builder/src/Section.php b/core/modules/layout_builder/src/Section.php index 41fe13c57e..bf5c183c46 100644 --- a/core/modules/layout_builder/src/Section.php +++ b/core/modules/layout_builder/src/Section.php @@ -3,6 +3,7 @@ namespace Drupal\layout_builder; use Drupal\Core\Config\Entity\ThirdPartySettingsInterface; +use Drupal\Core\Entity\EntityInterface; /** * Provides a domain object for layout sections. @@ -88,7 +89,19 @@ public function toRenderArray(array $contexts = [], $in_preview = FALSE) { } } - return $this->getLayout()->build($regions); + $build = $this->getLayout()->build($regions); + + // Find the first non-global entity context value and add it to the build. + foreach ($contexts as $context_id => $context) { + if (strpos($context_id, '@') !== 0 && $context->hasContextValue()) { + $value = $context->getContextValue(); + if ($value instanceof EntityInterface) { + $build['#entity'] = $value; + break; + } + } + } + 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 bccf0236bb..a5f678d8f4 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 @@ -95,3 +95,15 @@ function layout_builder_entity_form_display_alter(EntityFormDisplayInterface $fo ]); } } + +/** + * Implements hook_preprocess_HOOK(). + */ +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()), + ]; + } +} diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php index daf54a4f78..7c22e781d3 100644 --- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php @@ -292,6 +292,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->drupalPostForm("$field_ui_prefix/display/default", ['layout[allow_custom]' => TRUE], 'Save');