From fbec11ff630a7a575ed9ba67b172b1164180b9cf Mon Sep 17 00:00:00 2001
From: Lauri Eskola <lauri.eskola@acquia.com>
Date: Mon, 5 Jun 2023 21:27:27 +0300
Subject: [PATCH] Issue #3039185 by nord102, yogeshmpawar, swentel,
 tim.plunkett, manishsaharan, nkoporec, catch, bkosborne: Allow field blocks
 to display the configuration label when set in Layout Builder

(cherry picked from commit 7598b15a28f370ae194153c183b158b13670703a)
---
 .../src/Plugin/Block/FieldBlock.php           |  6 ++-
 .../src/Functional/LayoutBuilderTest.php      | 40 +++++++++++++++++++
 .../tests/src/Kernel/FieldBlockTest.php       |  2 +-
 3 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php b/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php
index 1cdb92f917e2..ff3907f91211 100644
--- a/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php
+++ b/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php
@@ -159,7 +159,11 @@ public function build() {
     $display_settings['third_party_settings']['layout_builder']['view_mode'] = $this->getContextValue('view_mode');
     $entity = $this->getEntity();
     try {
-      $build = $entity->get($this->fieldName)->view($display_settings);
+      $build = [];
+      $view = $entity->get($this->fieldName)->view($display_settings);
+      if ($view) {
+        $build = [$view];
+      }
     }
     // @todo Remove in https://www.drupal.org/project/drupal/issues/2367555.
     catch (EnforcedResponseException $e) {
diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
index 2399ac5facac..6a8382bf6e77 100644
--- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
+++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
@@ -1305,6 +1305,46 @@ public function testBlockPlaceholder() {
     $assert_session->pageTextContains($block_content);
   }
 
+  /**
+   * Tests the ability to use a specified block label for field blocks.
+   */
+  public function testFieldBlockLabel() {
+    $assert_session = $this->assertSession();
+    $page = $this->getSession()->getPage();
+
+    $this->drupalLogin($this->drupalCreateUser([
+      'configure any layout',
+      'administer node display',
+    ]));
+
+    $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';
+    $this->drupalGet("$field_ui_prefix/display/default");
+    $this->submitForm(['layout[enabled]' => TRUE], 'Save');
+
+    // Customize the default view mode.
+    $this->drupalGet("$field_ui_prefix/display/default/layout");
+
+    // Add a body block whose label will be overridden.
+    $this->clickLink('Add block');
+    $this->clickLink('Body');
+
+    // Enable the Label Display and set the Label to a modified field
+    // block label.
+    $modified_field_block_label = 'Modified Field Block Label';
+    $page->checkField('settings[label_display]');
+    $page->fillField('settings[label]', $modified_field_block_label);
+
+    // Save the block and layout.
+    $page->pressButton('Add block');
+    $page->pressButton('Save layout');
+
+    // Revisit the default layout view mode page.
+    $this->drupalGet("$field_ui_prefix/display/default/layout");
+
+    // The modified field block label is displayed.
+    $assert_session->pageTextContains($modified_field_block_label);
+  }
+
   /**
    * Tests a custom alter of the overrides form.
    */
diff --git a/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php b/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php
index b8493ae37d63..605006f12d06 100644
--- a/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php
+++ b/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php
@@ -272,7 +272,7 @@ public function testBuild(PromiseInterface $promise, $expected_markup, $log_mess
       ],
     ];
     if ($expected_markup) {
-      $expected['content']['#markup'] = $expected_markup;
+      $expected[0]['content']['#markup'] = $expected_markup;
     }
 
     $actual = $block->build();
-- 
GitLab