/** * Creates a new violation with the message and adds it to this list. * * @param \Drupal\Core\StringTranslation\TranslatableMarkup $message * The violation message. * @param string $uuid * (Optional) UUID of the expression where the violation occurred. */ public function addViolationWithMessage(TranslatableMarkup $message, $uuid = NULL) { $violation = new IntegrityViolation(); $violation->setMessage($message); $violation->setUuid($uuid); $this[] = $violation; }
/** * Checks that the data type of a mapped variable matches the expectation. * * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition * The context definition of the context on the plugin. * @param \Drupal\Core\TypedData\DataDefinitionInterface $provided * The data definition of the mapped variable to the context. * @param string $context_name * The name of the context on the plugin. * @param \Drupal\rules\Engine\IntegrityViolationList $violation_list * The list of violations where new ones will be added. */ protected function checkDataTypeCompatible(ContextDefinitionInterface $context_definition, DataDefinitionInterface $provided, $context_name, IntegrityViolationList $violation_list) { $expected_class = $context_definition->getDataDefinition()->getClass(); $provided_class = $provided->getClass(); $expected_type_problem = NULL; if (is_subclass_of($expected_class, PrimitiveInterface::class) && !is_subclass_of($provided_class, PrimitiveInterface::class)) { $expected_type_problem = $this->t('primitive'); } elseif (is_subclass_of($expected_class, ListInterface::class) && !is_subclass_of($provided_class, ListInterface::class)) { $expected_type_problem = $this->t('list'); } elseif (is_subclass_of($expected_class, ComplexDataInterface::class) && !is_subclass_of($provided_class, ComplexDataInterface::class)) { $expected_type_problem = $this->t('complex'); } if ($expected_type_problem) { $violation = new IntegrityViolation(); $violation->setMessage($this->t('Expected a @expected_type data type for context %context_name but got a @provided_type data type instead.', ['@expected_type' => $expected_type_problem, '%context_name' => $context_definition->getLabel(), '@provided_type' => $provided->getDataType()])); $violation->setContextName($context_name); $violation_list->add($violation); } }
/** * Checks that the data type of a mapped variable matches the expectation. * * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition * The context definition of the context on the plugin. * @param \Drupal\Core\TypedData\DataDefinitionInterface $provided * The data definition of the mapped variable to the context. * @param string $context_name * The name of the context on the plugin. * @param \Drupal\rules\Engine\IntegrityViolationList $violation_list * The list of violations where new ones will be added. */ protected function checkDataTypeCompatible(CoreContextDefinitionInterface $context_definition, DataDefinitionInterface $provided, $context_name, IntegrityViolationList $violation_list) { // Compare data types. For now, fail if they are not equal. // @todo: Add support for matching based upon type-inheritance. $target_type = $context_definition->getDataDefinition()->getDataType(); // Special case any and entity target types for now. if ($target_type == 'any' || $target_type == 'entity' && strpos($provided->getDataType(), 'entity:') !== FALSE) { return; } if ($target_type != $provided->getDataType()) { $expected_type_problem = $context_definition->getDataDefinition()->getDataType(); $violation = new IntegrityViolation(); $violation->setMessage($this->t('Expected a @expected_type data type for context %context_name but got a @provided_type data type instead.', ['@expected_type' => $expected_type_problem, '%context_name' => $context_definition->getLabel(), '@provided_type' => $provided->getDataType()])); $violation->setContextName($context_name); $violation->setUuid($this->getUuid()); $violation_list->add($violation); } }