Namespace
Drupal\rules\Form
File
-
src/Form/DeleteEventForm.php
View source
<?php
namespace Drupal\rules\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rules\Core\RulesEventManager;
use Drupal\rules\Entity\ReactionRuleConfig;
use Drupal\rules\Ui\RulesUiHandlerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DeleteEventForm extends ConfirmFormBase {
protected $eventManager;
protected $rulesUiHandler;
protected $reactionRule;
protected $id;
public function __construct(RulesEventManager $event_manager) {
$this->eventManager = $event_manager;
}
public static function create(ContainerInterface $container) {
return new static($container->get('plugin.manager.rules_event'));
}
public function getFormId() {
return 'rules_delete_event';
}
public function buildForm(array $form, FormStateInterface $form_state, RulesUiHandlerInterface $rules_ui_handler = NULL, ReactionRuleConfig $rules_reaction_rule = NULL, $id = NULL) {
$this->rulesUiHandler = $rules_ui_handler;
$this->reactionRule = $rules_reaction_rule;
$this->id = $id;
return parent::buildForm($form, $form_state);
}
public function getConfirmText() {
return $this->t('Delete');
}
public function getQuestion() {
if (count($this->reactionRule
->getEvents()) === 1) {
throw new AccessDeniedHttpException('An event cannot be deleted if the reaction rule has only one.');
}
if (!$this->reactionRule
->hasEvent($this->id)) {
throw new NotFoundHttpException();
}
$event_definition = $this->eventManager
->getDefinition($this->id);
return $this->t('Are you sure you want to delete the event %title from %rule?', [
'%title' => $event_definition['label'],
'%rule' => $this->rulesUiHandler
->getComponentLabel(),
]);
}
public function getCancelUrl() {
return $this->rulesUiHandler
->getBaseRouteUrl();
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->reactionRule
->removeEvent($this->id);
$this->reactionRule
->save();
$this->messenger()
->addMessage($this->t('Deleted event %label from %rule.', [
'%label' => $this->eventManager
->getDefinition($this->id)['label'],
'%rule' => $this->reactionRule
->label(),
]));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
Classes