Issue #3314469 by alecsmrekar, plach: ContextDefinition::isSatisfiedBy does not take into account cardinality
(cherry picked from commit 6dcb915131
)
merge-requests/2710/merge
parent
b98c7e543d
commit
f225ff2136
|
@ -4,6 +4,7 @@ namespace Drupal\Core\Plugin\Context;
|
|||
|
||||
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
|
||||
use Drupal\Core\TypedData\TypedDataTrait;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
|
||||
/**
|
||||
* Defines a class for context definitions.
|
||||
|
@ -307,7 +308,15 @@ class ContextDefinition implements ContextDefinitionInterface {
|
|||
$validator = $this->getTypedDataManager()->getValidator();
|
||||
foreach ($values as $value) {
|
||||
$constraints = array_values($this->getConstraintObjects());
|
||||
$violations = $validator->validate($value, $constraints);
|
||||
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)) {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Drupal\KernelTests\Core\Plugin;
|
||||
|
||||
use Drupal\Core\Plugin\Context\Context;
|
||||
use Drupal\Core\Plugin\Context\ContextDefinition;
|
||||
use Drupal\Core\Plugin\Context\EntityContext;
|
||||
use Drupal\Core\Plugin\Context\EntityContextDefinition;
|
||||
|
@ -33,6 +34,16 @@ class ContextDefinitionTest extends KernelTestBase {
|
|||
$requirement = new ContextDefinition('any');
|
||||
$context = EntityContext::fromEntity($value);
|
||||
$this->assertTrue($requirement->isSatisfiedBy($context));
|
||||
|
||||
// Test with multiple values.
|
||||
$definition = EntityContextDefinition::create('entity_test');
|
||||
$definition->setMultiple();
|
||||
$entities = [
|
||||
EntityTest::create([]),
|
||||
EntityTest::create([]),
|
||||
];
|
||||
$context = new Context($definition, $entities);
|
||||
$this->assertTrue($definition->isSatisfiedBy($context));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue