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

merge-requests/2817/merge
Alex Pott 2022-10-04 10:39:33 +01:00
parent 2e76fda4ff
commit 3f9bc84fe6
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
3 changed files with 36 additions and 1 deletions

View File

@ -4,6 +4,7 @@ namespace Drupal\layout_builder;
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 @@ class Section implements ThirdPartySettingsInterface {
$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;
}
/**

View File

@ -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().
*/

View File

@ -351,6 +351,7 @@ class LayoutBuilderTest extends BrowserTestBase {
$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 @@ class LayoutBuilderTest extends BrowserTestBase {
$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 @@ class LayoutBuilderTest extends BrowserTestBase {
$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 @@ class LayoutBuilderTest extends BrowserTestBase {
$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");