Verified Commit 26446bd2 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3325167 by smustgrave, acbramley, murilohp, rkoller, larowlan,...

Issue #3325167 by smustgrave, acbramley, murilohp, rkoller, larowlan, AaronMcHale, dww, BramDriesen: Revisit the redirect to 'add block' form in the 'add block content' form

(cherry picked from commit fca86410)
parent 5b39ed89
Loading
Loading
Loading
Loading
Loading
+57 −12
Original line number Diff line number Diff line
@@ -38,6 +38,51 @@ public function form(array $form, FormStateInterface $form_state) {
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function actions(array $form, FormStateInterface $form_state): array {
    $element = parent::actions($form, $form_state);

    if ($this->getRequest()->query->has('theme')) {
      $element['submit']['#value'] = $this->t('Save and configure');
    }

    if ($this->currentUser()->hasPermission('administer blocks') && !$this->getRequest()->query->has('theme') && $this->entity->isNew()) {
      $element['configure_block'] = [
        '#type' => 'submit',
        '#value' => $this->t('Save and configure'),
        '#weight' => 20,
        '#submit' => array_merge($element['submit']['#submit'], ['::configureBlock']),
      ];
    }

    return $element;
  }

  /**
   * Form submission handler for the 'configureBlock' action.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function configureBlock(array $form, FormStateInterface $form_state): void {
    $block = $this->entity;
    if (!$theme = $block->getTheme()) {
      $theme = $this->config('system.theme')->get('default');
    }
    $form_state->setRedirect(
      'block.admin_add',
      [
        'plugin_id' => 'block_content:' . $block->uuid(),
        'theme' => $theme,
      ]
    );
    $form_state->setIgnoreDestination();
  }

  /**
   * {@inheritdoc}
   */
@@ -64,9 +109,8 @@ public function save(array $form, FormStateInterface $form_state) {
      $form_state->setValue('id', $block->id());
      $form_state->set('id', $block->id());
      if ($insert) {
        if (!$theme = $block->getTheme()) {
          $theme = $this->config('system.theme')->get('default');
        }
        $theme = $block->getTheme();
        if ($theme) {
          $form_state->setRedirect(
            'block.admin_add',
            [
@@ -79,6 +123,7 @@ public function save(array $form, FormStateInterface $form_state) {
          $form_state->setRedirectUrl($block->toUrl('collection'));
        }
      }
    }
    else {
      // In the unlikely case something went wrong on save, the block will be
      // rebuilt and block form redisplayed.
+43 −4
Original line number Diff line number Diff line
@@ -95,10 +95,7 @@ public function testBlockContentCreationMultipleViewModes() {
    $edit['info[0][value]'] = 'Test Block';
    $edit['body[0][value]'] = $this->randomMachineName(16);
    $this->drupalGet('block/add/basic');
    $this->submitForm($edit, 'Save');

    // Check that the Basic block has been created.
    $this->assertSession()->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');
    $this->submitForm($edit, 'Save and configure');

    // Save our block permanently
    $this->submitForm(['region' => 'content'], 'Save block');
@@ -143,6 +140,48 @@ public function testBlockContentCreationMultipleViewModes() {
    $this->assertNotEmpty($block, 'Content Block found in database.');
  }

  /**
   * Tests the redirect workflow of creating a block_content and block.
   */
  public function testBlockContentFormSubmitHandlers() {
    $this->drupalLogin($this->adminUser);

    // Create a block and place in block layout.
    $this->drupalGet('/admin/content/block');
    $this->clickLink('Add content block');
    // Verify destination URL, when clicking "Save and configure" this
    // destination will be ignored.
    $base = base_path();
    $url = 'block/add?destination=' . $base . 'admin/content/block';
    $this->assertSession()->addressEquals($url);
    $edit = [];
    $edit['info[0][value]'] = 'Test Block';
    $edit['body[0][value]'] = $this->randomMachineName(16);
    $this->submitForm($edit, 'Save and configure');
    $this->assertSession()->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');
    $this->assertSession()->pageTextContains('Configure block');

    // Verify when editing a block "Save and configure" does not appear.
    $this->drupalGet('/admin/content/block/1');
    $this->assertSession()->buttonNotExists('Save and configure');

    // Create a block but go back to block library.
    $edit = [];
    $edit['info[0][value]'] = 'Test Block';
    $edit['body[0][value]'] = $this->randomMachineName(16);
    $this->drupalGet('block/add/basic');
    $this->submitForm($edit, 'Save');
    // Check that the Basic block has been created.
    $this->assertSession()->pageTextContains('basic ' . $edit['info[0][value]'] . ' has been created.');
    $this->assertSession()->addressEquals('/admin/content/block');

    // Test with user who doesn't have permission to place a block.
    $this->drupalLogin($this->drupalCreateUser(['administer block content']));
    $this->drupalGet('block/add/basic');
    $this->assertSession()->buttonNotExists('Save and configure');

  }

  /**
   * Create a default content block.
   *
+1 −1
Original line number Diff line number Diff line
@@ -233,7 +233,7 @@ public function testsBlockContentAddTypes() {
        $this->clickLink('foo');
        // Create a new block.
        $edit = ['info[0][value]' => $this->randomMachineName(8)];
        $this->submitForm($edit, 'Save');
        $this->submitForm($edit, 'Save and configure');
        $blocks = $storage->loadByProperties(['info' => $edit['info[0][value]']]);
        if (!empty($blocks)) {
          $block = reset($blocks);