From 6483b2997accdc3618c68b55924360e6b84615b2 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 3 Oct 2022 11:44:57 +0100
Subject: [PATCH] Issue #3273317 by clayfreeman, larowlan, tim.plunkett,
 danflanagan8: Add 'in_preview' variable to block and layout templates

---
 core/lib/Drupal/Core/Layout/LayoutDefault.php |  1 +
 core/modules/block/block.module               |  1 +
 core/modules/block/templates/block.html.twig  |  1 +
 .../tests/src/Unit/FieldLayoutBuilderTest.php |  2 ++
 .../layout--fourcol-section.html.twig         |  1 +
 .../layout--threecol-section.html.twig        |  1 +
 .../layout--twocol-section.html.twig          |  1 +
 .../BlockComponentRenderArray.php             |  1 +
 .../layout_builder_test.module                | 11 +++++++
 .../block--preview-aware-block.html.twig      |  5 +++
 .../src/Functional/LayoutBuilderTest.php      | 32 +++++++++++++++++++
 .../Unit/BlockComponentRenderArrayTest.php    |  6 ++++
 .../tests/src/Unit/SectionRenderTest.php      |  3 ++
 .../layout_discovery/layout_discovery.module  |  3 +-
 .../layouts/onecol/layout--onecol.html.twig   |  1 +
 .../layout--threecol-25-50-25.html.twig       |  1 +
 .../layout--threecol-33-34-33.html.twig       |  1 +
 .../layouts/twocol/layout--twocol.html.twig   |  1 +
 .../layout--twocol-bricks.html.twig           |  1 +
 .../block--system-menu-block.html.twig        |  1 +
 .../templates/layout-test-1col.html.twig      |  3 ++
 .../layout--oneplusfourgrid-section.html.twig |  1 +
 .../block/block--system-menu-block.html.twig  |  1 +
 .../templates/classy/block/block.html.twig    |  1 +
 .../block--bundle--banner-block.html.twig     |  1 +
 ...lock--bundle--footer-promo-block.html.twig |  1 +
 .../help-block/block--help.html.twig          |  1 +
 .../block--umami-main-menu.html.twig          |  1 +
 .../search/block--search-form-block.html.twig |  1 +
 .../Tests/Core/Layout/LayoutDefaultTest.php   |  1 +
 .../block/block--search-form-block.html.twig  |  1 +
 .../block/block--system-menu-block.html.twig  |  1 +
 .../templates/classy/block/block.html.twig    |  1 +
 .../block/block--page-title-block.html.twig   |  1 +
 ...nu--plugin-id--search-form-block.html.twig |  1 +
 .../block/block--secondary-menu.html.twig     |  1 +
 .../block/block--system-menu-block.html.twig  |  1 +
 .../olivero/templates/block/block.html.twig   |  1 +
 .../layout--fourcol-section.html.twig         |  1 +
 .../layout--threecol-section.html.twig        |  1 +
 .../layout--twocol-section.html.twig          |  1 +
 .../onecol/layout--onecol.html.twig           |  1 +
 .../layout--threecol-25-50-25.html.twig       |  1 +
 .../layout--threecol-33-34-33.html.twig       |  1 +
 .../twocol/layout--twocol.html.twig           |  1 +
 .../layout--twocol-bricks.html.twig           |  1 +
 .../block/block--system-menu-block.html.twig  |  1 +
 .../stable9/templates/block/block.html.twig   |  1 +
 .../block/block--search-form-block.html.twig  |  1 +
 .../block/block--system-menu-block.html.twig  |  1 +
 .../templates/block/block.html.twig           |  1 +
 51 files changed, 107 insertions(+), 1 deletion(-)
 create mode 100644 core/modules/layout_builder/tests/modules/layout_builder_test/templates/block--preview-aware-block.html.twig

diff --git a/core/lib/Drupal/Core/Layout/LayoutDefault.php b/core/lib/Drupal/Core/Layout/LayoutDefault.php
index 607b34a48cd9..bd3780987a4d 100644
--- a/core/lib/Drupal/Core/Layout/LayoutDefault.php
+++ b/core/lib/Drupal/Core/Layout/LayoutDefault.php
@@ -51,6 +51,7 @@ public function build(array $regions) {
         $build[$region_name] = $regions[$region_name];
       }
     }
+    $build['#in_preview'] = $this->inPreview;
     $build['#settings'] = $this->getConfiguration();
     $build['#layout'] = $this->pluginDefinition;
     $build['#theme'] = $this->pluginDefinition->getThemeHook();
diff --git a/core/modules/block/block.module b/core/modules/block/block.module
index 062a6fa9c7d3..11bb4492b57d 100644
--- a/core/modules/block/block.module
+++ b/core/modules/block/block.module
@@ -246,6 +246,7 @@ function template_preprocess_block(&$variables) {
   $variables['plugin_id'] = $variables['elements']['#plugin_id'];
   $variables['base_plugin_id'] = $variables['elements']['#base_plugin_id'];
   $variables['derivative_plugin_id'] = $variables['elements']['#derivative_plugin_id'];
+  $variables['in_preview'] = $variables['elements']['#in_preview'] ?? FALSE;
   $variables['label'] = !empty($variables['configuration']['label_display']) ? $variables['configuration']['label'] : '';
   $variables['content'] = $variables['elements']['content'];
   // A block's label is configuration: it is static. Allow dynamic labels to be
diff --git a/core/modules/block/templates/block.html.twig b/core/modules/block/templates/block.html.twig
index d880475255d0..8fc22725d5fb 100644
--- a/core/modules/block/templates/block.html.twig
+++ b/core/modules/block/templates/block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: array of HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template.
diff --git a/core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php b/core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php
index 8b074d5a071d..431b8c1d35e8 100644
--- a/core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php
+++ b/core/modules/field_layout/tests/src/Unit/FieldLayoutBuilderTest.php
@@ -143,6 +143,7 @@ public function testBuildView() {
             '#markup' => 'Test1',
           ],
         ],
+        '#in_preview' => FALSE,
         '#settings' => [
           'label' => '',
         ],
@@ -243,6 +244,7 @@ public function testBuildForm() {
           '#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
           '#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
         ],
+        '#in_preview' => FALSE,
         '#settings' => [
           'label' => '',
         ],
diff --git a/core/modules/layout_builder/layouts/fourcol_section/layout--fourcol-section.html.twig b/core/modules/layout_builder/layouts/fourcol_section/layout--fourcol-section.html.twig
index 9a380f76e01d..1f08a2ab551c 100644
--- a/core/modules/layout_builder/layouts/fourcol_section/layout--fourcol-section.html.twig
+++ b/core/modules/layout_builder/layouts/fourcol_section/layout--fourcol-section.html.twig
@@ -4,6 +4,7 @@
  * Default theme implementation for a four-column 25%-25%-25%-25% layout.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  *
diff --git a/core/modules/layout_builder/layouts/threecol_section/layout--threecol-section.html.twig b/core/modules/layout_builder/layouts/threecol_section/layout--threecol-section.html.twig
index 1311f28ab2a1..204a43621dc5 100644
--- a/core/modules/layout_builder/layouts/threecol_section/layout--threecol-section.html.twig
+++ b/core/modules/layout_builder/layouts/threecol_section/layout--threecol-section.html.twig
@@ -4,6 +4,7 @@
  * Default theme implementation for a three-column layout.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  *
diff --git a/core/modules/layout_builder/layouts/twocol_section/layout--twocol-section.html.twig b/core/modules/layout_builder/layouts/twocol_section/layout--twocol-section.html.twig
index a5d1d36376b7..9969f8e57a1d 100644
--- a/core/modules/layout_builder/layouts/twocol_section/layout--twocol-section.html.twig
+++ b/core/modules/layout_builder/layouts/twocol_section/layout--twocol-section.html.twig
@@ -4,6 +4,7 @@
  * Default theme implementation to display a two-column layout.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  *
diff --git a/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php b/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php
index 083de3983d73..7bedf79a5ca4 100644
--- a/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php
+++ b/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php
@@ -124,6 +124,7 @@ public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
         '#plugin_id' => $block->getPluginId(),
         '#base_plugin_id' => $block->getBaseId(),
         '#derivative_plugin_id' => $block->getDerivativeId(),
+        '#in_preview' => $event->inPreview(),
         '#weight' => $event->getComponent()->getWeight(),
       ];
 
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 cd6070f53ff3..0ab1298a7a13 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
@@ -131,3 +131,14 @@ function layout_builder_test_module_implements_alter(&$implementations, $hook) {
     ] + $implementations;
   }
 }
+
+/**
+ * Implements hook_theme().
+ */
+function layout_builder_test_theme() {
+  return [
+    'block__preview_aware_block' => [
+      'base hook' => 'block',
+    ],
+  ];
+}
diff --git a/core/modules/layout_builder/tests/modules/layout_builder_test/templates/block--preview-aware-block.html.twig b/core/modules/layout_builder/tests/modules/layout_builder_test/templates/block--preview-aware-block.html.twig
new file mode 100644
index 000000000000..fe9ca8e1e4d0
--- /dev/null
+++ b/core/modules/layout_builder/tests/modules/layout_builder_test/templates/block--preview-aware-block.html.twig
@@ -0,0 +1,5 @@
+{% if in_preview %}
+  The block template is being previewed.
+{% endif %}
+
+{% include '@block/block.html.twig' %}
diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
index 24c88e181866..e3baf8870c16 100644
--- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
+++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
@@ -719,15 +719,47 @@ public function testPreviewAwarePlugins() {
     $page->pressButton('Add block');
 
     $assert_session->elementExists('css', '.go-birds-preview');
+    $assert_session->pageTextContains('The block template is being previewed.');
     $assert_session->pageTextContains('This block is being rendered in preview mode.');
 
     $page->pressButton('Save layout');
     $this->drupalGet('node/1');
 
     $assert_session->elementNotExists('css', '.go-birds-preview');
+    $assert_session->pageTextNotContains('The block template is being previewed.');
     $assert_session->pageTextContains('This block is being rendered normally.');
   }
 
+  /**
+   * Tests preview-aware templates.
+   */
+  public function testPreviewAwareTemplates() {
+    $assert_session = $this->assertSession();
+    $page = $this->getSession()->getPage();
+
+    $this->drupalLogin($this->drupalCreateUser([
+      'configure any layout',
+      'administer node display',
+    ]));
+
+    $this->drupalGet('admin/structure/types/manage/bundle_with_section_field/display/default');
+    $this->submitForm(['layout[enabled]' => TRUE], 'Save');
+    $page->clickLink('Manage layout');
+    $page->clickLink('Add section');
+    $page->clickLink('1 column layout');
+    $page->pressButton('Add section');
+    $page->clickLink('Add block');
+    $page->clickLink('Preview-aware block');
+    $page->pressButton('Add block');
+
+    $assert_session->pageTextContains('This is a preview, indeed');
+
+    $page->pressButton('Save layout');
+    $this->drupalGet('node/1');
+
+    $assert_session->pageTextNotContains('This is a preview, indeed');
+  }
+
   /**
    * Tests the interaction between full and default view modes.
    *
diff --git a/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php b/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php
index 883d3c7d2060..9d797038db2b 100644
--- a/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php
+++ b/core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php
@@ -120,6 +120,7 @@ public function testOnBuildRender($refinable_dependent_access) {
       '#base_plugin_id' => 'block_plugin_id',
       '#derivative_plugin_id' => NULL,
       'content' => $block_content,
+      '#in_preview' => FALSE,
     ];
 
     $expected_build_with_expected_cache = $expected_build + [
@@ -195,6 +196,7 @@ public function testOnBuildRenderWithoutPreviewFallbackString($refinable_depende
       '#base_plugin_id' => 'block_plugin_id',
       '#derivative_plugin_id' => NULL,
       'content' => $block_content,
+      '#in_preview' => FALSE,
     ];
 
     $expected_cache = $expected_build + [
@@ -324,6 +326,7 @@ public function testOnBuildRenderInPreview($refinable_dependent_access) {
       '#attributes' => [
         'data-layout-content-preview-placeholder-label' => $placeholder_label,
       ],
+      '#in_preview' => TRUE,
     ];
 
     $expected_cache = $expected_build + [
@@ -332,6 +335,7 @@ public function testOnBuildRenderInPreview($refinable_dependent_access) {
         'tags' => ['test'],
         'max-age' => 0,
       ],
+      '#in_preview' => TRUE,
     ];
 
     $subscriber->onBuildRender($event);
@@ -383,6 +387,7 @@ public function testOnBuildRenderInPreviewEmptyBuild() {
       '#attributes' => [
         'data-layout-content-preview-placeholder-label' => $placeholder_string,
       ],
+      '#in_preview' => TRUE,
     ];
     $expected_build['content']['#markup'] = $placeholder_string;
 
@@ -392,6 +397,7 @@ public function testOnBuildRenderInPreviewEmptyBuild() {
         'tags' => ['test'],
         'max-age' => 0,
       ],
+      '#in_preview' => TRUE,
     ];
 
     $subscriber->onBuildRender($event);
diff --git a/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php b/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php
index fe9fbdc35c21..a149ff846753 100644
--- a/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php
+++ b/core/modules/layout_builder/tests/src/Unit/SectionRenderTest.php
@@ -115,6 +115,7 @@ public function testToRenderArray() {
         'tags' => [],
         'max-age' => -1,
       ],
+      '#in_preview' => FALSE,
     ];
 
     $block = $this->prophesize(BlockPluginInterface::class)->willImplement(PreviewFallbackInterface::class);
@@ -198,6 +199,7 @@ public function testToRenderArrayPreview() {
         'tags' => [],
         'max-age' => 0,
       ],
+      '#in_preview' => TRUE,
     ];
     $block = $this->prophesize(BlockPluginInterface::class)->willImplement(PreviewFallbackInterface::class);
     $this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());
@@ -254,6 +256,7 @@ public function testContextAwareBlock() {
         'tags' => [],
         'max-age' => -1,
       ],
+      '#in_preview' => FALSE,
     ];
 
     $block = $this->prophesize(BlockPluginInterface::class)
diff --git a/core/modules/layout_discovery/layout_discovery.module b/core/modules/layout_discovery/layout_discovery.module
index ce08617e025d..efc9d3b30baf 100644
--- a/core/modules/layout_discovery/layout_discovery.module
+++ b/core/modules/layout_discovery/layout_discovery.module
@@ -34,11 +34,12 @@ function layout_discovery_theme() {
  * @param array &$variables
  *   An associative array containing:
  *   - content: An associative array containing the properties of the element.
- *     Properties used: #settings, #layout.
+ *     Properties used: #settings, #layout, #in_preview.
  */
 function template_preprocess_layout(&$variables) {
   $variables['settings'] = $variables['content']['#settings'] ?? [];
   $variables['layout'] = $variables['content']['#layout'] ?? [];
+  $variables['in_preview'] = $variables['content']['#in_preview'] ?? FALSE;
 
   // Create an attributes variable for each region.
   foreach (Element::children($variables['content']) as $name) {
diff --git a/core/modules/layout_discovery/layouts/onecol/layout--onecol.html.twig b/core/modules/layout_discovery/layouts/onecol/layout--onecol.html.twig
index 3a7f9934d099..01d61e009fd9 100644
--- a/core/modules/layout_discovery/layouts/onecol/layout--onecol.html.twig
+++ b/core/modules/layout_discovery/layouts/onecol/layout--onecol.html.twig
@@ -4,6 +4,7 @@
  * Default theme implementation to display a one-column layout.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  *
diff --git a/core/modules/layout_discovery/layouts/threecol_25_50_25/layout--threecol-25-50-25.html.twig b/core/modules/layout_discovery/layouts/threecol_25_50_25/layout--threecol-25-50-25.html.twig
index e5441d8b6be9..cd226f020190 100644
--- a/core/modules/layout_discovery/layouts/threecol_25_50_25/layout--threecol-25-50-25.html.twig
+++ b/core/modules/layout_discovery/layouts/threecol_25_50_25/layout--threecol-25-50-25.html.twig
@@ -7,6 +7,7 @@
  * additional areas for the top and the bottom.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  *
diff --git a/core/modules/layout_discovery/layouts/threecol_33_34_33/layout--threecol-33-34-33.html.twig b/core/modules/layout_discovery/layouts/threecol_33_34_33/layout--threecol-33-34-33.html.twig
index 6445061c04e4..762cece8cf79 100644
--- a/core/modules/layout_discovery/layouts/threecol_33_34_33/layout--threecol-33-34-33.html.twig
+++ b/core/modules/layout_discovery/layouts/threecol_33_34_33/layout--threecol-33-34-33.html.twig
@@ -7,6 +7,7 @@
  * additional areas for the top and the bottom.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  *
diff --git a/core/modules/layout_discovery/layouts/twocol/layout--twocol.html.twig b/core/modules/layout_discovery/layouts/twocol/layout--twocol.html.twig
index 262c657f919e..11939907bafd 100644
--- a/core/modules/layout_discovery/layouts/twocol/layout--twocol.html.twig
+++ b/core/modules/layout_discovery/layouts/twocol/layout--twocol.html.twig
@@ -4,6 +4,7 @@
  * Default theme implementation to display a two-column layout.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  *
diff --git a/core/modules/layout_discovery/layouts/twocol_bricks/layout--twocol-bricks.html.twig b/core/modules/layout_discovery/layouts/twocol_bricks/layout--twocol-bricks.html.twig
index dc29e03e4302..f772834f5057 100644
--- a/core/modules/layout_discovery/layouts/twocol_bricks/layout--twocol-bricks.html.twig
+++ b/core/modules/layout_discovery/layouts/twocol_bricks/layout--twocol-bricks.html.twig
@@ -7,6 +7,7 @@
  * the top, bottom and in the middle.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  *
diff --git a/core/modules/system/templates/block--system-menu-block.html.twig b/core/modules/system/templates/block--system-menu-block.html.twig
index 6113bc2ed8f6..1e6c3e020e1b 100644
--- a/core/modules/system/templates/block--system-menu-block.html.twig
+++ b/core/modules/system/templates/block--system-menu-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: HTML attributes for the containing element.
  *   - id: A valid HTML ID and guaranteed unique.
diff --git a/core/modules/system/tests/modules/layout_test/templates/layout-test-1col.html.twig b/core/modules/system/tests/modules/layout_test/templates/layout-test-1col.html.twig
index 2207e3c71ab1..ab03efabc94b 100644
--- a/core/modules/system/tests/modules/layout_test/templates/layout-test-1col.html.twig
+++ b/core/modules/system/tests/modules/layout_test/templates/layout-test-1col.html.twig
@@ -5,6 +5,9 @@
  */
 #}
 <div{{ attributes.addClass('layout-example-1col', 'clearfix') }}>
+  {% if in_preview %}
+    This is a preview, indeed
+  {% endif %}
   <div {{ region_attributes.top.addClass('region-top') }}>
     {{ content.top }}
   </div>
diff --git a/core/profiles/demo_umami/themes/umami/layouts/oneplusfourgrid_section/layout--oneplusfourgrid-section.html.twig b/core/profiles/demo_umami/themes/umami/layouts/oneplusfourgrid_section/layout--oneplusfourgrid-section.html.twig
index 5345bcae80bb..40fc3e770581 100644
--- a/core/profiles/demo_umami/themes/umami/layouts/oneplusfourgrid_section/layout--oneplusfourgrid-section.html.twig
+++ b/core/profiles/demo_umami/themes/umami/layouts/oneplusfourgrid_section/layout--oneplusfourgrid-section.html.twig
@@ -4,6 +4,7 @@
  * Default theme implementation to display a one plus four grid layout.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  *
diff --git a/core/profiles/demo_umami/themes/umami/templates/classy/block/block--system-menu-block.html.twig b/core/profiles/demo_umami/themes/umami/templates/classy/block/block--system-menu-block.html.twig
index 407f8403fd28..db3f9f808815 100644
--- a/core/profiles/demo_umami/themes/umami/templates/classy/block/block--system-menu-block.html.twig
+++ b/core/profiles/demo_umami/themes/umami/templates/classy/block/block--system-menu-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: HTML attributes for the containing element.
  *   - id: A valid HTML ID and guaranteed unique.
diff --git a/core/profiles/demo_umami/themes/umami/templates/classy/block/block.html.twig b/core/profiles/demo_umami/themes/umami/templates/classy/block/block.html.twig
index fd3311be958b..114d7c4de4f8 100644
--- a/core/profiles/demo_umami/themes/umami/templates/classy/block/block.html.twig
+++ b/core/profiles/demo_umami/themes/umami/templates/classy/block/block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: array of HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template.
diff --git a/core/profiles/demo_umami/themes/umami/templates/components/banner-block/block--bundle--banner-block.html.twig b/core/profiles/demo_umami/themes/umami/templates/components/banner-block/block--bundle--banner-block.html.twig
index 9b13cd68443d..d23cc297bf71 100644
--- a/core/profiles/demo_umami/themes/umami/templates/components/banner-block/block--bundle--banner-block.html.twig
+++ b/core/profiles/demo_umami/themes/umami/templates/components/banner-block/block--bundle--banner-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: array of HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template.
diff --git a/core/profiles/demo_umami/themes/umami/templates/components/footer-promo-block/block--bundle--footer-promo-block.html.twig b/core/profiles/demo_umami/themes/umami/templates/components/footer-promo-block/block--bundle--footer-promo-block.html.twig
index e90416b9cc0d..489dd0c2e247 100644
--- a/core/profiles/demo_umami/themes/umami/templates/components/footer-promo-block/block--bundle--footer-promo-block.html.twig
+++ b/core/profiles/demo_umami/themes/umami/templates/components/footer-promo-block/block--bundle--footer-promo-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: array of HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template.
diff --git a/core/profiles/demo_umami/themes/umami/templates/components/help-block/block--help.html.twig b/core/profiles/demo_umami/themes/umami/templates/components/help-block/block--help.html.twig
index 20e2a4a9aa84..a924817e48b7 100644
--- a/core/profiles/demo_umami/themes/umami/templates/components/help-block/block--help.html.twig
+++ b/core/profiles/demo_umami/themes/umami/templates/components/help-block/block--help.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: array of HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template.
diff --git a/core/profiles/demo_umami/themes/umami/templates/components/navigation/block--umami-main-menu.html.twig b/core/profiles/demo_umami/themes/umami/templates/components/navigation/block--umami-main-menu.html.twig
index 51174d43c98d..ad21956c338c 100644
--- a/core/profiles/demo_umami/themes/umami/templates/components/navigation/block--umami-main-menu.html.twig
+++ b/core/profiles/demo_umami/themes/umami/templates/components/navigation/block--umami-main-menu.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: HTML attributes for the containing element.
  *   - id: A valid HTML ID and guaranteed unique.
diff --git a/core/profiles/demo_umami/themes/umami/templates/components/search/block--search-form-block.html.twig b/core/profiles/demo_umami/themes/umami/templates/components/search/block--search-form-block.html.twig
index 01599d5f7e75..29e828c12e59 100644
--- a/core/profiles/demo_umami/themes/umami/templates/components/search/block--search-form-block.html.twig
+++ b/core/profiles/demo_umami/themes/umami/templates/components/search/block--search-form-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: A list HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template. Includes:
diff --git a/core/tests/Drupal/Tests/Core/Layout/LayoutDefaultTest.php b/core/tests/Drupal/Tests/Core/Layout/LayoutDefaultTest.php
index af9cc38759bb..30b147100576 100644
--- a/core/tests/Drupal/Tests/Core/Layout/LayoutDefaultTest.php
+++ b/core/tests/Drupal/Tests/Core/Layout/LayoutDefaultTest.php
@@ -30,6 +30,7 @@ public function testBuild($regions, $expected) {
       ],
     ]);
     $expected += [
+      '#in_preview' => FALSE,
       '#settings' => [
         'label' => '',
       ],
diff --git a/core/themes/claro/templates/classy/block/block--search-form-block.html.twig b/core/themes/claro/templates/classy/block/block--search-form-block.html.twig
index 667202fb6b65..d1cda724ba87 100644
--- a/core/themes/claro/templates/classy/block/block--search-form-block.html.twig
+++ b/core/themes/claro/templates/classy/block/block--search-form-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: A list HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template. Includes:
diff --git a/core/themes/claro/templates/classy/block/block--system-menu-block.html.twig b/core/themes/claro/templates/classy/block/block--system-menu-block.html.twig
index 407f8403fd28..db3f9f808815 100644
--- a/core/themes/claro/templates/classy/block/block--system-menu-block.html.twig
+++ b/core/themes/claro/templates/classy/block/block--system-menu-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: HTML attributes for the containing element.
  *   - id: A valid HTML ID and guaranteed unique.
diff --git a/core/themes/claro/templates/classy/block/block.html.twig b/core/themes/claro/templates/classy/block/block.html.twig
index fd3311be958b..114d7c4de4f8 100644
--- a/core/themes/claro/templates/classy/block/block.html.twig
+++ b/core/themes/claro/templates/classy/block/block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: array of HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template.
diff --git a/core/themes/olivero/templates/block/block--page-title-block.html.twig b/core/themes/olivero/templates/block/block--page-title-block.html.twig
index 643617016889..1cf7bcb0e982 100644
--- a/core/themes/olivero/templates/block/block--page-title-block.html.twig
+++ b/core/themes/olivero/templates/block/block--page-title-block.html.twig
@@ -12,6 +12,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: array of HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template.
diff --git a/core/themes/olivero/templates/block/block--primary-menu--plugin-id--search-form-block.html.twig b/core/themes/olivero/templates/block/block--primary-menu--plugin-id--search-form-block.html.twig
index f6bb1414d7e2..529b857ec979 100644
--- a/core/themes/olivero/templates/block/block--primary-menu--plugin-id--search-form-block.html.twig
+++ b/core/themes/olivero/templates/block/block--primary-menu--plugin-id--search-form-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - content_attributes: A list of HTML attributes applied to the main content
  * - attributes: A list HTML attributes populated by modules, intended to
diff --git a/core/themes/olivero/templates/block/block--secondary-menu.html.twig b/core/themes/olivero/templates/block/block--secondary-menu.html.twig
index 6c6c77a9b0ac..3c03dace1be9 100644
--- a/core/themes/olivero/templates/block/block--secondary-menu.html.twig
+++ b/core/themes/olivero/templates/block/block--secondary-menu.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: HTML attributes for the containing element.
  *   - id: A valid HTML ID and guaranteed unique.
diff --git a/core/themes/olivero/templates/block/block--system-menu-block.html.twig b/core/themes/olivero/templates/block/block--system-menu-block.html.twig
index 1b56b155d0dd..9e9c25df6a62 100644
--- a/core/themes/olivero/templates/block/block--system-menu-block.html.twig
+++ b/core/themes/olivero/templates/block/block--system-menu-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: HTML attributes for the containing element.
  *   - id: A valid HTML ID and guaranteed unique.
diff --git a/core/themes/olivero/templates/block/block.html.twig b/core/themes/olivero/templates/block/block.html.twig
index 647fb4e1f709..6681e1ea851c 100644
--- a/core/themes/olivero/templates/block/block.html.twig
+++ b/core/themes/olivero/templates/block/block.html.twig
@@ -12,6 +12,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: array of HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template.
diff --git a/core/themes/stable9/layouts/layout_builder/fourcol_section/layout--fourcol-section.html.twig b/core/themes/stable9/layouts/layout_builder/fourcol_section/layout--fourcol-section.html.twig
index e610278c45e0..0dd0820a7d5b 100644
--- a/core/themes/stable9/layouts/layout_builder/fourcol_section/layout--fourcol-section.html.twig
+++ b/core/themes/stable9/layouts/layout_builder/fourcol_section/layout--fourcol-section.html.twig
@@ -4,6 +4,7 @@
  * Theme override for a four-column 25%-25%-25%-25% layout.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  */
diff --git a/core/themes/stable9/layouts/layout_builder/threecol_section/layout--threecol-section.html.twig b/core/themes/stable9/layouts/layout_builder/threecol_section/layout--threecol-section.html.twig
index 123acfec3e60..a85b90c127f0 100644
--- a/core/themes/stable9/layouts/layout_builder/threecol_section/layout--threecol-section.html.twig
+++ b/core/themes/stable9/layouts/layout_builder/threecol_section/layout--threecol-section.html.twig
@@ -4,6 +4,7 @@
  * Theme override for a three-column layout.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  */
diff --git a/core/themes/stable9/layouts/layout_builder/twocol_section/layout--twocol-section.html.twig b/core/themes/stable9/layouts/layout_builder/twocol_section/layout--twocol-section.html.twig
index d047debe53c3..dc9ee5ccdc73 100644
--- a/core/themes/stable9/layouts/layout_builder/twocol_section/layout--twocol-section.html.twig
+++ b/core/themes/stable9/layouts/layout_builder/twocol_section/layout--twocol-section.html.twig
@@ -4,6 +4,7 @@
  * Theme override to display a two-column layout.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  */
diff --git a/core/themes/stable9/layouts/layout_discovery/onecol/layout--onecol.html.twig b/core/themes/stable9/layouts/layout_discovery/onecol/layout--onecol.html.twig
index 7fe5b121ca8f..f3bda9290c20 100644
--- a/core/themes/stable9/layouts/layout_discovery/onecol/layout--onecol.html.twig
+++ b/core/themes/stable9/layouts/layout_discovery/onecol/layout--onecol.html.twig
@@ -4,6 +4,7 @@
  * Theme override to display a one-column layout.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  */
diff --git a/core/themes/stable9/layouts/layout_discovery/threecol_25_50_25/layout--threecol-25-50-25.html.twig b/core/themes/stable9/layouts/layout_discovery/threecol_25_50_25/layout--threecol-25-50-25.html.twig
index 715470bd061a..6829a6e6fed0 100644
--- a/core/themes/stable9/layouts/layout_discovery/threecol_25_50_25/layout--threecol-25-50-25.html.twig
+++ b/core/themes/stable9/layouts/layout_discovery/threecol_25_50_25/layout--threecol-25-50-25.html.twig
@@ -7,6 +7,7 @@
  * additional areas for the top and the bottom.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  */
diff --git a/core/themes/stable9/layouts/layout_discovery/threecol_33_34_33/layout--threecol-33-34-33.html.twig b/core/themes/stable9/layouts/layout_discovery/threecol_33_34_33/layout--threecol-33-34-33.html.twig
index f0a541d49581..43bfe201570d 100644
--- a/core/themes/stable9/layouts/layout_discovery/threecol_33_34_33/layout--threecol-33-34-33.html.twig
+++ b/core/themes/stable9/layouts/layout_discovery/threecol_33_34_33/layout--threecol-33-34-33.html.twig
@@ -7,6 +7,7 @@
  * additional areas for the top and the bottom.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  */
diff --git a/core/themes/stable9/layouts/layout_discovery/twocol/layout--twocol.html.twig b/core/themes/stable9/layouts/layout_discovery/twocol/layout--twocol.html.twig
index 4de66f048958..c56c36c66b55 100644
--- a/core/themes/stable9/layouts/layout_discovery/twocol/layout--twocol.html.twig
+++ b/core/themes/stable9/layouts/layout_discovery/twocol/layout--twocol.html.twig
@@ -4,6 +4,7 @@
  * Theme override to display a two-column layout.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  */
diff --git a/core/themes/stable9/layouts/layout_discovery/twocol_bricks/layout--twocol-bricks.html.twig b/core/themes/stable9/layouts/layout_discovery/twocol_bricks/layout--twocol-bricks.html.twig
index 08a0b4792061..0069dd132e82 100644
--- a/core/themes/stable9/layouts/layout_discovery/twocol_bricks/layout--twocol-bricks.html.twig
+++ b/core/themes/stable9/layouts/layout_discovery/twocol_bricks/layout--twocol-bricks.html.twig
@@ -7,6 +7,7 @@
  * the top, bottom and in the middle.
  *
  * Available variables:
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content for this layout.
  * - attributes: HTML attributes for the layout <div>.
  */
diff --git a/core/themes/stable9/templates/block/block--system-menu-block.html.twig b/core/themes/stable9/templates/block/block--system-menu-block.html.twig
index e78e1de7f66c..937c1865eda4 100644
--- a/core/themes/stable9/templates/block/block--system-menu-block.html.twig
+++ b/core/themes/stable9/templates/block/block--system-menu-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: HTML attributes for the containing element.
  *   - id: A valid HTML ID and guaranteed unique.
diff --git a/core/themes/stable9/templates/block/block.html.twig b/core/themes/stable9/templates/block/block.html.twig
index dca6f48fb319..e842e4045766 100644
--- a/core/themes/stable9/templates/block/block.html.twig
+++ b/core/themes/stable9/templates/block/block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: array of HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template.
diff --git a/core/themes/starterkit_theme/templates/block/block--search-form-block.html.twig b/core/themes/starterkit_theme/templates/block/block--search-form-block.html.twig
index 667202fb6b65..d1cda724ba87 100644
--- a/core/themes/starterkit_theme/templates/block/block--search-form-block.html.twig
+++ b/core/themes/starterkit_theme/templates/block/block--search-form-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: A list HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template. Includes:
diff --git a/core/themes/starterkit_theme/templates/block/block--system-menu-block.html.twig b/core/themes/starterkit_theme/templates/block/block--system-menu-block.html.twig
index 407f8403fd28..db3f9f808815 100644
--- a/core/themes/starterkit_theme/templates/block/block--system-menu-block.html.twig
+++ b/core/themes/starterkit_theme/templates/block/block--system-menu-block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: HTML attributes for the containing element.
  *   - id: A valid HTML ID and guaranteed unique.
diff --git a/core/themes/starterkit_theme/templates/block/block.html.twig b/core/themes/starterkit_theme/templates/block/block.html.twig
index fd3311be958b..114d7c4de4f8 100644
--- a/core/themes/starterkit_theme/templates/block/block.html.twig
+++ b/core/themes/starterkit_theme/templates/block/block.html.twig
@@ -11,6 +11,7 @@
  *   - label_display: The display settings for the label.
  *   - provider: The module or other provider that provided this block plugin.
  *   - Block plugin specific settings will also be stored here.
+ * - in_preview: Whether the plugin is being rendered in preview mode.
  * - content: The content of this block.
  * - attributes: array of HTML attributes populated by modules, intended to
  *   be added to the main container tag of this template.
-- 
GitLab