class AddEventForm

UI form to add an event to a rule.

Hierarchy

Expanded class hierarchy of AddEventForm

File

src/Form/AddEventForm.php, line 17

Namespace

Drupal\rules\Form
View source
class AddEventForm extends FormBase {
    use AddEventFormTrait;
    
    /**
     * The Reaction Rule being modified.
     *
     * @var \Drupal\rules\Entity\ReactionRuleConfig
     */
    protected $reactionRule;
    
    /**
     * Constructs a new event add form.
     *
     * @param \Drupal\rules\Core\RulesEventManager $event_manager
     *   The Rules event manager.
     * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_bundle_info
     *   The entity type bundle information manager.
     */
    public function __construct(RulesEventManager $event_manager, EntityTypeBundleInfoInterface $entity_bundle_info) {
        $this->eventManager = $event_manager;
        $this->entityBundleInfo = $entity_bundle_info;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('plugin.manager.rules_event'), $container->get('entity_type.bundle.info'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'rules_add_event';
    }
    
    /**
     * Provides the page title on the form.
     */
    public function getTitle(RulesUiHandlerInterface $rules_ui_handler) {
        return $this->t('Add event to %rule', [
            '%rule' => $rules_ui_handler->getComponentLabel(),
        ]);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function actions(array $form, FormStateInterface $form_state) {
        $actions = parent::actions($form, $form_state);
        $actions['submit']['#value'] = $this->t('Save');
        return $actions;
    }
    
    /**
     * {@inheritdoc}
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    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(),
        ]);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AddEventForm::$reactionRule protected property The Reaction Rule being modified.
AddEventForm::actions protected function
AddEventForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
AddEventForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
AddEventForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AddEventForm::getTitle public function Provides the page title on the form.
AddEventForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
AddEventForm::__construct public function Constructs a new event add form.
AddEventFormTrait::$entityBundleInfo protected property The entity type bundle information manager.
AddEventFormTrait::$eventManager protected property The Rules event manager.
AddEventFormTrait::buildEventForm public function
AddEventFormTrait::bundleSelectCallback public function Ajax callback for the entity bundle restriction select element.
AddEventFormTrait::entityBundleBuilder public function Callback method for the #entity_builder form property.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 73
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 17
MessengerTrait::messenger public function Gets the messenger. 17
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.