From e95548becf180b211f9da270a3a30c4d51a01421 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 28 Nov 2022 12:03:48 +0000
Subject: [PATCH] Issue #2779321 by nedjo, Akram Khan, smustgrave, Meenakshi_j,
 pooja saraah, alexpott: Submitting empty block layout form results in
 breakage for all block entities

---
 core/modules/block/src/BlockListBuilder.php   | 23 ++++++++++++-------
 .../tests/src/Functional/BlockUiTest.php      | 12 ++++++++++
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/core/modules/block/src/BlockListBuilder.php b/core/modules/block/src/BlockListBuilder.php
index 6faa72257d2a..783adff47835 100644
--- a/core/modules/block/src/BlockListBuilder.php
+++ b/core/modules/block/src/BlockListBuilder.php
@@ -372,20 +372,27 @@ public function getDefaultOperations(EntityInterface $entity) {
    * {@inheritdoc}
    */
   public function validateForm(array &$form, FormStateInterface $form_state) {
-    // No validation.
+    if (empty($form_state->getValue('blocks'))) {
+      $form_state->setErrorByName('blocks', 'No blocks settings to update.');
+    }
+
   }
 
   /**
    * {@inheritdoc}
    */
   public function submitForm(array &$form, FormStateInterface $form_state) {
-    $entities = $this->storage->loadMultiple(array_keys($form_state->getValue('blocks')));
-    /** @var \Drupal\block\BlockInterface[] $entities */
-    foreach ($entities as $entity_id => $entity) {
-      $entity_values = $form_state->getValue(['blocks', $entity_id]);
-      $entity->setWeight($entity_values['weight']);
-      $entity->setRegion($entity_values['region']);
-      $entity->save();
+    $blocks = $form_state->getValue('blocks');
+    // Passing empty value to load Multiple would load all items from storage.
+    if (!empty($blocks)) {
+      $entities = $this->storage->loadMultiple(array_keys($blocks));
+      /** @var \Drupal\block\BlockInterface[] $entities */
+      foreach ($entities as $entity_id => $entity) {
+        $entity_values = $form_state->getValue(['blocks', $entity_id]);
+        $entity->setWeight($entity_values['weight']);
+        $entity->setRegion($entity_values['region']);
+        $entity->save();
+      }
     }
     $this->messenger->addStatus($this->t('The block settings have been updated.'));
   }
diff --git a/core/modules/block/tests/src/Functional/BlockUiTest.php b/core/modules/block/tests/src/Functional/BlockUiTest.php
index b60818f2d74e..b816d1153d21 100644
--- a/core/modules/block/tests/src/Functional/BlockUiTest.php
+++ b/core/modules/block/tests/src/Functional/BlockUiTest.php
@@ -107,6 +107,18 @@ public function testBlockDemoUiPage() {
     \Drupal::service('theme_installer')->install(['stable9']);
     $this->drupalGet('admin/structure/block/demo/stable9');
     $this->assertSession()->statusCodeEquals(404);
+
+    // Delete all blocks and verify block layout can be saved.
+    $block_storage = \Drupal::service('entity_type.manager')->getStorage('block');
+    $blocks = $block_storage->loadMultiple();
+    foreach ($blocks as $block) {
+      $block->delete();
+    }
+    $this->drupalGet('admin/structure/block');
+    $blocks_table = $this->xpath("//tr[@class='block-enabled']");
+    $this->assertEmpty($blocks_table, 'The blocks table is now empty.');
+    $this->submitForm([], 'Save blocks');
+
   }
 
   /**
-- 
GitLab