/** * {@inheritdoc} */ public function checkIntegrity(ExecutionMetadataStateInterface $metadata_state) { $violation_list = new IntegrityViolationList(); if (empty($this->configuration['list'])) { $violation_list->addViolationWithMessage($this->t('List variable is missing.')); return $violation_list; } try { $list_definition = $metadata_state->fetchDefinitionByPropertyPath($this->configuration['list']); } catch (RulesIntegrityException $e) { $violation_list->addViolationWithMessage($this->t('List variable %list does not exist. @message', ['%list' => $this->configuration['list'], '@message' => $e->getMessage()])); return $violation_list; } $list_item_name = isset($this->configuration['list_item']) ? $this->configuration['list_item'] : 'list_item'; if ($metadata_state->hasDataDefinition($list_item_name)) { $violation_list->addViolationWithMessage($this->t('List item name %name conflicts with an existing variable.', ['%name' => $list_item_name])); return $violation_list; } if ($list_definition instanceof ListDataDefinitionInterface) { $list_item_definition = $list_definition->getItemDefinition(); $metadata_state->setDataDefinition($list_item_name, $list_item_definition); $violation_list = parent::checkIntegrity($metadata_state); // Remove the list item variable after the loop, it is out of scope now. $metadata_state->removeDataDefinition($list_item_name); return $violation_list; } $violation_list->addViolationWithMessage($this->t('The data type of list variable %list is not a list.', ['%list' => $this->configuration['list']])); return $violation_list; }
/** * Performs the integrity check. * * @param CoreContextAwarePluginInterface $plugin * The plugin with its defined context. * @param \Drupal\rules\Engine\ExecutionMetadataStateInterface $metadata_state * The current configuration state with all defined variables that are * available. * * @return \Drupal\rules\Engine\IntegrityViolationList * The list of integrity violations. */ protected function doCheckIntegrity(CoreContextAwarePluginInterface $plugin, ExecutionMetadataStateInterface $metadata_state) { $violation_list = new IntegrityViolationList(); $context_definitions = $plugin->getContextDefinitions(); foreach ($context_definitions as $name => $context_definition) { // Check if a data selector is configured that maps to the state. if (isset($this->configuration['context_mapping'][$name])) { try { $data_definition = $metadata_state->fetchDefinitionByPropertyPath($this->configuration['context_mapping'][$name]); $this->checkDataTypeCompatible($context_definition, $data_definition, $name, $violation_list); } catch (RulesIntegrityException $e) { $violation = new IntegrityViolation(); $violation->setMessage($this->t('Data selector %selector for context %context_name is invalid. @message', ['%selector' => $this->configuration['context_mapping'][$name], '%context_name' => $context_definition->getLabel(), '@message' => $e->getMessage()])); $violation->setContextName($name); $violation_list->add($violation); } if ($context_definition instanceof RulesContextDefinitionInterface && $context_definition->getAssignmentRestriction() === RulesContextDefinitionInterface::ASSIGNMENT_RESTRICTION_INPUT) { $violation = new IntegrityViolation(); $violation->setMessage($this->t('The context %context_name may not be configured using a selector.', ['%context_name' => $context_definition->getLabel()])); $violation->setContextName($name); $violation_list->add($violation); } } elseif (isset($this->configuration['context_values'][$name])) { if ($context_definition instanceof RulesContextDefinitionInterface && $context_definition->getAssignmentRestriction() === RulesContextDefinitionInterface::ASSIGNMENT_RESTRICTION_SELECTOR) { $violation = new IntegrityViolation(); $violation->setMessage($this->t('The context %context_name may only be configured using a selector.', ['%context_name' => $context_definition->getLabel()])); $violation->setContextName($name); $violation_list->add($violation); } } elseif ($context_definition->isRequired()) { $violation = new IntegrityViolation(); $violation->setMessage($this->t('The required context %context_name is missing.', ['%context_name' => $context_definition->getLabel()])); $violation->setContextName($name); $violation_list->add($violation); } } if ($plugin instanceof ContextProviderInterface) { $provided_context_definitions = $plugin->getProvidedContextDefinitions(); foreach ($provided_context_definitions as $name => $context_definition) { if (isset($this->configuration['provides_mapping'][$name])) { if (!preg_match('/^[0-9a-zA-Z_]*$/', $this->configuration['provides_mapping'][$name])) { $violation = new IntegrityViolation(); $violation->setMessage($this->t('Provided variable name %name contains not allowed characters.', ['%name' => $this->configuration['provides_mapping'][$name]])); $violation->setContextName($name); $violation_list->add($violation); } // Populate the state with the new variable that is provided by this // plugin. That is necessary so that the integrity check in subsequent // actions knows about the variable and does not throw violations. $metadata_state->setDataDefinition($this->configuration['provides_mapping'][$name], $context_definition->getDataDefinition()); } else { $metadata_state->setDataDefinition($name, $context_definition->getDataDefinition()); } } } return $violation_list; }
/** * Adds the definitions of provided context to the execution metadata state. * * @param CoreContextAwarePluginInterface $plugin * The context aware plugin of which to add provided context. * @param \Drupal\rules\Engine\ExecutionMetadataStateInterface $metadata_state * The execution metadata state to add variables to. */ protected function addProvidedContextDefinitions(CoreContextAwarePluginInterface $plugin, ExecutionMetadataStateInterface $metadata_state) { // If the plugin does not support providing context, there is nothing to do. if (!$plugin instanceof ContextProviderInterface) { return; } foreach ($plugin->getProvidedContextDefinitions() as $name => $context_definition) { if (isset($this->configuration['provides_mapping'][$name])) { // Populate the state with the new variable that is provided by this // plugin. That is necessary so that the integrity check in subsequent // actions knows about the variable and does not throw violations. $metadata_state->setDataDefinition($this->configuration['provides_mapping'][$name], $context_definition->getDataDefinition()); } else { $metadata_state->setDataDefinition($name, $context_definition->getDataDefinition()); } } }