function AddEventForm::submitForm

Overrides FormInterface::submitForm

File

src/Form/AddEventForm.php, line 93

Class

AddEventForm
UI form to add an event to a rule.

Namespace

Drupal\rules\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
    $event_name = $form_state->getValue([
        'events',
        0,
        'event_name',
    ]);
    // Check if the selected event is an entity event.
    $event_definition = $this->eventManager
        ->getDefinition($event_name);
    $handler_class = $event_definition['class'];
    if (is_subclass_of($handler_class, RulesConfigurableEventHandlerInterface::class)) {
        // Support non-javascript browsers.
        if (!array_key_exists('bundle', $form_state->getValues())) {
            // The form field for "bundle" was not displayed yet, so rebuild the
            // form so that the user gets a chance to fill it in.
            $form_state->setRebuild();
            return;
        }
        // Add the bundle name to the event name if a bundle was selected.
        $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(),
    ]);
}