コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function form(array $form, FormStateInterface $form_state)
 {
     foreach ($this->entity->getEventNames() as $key => $event_name) {
         $event_definition = $this->eventManager->getDefinition($event_name);
         $form['event'][$key] = ['#type' => 'item', '#title' => $this->t('Events:'), '#markup' => $this->t('@label (@name)', ['@label' => $event_definition['label'], '@name' => $event_name])];
     }
     $form = $this->rulesUiHandler->getForm()->buildForm($form, $form_state);
     return parent::form($form, $form_state);
 }
コード例 #2
0
 /**
  * Returns a list of event names that are used by active reaction rules.
  *
  * @return string[]
  *   The list of event names keyed by event name.
  */
 protected function getRegisteredEvents()
 {
     $events = [];
     foreach ($this->loadMultiple() as $rules_config) {
         foreach ($rules_config->getEventNames() as $event_name) {
             $event_name = $this->eventManager->getEventBaseName($event_name);
             if (!isset($events[$event_name])) {
                 $events[$event_name] = $event_name;
             }
         }
     }
     return $events;
 }
コード例 #3
0
 /**
  * {@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['events']['#tree'] = TRUE;
     $form['events'][]['event_name'] = ['#type' => 'select', '#title' => $this->t('React on event'), '#options' => $options, '#required' => TRUE, '#description' => $this->t('Whenever the event occurs, rule evaluation is triggered.')];
     return $form;
 }
コード例 #4
0
 /**
  * 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)
 {
     $storage = $this->entityTypeManager->getStorage('rules_reaction_rule');
     // Get event metadata and the to be triggered events.
     $event_definition = $this->eventManager->getDefinition($event_name);
     $handler_class = $event_definition['class'];
     $triggered_events = [$event_name];
     if (is_subclass_of($handler_class, RulesConfigurableEventHandlerInterface::class)) {
         $qualified_event_suffixes = $handler_class::determineQualifiedEvents($event, $event_name, $event_definition);
         foreach ($qualified_event_suffixes as $qualified_event_suffix) {
             $triggered_events[] = "{$event_name}--{$qualified_event_suffix}";
         }
     }
     // Setup the execution state.
     $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);
     }
     // Invoke the rules.
     // @todo: Improve this by cloning the state after each rule, such that added
     // variables added by one rule are not interfering with the variables of
     // another rule.
     foreach ($triggered_events as $triggered_event) {
         // @todo Only load active reaction rules here.
         $configs = $storage->loadByProperties(['events.*.event_name' => $triggered_event]);
         // Loop over all rules and execute them.
         foreach ($configs as $config) {
             /** @var \Drupal\rules\Entity\ReactionRuleConfig $config */
             $config->getExpression()->executeWithState($state);
         }
     }
     $state->autoSave();
 }