Issue #2779321 by nedjo, Akram Khan, smustgrave, Meenakshi_j, pooja saraah, alexpott: Submitting empty block layout form results in breakage for all block entities

(cherry picked from commit e95548becf)
merge-requests/2670/head
catch 2022-11-28 12:03:48 +00:00
parent f035d25c46
commit a8179506e8
2 changed files with 27 additions and 8 deletions

View File

@ -372,20 +372,27 @@ class BlockListBuilder extends ConfigEntityListBuilder implements FormInterface
* {@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.'));
}

View File

@ -107,6 +107,18 @@ class BlockUiTest extends BrowserTestBase {
\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');
}
/**