Verified Commit 921f2563 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3111192 by tim.plunkett, larowlan, mohit_aghera, mstrelan, lauriii,...

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

(cherry picked from commit 3f9bc84f)
parent 8671583c
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -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;
  }

  /**
+24 −0
Original line number Diff line number Diff line
@@ -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().
 */
+5 −0
Original line number Diff line number Diff line
@@ -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");