function ContextDefinition::isSatisfiedBy
Same name in other branches
- 9 core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php \Drupal\Core\Plugin\Context\ContextDefinition::isSatisfiedBy()
- 8.9.x core/lib/Drupal/Core/Plugin/Context/ContextDefinition.php \Drupal\Core\Plugin\Context\ContextDefinition::isSatisfiedBy()
- 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 293
Class
- ContextDefinition
- Defines a class for context definitions.
Namespace
Drupal\Core\Plugin\ContextCode
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) {
$values = $definition->getSampleValues();
}
else {
$values = [];
}
$validator = $this->getTypedDataManager()
->getValidator();
foreach ($values as $value) {
$constraints = array_values($this->getConstraintObjects());
if ($definition->isMultiple()) {
$violations = new ConstraintViolationList();
foreach ($value as $item) {
$violations->addAll($validator->validate($item, $constraints));
}
}
else {
$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.