diff --git a/core/modules/block/src/BlockListBuilder.php b/core/modules/block/src/BlockListBuilder.php index 6faa72257d2a10b3ba33199c25a7f6ac1181fe00..783adff4783535db6cb947685d97b3b5d8dd8077 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 b60818f2d74ef1d7ace4d466df788b75184f6086..b816d1153d2112d9b312e2aca78d561f47189f43 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'); + } /**