Namespace
Drupal\rules\Form
File
-
src/Form/AddEventForm.php
View source
<?php
namespace Drupal\rules\Form;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\rules\Core\RulesConfigurableEventHandlerInterface;
use Drupal\rules\Core\RulesEventManager;
use Drupal\rules\Ui\RulesUiHandlerInterface;
use Drupal\rules\Entity\ReactionRuleConfig;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AddEventForm extends FormBase {
use AddEventFormTrait;
protected $reactionRule;
public function __construct(RulesEventManager $event_manager, EntityTypeBundleInfoInterface $entity_bundle_info) {
$this->eventManager = $event_manager;
$this->entityBundleInfo = $entity_bundle_info;
}
public static function create(ContainerInterface $container) {
return new static($container->get('plugin.manager.rules_event'), $container->get('entity_type.bundle.info'));
}
public function getFormId() {
return 'rules_add_event';
}
public function getTitle(RulesUiHandlerInterface $rules_ui_handler) {
return $this->t('Add event to %rule', [
'%rule' => $rules_ui_handler->getComponentLabel(),
]);
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->t('Save');
return $actions;
}
public function buildForm(array $form, FormStateInterface $form_state, ReactionRuleConfig $rules_reaction_rule = NULL) {
$this->reactionRule = $rules_reaction_rule;
$form = $this->buildEventForm($form, $form_state);
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Add'),
'#button_type' => 'primary',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$event_name = $form_state->getValue([
'events',
0,
'event_name',
]);
$event_definition = $this->eventManager
->getDefinition($event_name);
$handler_class = $event_definition['class'];
if (is_subclass_of($handler_class, RulesConfigurableEventHandlerInterface::class)) {
if (!array_key_exists('bundle', $form_state->getValues())) {
$form_state->setRebuild();
return;
}
$this->entityBundleBuilder('rules_reaction_rule', $this->reactionRule, $form, $form_state);
$event_name = $form_state->getValue([
'events',
0,
'event_name',
]);
}
$this->reactionRule
->addEvent($event_name);
$this->reactionRule
->save();
$this->messenger()
->addMessage($this->t('Added event %label to %rule.', [
'%label' => $this->eventManager
->getDefinition($event_name)['label'],
'%rule' => $this->reactionRule
->label(),
]));
$form_state->setRedirect('entity.rules_reaction_rule.edit_form', [
'rules_reaction_rule' => $this->reactionRule
->id(),
]);
}
}
Classes
| Title |
Deprecated |
Summary |
| AddEventForm |
|
UI form to add an event to a rule. |