diff --git a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityTypeConstraint.php b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityTypeConstraint.php index df25b6c..6affd3e 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityTypeConstraint.php +++ b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityTypeConstraint.php @@ -20,6 +20,8 @@ */ class EntityTypeConstraint extends Constraint { + public $typedData; + /** * The default violation message. * diff --git a/core/lib/Drupal/Core/TypedData/TypedData.php b/core/lib/Drupal/Core/TypedData/TypedData.php index c81f7c3..0112609 100644 --- a/core/lib/Drupal/Core/TypedData/TypedData.php +++ b/core/lib/Drupal/Core/TypedData/TypedData.php @@ -128,7 +128,9 @@ public function getConstraints() { $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager(); $constraints = array(); foreach ($this->definition->getConstraints() as $name => $options) { - $constraints[] = $constraint_manager->create($name, $options); + $constraint = $constraint = $constraint_manager->create($name, $options); + $constraint->typedData = $this; + $constraints[] = $constraint; } return $constraints; } diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraint.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraint.php index 939fbab..b2d614d 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraint.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraint.php @@ -21,6 +21,8 @@ */ class AllowedValuesConstraint extends Choice { + public $typedData = NULL; + public $minMessage = 'You must select at least %limit choice.|You must select at least %limit choices.'; public $maxMessage = 'You must select at most %limit choice.|You must select at most %limit choices.'; } diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraintValidator.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraintValidator.php index efc6906..8bba66c 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraintValidator.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/AllowedValuesConstraintValidator.php @@ -21,7 +21,7 @@ class AllowedValuesConstraintValidator extends ChoiceValidator { * {@inheritdoc} */ public function validate($value, Constraint $constraint) { - $typed_data = $this->context->getMetadata()->getTypedData(); + $typed_data = $constraint->typedData; if ($typed_data instanceof OptionsProviderInterface) { $account = \Drupal::currentUser(); diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraint.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraint.php index 1d9ca3c..2baf47b 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraint.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraint.php @@ -19,5 +19,7 @@ */ class PrimitiveTypeConstraint extends Constraint { + public $typedData; + public $message = 'This value should be of the correct primitive type.'; } diff --git a/core/modules/system/src/Tests/Validation/AllowedValuesConstraintValidatorTest.php b/core/modules/system/src/Tests/Validation/AllowedValuesConstraintValidatorTest.php index a5242c1..7083649 100644 --- a/core/modules/system/src/Tests/Validation/AllowedValuesConstraintValidatorTest.php +++ b/core/modules/system/src/Tests/Validation/AllowedValuesConstraintValidatorTest.php @@ -52,7 +52,7 @@ public function testValidation() { // Make sure the information provided by a violation is correct. $violation = $violations[0]; $this->assertEqual($violation->getMessage(), t('The value you selected is not a valid choice.'), 'The message for invalid value is correct.'); - $this->assertEqual($violation->getRoot(), $typed_data, 'Violation root is correct.'); + $this->assertEqual($violation->getRoot(), 4, 'Violation root is correct.'); $this->assertEqual($violation->getInvalidValue(), 4, 'The invalid value is set correctly in the violation.'); }