Ejemplo n.º 1
0
 /**
  * Maps variables from rules state into the plugin context.
  *
  * @param \Drupal\Core\Plugin\ContextAwarePluginInterface $plugin
  *   The plugin that is populated with context values.
  * @param \Drupal\rules\Engine\ExecutionStateInterface $state
  *   The Rules state containing available variables.
  *
  * @throws \Drupal\rules\Exception\RulesEvaluationException
  *   In case a required context is missing for the plugin.
  */
 protected function mapContext(CoreContextAwarePluginInterface $plugin, ExecutionStateInterface $state)
 {
     $context_definitions = $plugin->getContextDefinitions();
     foreach ($context_definitions as $name => $definition) {
         // Check if a data selector is configured that maps to the state.
         if (isset($this->configuration['context_mapping'][$name])) {
             $typed_data = $state->fetchDataByPropertyPath($this->configuration['context_mapping'][$name]);
             if ($typed_data->getValue() === NULL && !$definition->isAllowedNull()) {
                 throw new RulesEvaluationException('The value of data selector ' . $this->configuration['context_mapping'][$name] . " is NULL, but the context {$name} in " . $plugin->getPluginId() . ' requires a value.');
             }
             $context = $plugin->getContext($name);
             $new_context = Context::createFromContext($context, $typed_data);
             $plugin->setContext($name, $new_context);
         } elseif (isset($this->configuration['context_values']) && array_key_exists($name, $this->configuration['context_values'])) {
             if ($this->configuration['context_values'][$name] === NULL && !$definition->isAllowedNull()) {
                 throw new RulesEvaluationException("The context value for {$name} is NULL, but the context {$name} in " . $plugin->getPluginId() . ' requires a value.');
             }
             $context = $plugin->getContext($name);
             $new_context = Context::createFromContext($context, $this->configuration['context_values'][$name]);
             $plugin->setContext($name, $new_context);
         } elseif ($definition->isRequired()) {
             throw new RulesEvaluationException("Required context {$name} is missing for plugin " . $plugin->getPluginId() . '.');
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Maps variables from rules state into the plugin context.
  *
  * @param \Drupal\Core\Plugin\ContextAwarePluginInterface $plugin
  *   The plugin that is populated with context values.
  * @param \Drupal\rules\Engine\RulesStateInterface $state
  *   The Rules state containing available variables.
  *
  * @throws \Drupal\rules\Exception\RulesEvaluationException
  *   In case a required context is missing for the plugin.
  */
 protected function mapContext(CoreContextAwarePluginInterface $plugin, RulesStateInterface $state)
 {
     $context_definitions = $plugin->getContextDefinitions();
     foreach ($context_definitions as $name => $definition) {
         // Check if a data selector is configured that maps to the state.
         if (isset($this->configuration['context_mapping'][$name])) {
             $typed_data = $state->applyDataSelector($this->configuration['context_mapping'][$name]);
             if ($typed_data->getValue() === NULL && !$definition->isAllowedNull()) {
                 throw new RulesEvaluationException(SafeMarkup::format('The value of data selector @selector is NULL, but the context @name in @plugin requires a value.', ['@selector' => $this->configuration['context_mapping'][$name], '@name' => $name, '@plugin' => $plugin->getPluginId()]));
             }
             $plugin->getContext($name)->setContextData($typed_data);
         } elseif (array_key_exists($name, $this->configuration['context_values'])) {
             if ($this->configuration['context_values'][$name] === NULL && !$definition->isAllowedNull()) {
                 throw new RulesEvaluationException(SafeMarkup::format('The context value for @name is NULL, but the context @name in @plugin requires a value.', ['@name' => $name, '@plugin' => $plugin->getPluginId()]));
             }
             $plugin->getContext($name)->setContextValue($this->configuration['context_values'][$name]);
         } elseif ($definition->isRequired()) {
             throw new RulesEvaluationException(SafeMarkup::format('Required context @name is missing for plugin @plugin.', ['@name' => $name, '@plugin' => $plugin->getPluginId()]));
         }
     }
 }