/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $this->addLockInformation($form); $event_name = $this->entity->getEvent(); $event_definition = $this->eventManager->getDefinition($event_name); $form['event']['#markup'] = $this->t('Event: @label (@name)', ['@label' => $event_definition['label'], '@name' => $event_name]); $form_handler = $this->entity->getExpression()->getFormHandler(); $form = $form_handler->form($form, $form_state); return parent::form($form, $form_state); }
/** * Constructs a new class instance. * * @param array $configuration * A configuration array containing information about the plugin instance. * @param string $plugin_id * The plugin_id for the plugin instance. * @param array $plugin_definition * The plugin implementation definition. * @param \Drupal\rules\Engine\ExpressionManagerInterface $expression_manager * The rules expression plugin manager. * @param \Drupal\rules\Engine\RulesEventManager $event_manager * The Rules event manager. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, ExpressionManagerInterface $expression_manager, RulesEventManager $event_manager) { // @todo Reaction rules should also work with multiple events. if (isset($configuration['event'])) { $event_definition = $event_manager->getDefinition($configuration['event']); if (!empty($event_definition['context'])) { $plugin_definition['context'] = $event_definition['context']; } } parent::__construct($configuration, $plugin_id, $plugin_definition, $expression_manager); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $event_definitions = $this->eventManager->getGroupedDefinitions(); $options = []; foreach ($event_definitions as $group => $definitions) { foreach ($definitions as $id => $definition) { $options[$group][$id] = $definition['label']; } } $form['event'] = ['#type' => 'select', '#title' => $this->t('React on event'), '#options' => $options, '#required' => TRUE, '#empty_value' => $this->t('- Select -'), '#description' => $this->t('Whenever the event occurs, rule evaluation is triggered.')]; return $form; }
/** * Reacts on the given event and invokes configured reaction rules. * * @param \Symfony\Component\EventDispatcher\Event $event * The event object containing context for the event. * @param string $event_name * The event name. */ public function onRulesEvent(Event $event, $event_name) { // Load reaction rule config entities by $event_name. $storage = $this->entityTypeManager->getStorage('rules_reaction_rule'); // @todo Only load active reaction rules here. $configs = $storage->loadByProperties(['event' => $event_name]); // Set up an execution state with the event context. $event_definition = $this->eventManager->getDefinition($event_name); $state = ExecutionState::create(); foreach ($event_definition['context'] as $context_name => $context_definition) { // If this is a GenericEvent get the context for the rule from the event // arguments. if ($event instanceof GenericEvent) { $value = $event->getArgument($context_name); } else { $value = $event->{$context_name}; } $state->setVariable($context_name, $context_definition, $value); } // Loop over all rules and execute them. foreach ($configs as $config) { /** @var \Drupal\rules\Entity\ReactionRuleConfig $config */ $config->getExpression()->executeWithState($state); } $state->autoSave(); }