BlockDeleteForm.php in Context 8
File
src/Reaction/Blocks/Form/BlockDeleteForm.php
View source
<?php
namespace Drupal\context\Reaction\Blocks\Form;
use Drupal\context\ContextInterface;
use Drupal\context\ContextManager;
use Drupal\context\Plugin\ContextReaction\Blocks;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BlockDeleteForm extends ConfirmFormBase {
protected $context;
protected $reaction;
protected $block;
protected $contextManager;
public function __construct(ContextManager $contextManager) {
$this->contextManager = $contextManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('context.manager'));
}
public function getFormId() {
return 'context_reaction_blocks_delete_block_form';
}
public function getQuestion() {
return $this
->t('Are you sure you want to remove the %label block?', [
'%label' => $this->block
->getConfiguration()['label'],
]);
}
public function getCancelUrl() {
return $this->context
->urlInfo();
}
public function buildForm(array $form, FormStateInterface $form_state, ContextInterface $context = NULL, $block_id = NULL) {
$this->context = $context;
$this->reaction = $this->context
->getReaction('blocks');
$this->block = $this->reaction
->getBlock($block_id);
$form = parent::buildForm($form, $form_state);
if ($this
->getRequest()
->isXmlHttpRequest()) {
unset($form['actions']['cancel']);
}
$form['actions']['submit']['#ajax'] = [
'callback' => '::submitFormAjax',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$configuration = $this->block
->getConfiguration();
$this->reaction
->removeBlock($configuration['uuid']);
$this->context
->save();
if (!$this
->getRequest()
->isXmlHttpRequest()) {
drupal_set_message($this
->t('The %label block has been removed.', [
'%label' => $configuration['label'],
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}
public function submitFormAjax() {
$contextForm = $this->contextManager
->getForm($this->context, 'edit');
$response = new AjaxResponse();
$response
->addCommand(new CloseModalDialogCommand());
$response
->addCommand(new ReplaceCommand('#context-reactions', $contextForm['reactions']));
return $response;
}
}