ReactionDeleteForm.php in Context 8.0
File
modules/context_ui/src/Form/ReactionDeleteForm.php
View source
<?php
namespace Drupal\context_ui\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\context\ContextManager;
use Drupal\context\ContextInterface;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\context\ContextReactionInterface;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
class ReactionDeleteForm extends ConfirmFormBase implements ContainerInjectionInterface {
protected $context;
protected $reaction;
protected $contextManager;
function __construct(ContextManager $contextManager) {
$this->contextManager = $contextManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('context.manager'));
}
public function getQuestion() {
return $this
->t('Are you sure you want to remove the @reaction reaction.', [
'@reaction' => $this->reaction
->getPluginDefinition()['label'],
]);
}
public function getCancelUrl() {
return $this->context
->urlInfo();
}
public function getFormId() {
return 'context_reaction_delete_confirm';
}
public function buildForm(array $form, FormStateInterface $form_state, ContextInterface $context = NULL, $reaction_id = NULL) {
$this->context = $context;
$this->reaction = $this->context
->getReaction($reaction_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) {
$definition = $this->reaction
->getPluginDefinition();
$this->context
->removeReaction($this->reaction
->getPluginId());
$this->context
->save();
if (!$this
->getRequest()
->isXmlHttpRequest()) {
drupal_set_message($this
->t('The @label context reaction has been removed.', [
'@label' => $definition['label'],
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}
public function submitFormAjax() {
$response = new AjaxResponse();
$contextForm = $this->contextManager
->getForm($this->context, 'edit');
$response
->addCommand(new CloseModalDialogCommand());
$response
->addCommand(new ReplaceCommand('#context-reactions', $contextForm['reactions']));
return $response;
}
}