Пример #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() . '.');
         }
     }
 }
Пример #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()]));
         }
     }
 }
Пример #3
0
 /**
  * {@inheritdoc}
  */
 public function applyContextMapping(ContextAwarePluginInterface $plugin, $contexts, $mappings = array())
 {
     /** @var $contexts \Drupal\Core\Plugin\Context\ContextInterface[] */
     $mappings += $plugin->getContextMapping();
     // Loop through each of the expected contexts.
     $missing_value = [];
     foreach ($plugin->getContextDefinitions() as $plugin_context_id => $plugin_context_definition) {
         // If this context was given a specific name, use that.
         $context_id = isset($mappings[$plugin_context_id]) ? $mappings[$plugin_context_id] : $plugin_context_id;
         if (!empty($contexts[$context_id])) {
             // This assignment has been used, remove it.
             unset($mappings[$plugin_context_id]);
             // Plugins have their on context objects, only the value is applied.
             // They also need to know about the cacheability metadata of where that
             // value is coming from, so pass them through to those objects.
             $plugin_context = $plugin->getContext($plugin_context_id);
             if ($plugin_context instanceof ContextInterface && $contexts[$context_id] instanceof CacheableDependencyInterface) {
                 $plugin_context->addCacheableDependency($contexts[$context_id]);
             }
             // Pass the value to the plugin if there is one.
             if ($contexts[$context_id]->hasContextValue()) {
                 $plugin->setContextValue($plugin_context_id, $contexts[$context_id]->getContextData());
             } elseif ($plugin_context_definition->isRequired()) {
                 // Collect required contexts that exist but are missing a value.
                 $missing_value[] = $plugin_context_id;
             }
         } elseif ($plugin_context_definition->isRequired()) {
             // Collect required contexts that are missing.
             $missing_value[] = $plugin_context_id;
         } else {
             // Ignore mappings for optional missing context.
             unset($mappings[$plugin_context_id]);
         }
     }
     // If there are any required contexts without a value, throw an exception.
     if ($missing_value) {
         throw new ContextException(sprintf('Required contexts without a value: %s.', implode(', ', $missing_value)));
     }
     // If there are any mappings that were not satisfied, throw an exception.
     if (!empty($mappings)) {
         throw new ContextException('Assigned contexts were not satisfied: ' . implode(',', array_keys($mappings)));
     }
 }