function ContextDefinition::isSatisfiedBy

Same name in other branches
  1. 9 core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php \Drupal\Core\Plugin\Context\ContextDefinition::isSatisfiedBy()
  2. 10 core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php \Drupal\Core\Plugin\Context\ContextDefinition::isSatisfiedBy()
  3. 11.x core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php \Drupal\Core\Plugin\Context\ContextDefinition::isSatisfiedBy()

Overrides ContextDefinitionInterface::isSatisfiedBy

File

core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php, line 345

Class

ContextDefinition
Defines a class for context definitions.

Namespace

Drupal\Core\Plugin\Context

Code

public function isSatisfiedBy(ContextInterface $context) {
    $definition = $context->getContextDefinition();
    if (!$this->dataTypeMatches($context)) {
        return FALSE;
    }
    // Get the value for this context, either directly if possible or by
    // introspecting the definition.
    if ($context->hasContextValue()) {
        $values = [
            $context->getContextData(),
        ];
    }
    elseif ($definition instanceof self) {
        $this->initializeEntityContextDefinition();
        if ($this->entityContextDefinition) {
            $values = $this->entityContextDefinition
                ->getSampleValues();
        }
        else {
            $values = $definition->getSampleValues();
        }
    }
    else {
        $values = [];
    }
    $validator = $this->getTypedDataManager()
        ->getValidator();
    foreach ($values as $value) {
        $constraints = array_values($this->getConstraintObjects());
        $violations = $validator->validate($value, $constraints);
        foreach ($violations as $delta => $violation) {
            // Remove any violation that does not correspond to the constraints.
            if (!in_array($violation->getConstraint(), $constraints)) {
                $violations->remove($delta);
            }
        }
        // If a value has no violations then the requirement is satisfied.
        if (!$violations->count()) {
            return TRUE;
        }
    }
    return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.