You are here

protected function ContextHandlerIntegrityTrait::checkDataTypeCompatible in Rules 8.3

Checks that the data type of a mapped variable matches the expectation.

Parameters

\Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition: The context definition of the context on the plugin.

\Drupal\Core\TypedData\DataDefinitionInterface $provided: The data definition of the mapped variable to the context.

string $context_name: The name of the context on the plugin.

\Drupal\rules\Engine\IntegrityViolationList $violation_list: The list of violations where new ones will be added.

1 call to ContextHandlerIntegrityTrait::checkDataTypeCompatible()
ContextHandlerIntegrityTrait::checkContextConfigIntegrity in src/Context/ContextHandlerIntegrityTrait.php
Performs the integrity check.

File

src/Context/ContextHandlerIntegrityTrait.php, line 131

Class

ContextHandlerIntegrityTrait
Extends the context handler trait with support for checking integrity.

Namespace

Drupal\rules\Context

Code

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);
  }
}