Commit a8179506 authored by catch's avatar catch
Browse files

Issue #2779321 by nedjo, Akram Khan, smustgrave, Meenakshi_j, pooja saraah,...

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 e95548be)
parent f035d25c
Loading
Loading
Loading
Loading
+15 −8
Original line number Diff line number Diff line
@@ -372,14 +372,20 @@ 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')));
    $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]);
@@ -387,6 +393,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
        $entity->setRegion($entity_values['region']);
        $entity->save();
      }
    }
    $this->messenger->addStatus($this->t('The block settings have been updated.'));
  }

+12 −0
Original line number Diff line number Diff line
@@ -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');

  }

  /**