Commit b78b394a authored by renatog's avatar renatog Committed by renatog
Browse files

Issue #3269276 by RenatoG: Create a route to delete block class by id with confirmationForm

parent 14929802
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -21,6 +21,14 @@ block_class.confirm_bulk_operation:
  requirements:
    _permission: 'administer block classes'

block_class.confirm_deletion:
  path: '/admin/config/content/block-class/delete/{bid}'
  defaults:
    _form: '\Drupal\block_class\Form\BlockClassConfirmDeletionForm'
    _title: 'Confirm Deletion'
  requirements:
    _permission: 'administer block classes'

block_class.list:
  path: '/admin/config/content/block-class/list'
  defaults:
+96 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\block_class\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\block\Entity\Block;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Form for Block Class Confirm Bulk Operation.
 */
class BlockClassConfirmDeletionForm extends ConfirmFormBase {

  /**
   * Block ID.
   *
   * @var string
   */
  protected $bid;

  /**
   * {@inheritdoc}
   */
  // @codingStandardsIgnoreLine
  public function buildForm(array $form, FormStateInterface $form_state, $bid = NULL) {

    $this->bid = $bid;

    $message_to_confirm = (string) $this->getQuestion();

    $form['message_to_confirm'] = [
      '#type' => 'item',
      '#markup' => $message_to_confirm,
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Load block. Todo: We'll implements DI here @codingStandardsIgnoreLine
    $block = Block::load($this->bid);

    // If there is ThirdPartySetting remove that.
    $block->unsetThirdPartySetting('block_class', 'classes');

    // Block save.
    $block->save();

    // Set a messsage.
    \Drupal::messenger()->addStatus($this->t('Block Class deleted'));

    // Get the block class list path.
    $block_class_list_path = Url::fromRoute('block_class.list')->toString();

    // Get response.
    $response = new RedirectResponse($block_class_list_path);

    // Send to confirmation.
    $response->send();
    exit;

  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() : string {
    return "block_class_confirm_deletion_form";
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {

    return Url::fromRoute('block_class.list');

  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {

    $message_to_confirm = $this->t('Are you sure?');

    return $message_to_confirm;
  }

}