diff --git a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/CompositeConstraintBase.php b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/CompositeConstraintBase.php new file mode 100644 index 0000000..43936a5 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/CompositeConstraintBase.php @@ -0,0 +1,34 @@ +setTranslatable(TRUE) ->setSetting('max_length', 60) ->setDefaultValue('') - ->addConstraint('CommentName', array()); + ->addConstraint('CommentName'); $fields['mail'] = BaseFieldDefinition::create('email') ->setLabel(t('Email')) diff --git a/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraint.php b/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraint.php index d387542..15611e1 100644 --- a/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraint.php +++ b/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraint.php @@ -7,7 +7,7 @@ namespace Drupal\comment\Plugin\Validation\Constraint; -use Symfony\Component\Validator\Constraint; +use Drupal\Core\Entity\Plugin\Validation\Constraint\CompositeConstraintBase; /** * Supports validating comment author names. @@ -17,7 +17,7 @@ * label = @Translation("Comment author name", context = "Validation") * ) */ -class CommentNameConstraint extends Constraint { +class CommentNameConstraint extends CompositeConstraintBase { /** * Message shown when an anonymous user comments using a registered name. @@ -40,4 +40,11 @@ class CommentNameConstraint extends Constraint { */ public $messageMatch = 'The specified author name does not match the comment author.'; + /** + * {@inheritdoc} + */ + public function coversFields() { + return ['name', 'uid']; + } + } diff --git a/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraintValidator.php b/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraintValidator.php index f6397e0..8d52479 100644 --- a/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraintValidator.php +++ b/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraintValidator.php @@ -8,6 +8,7 @@ namespace Drupal\comment\Plugin\Validation\Constraint; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Entity\Plugin\Validation\Constraint\CompositeConstraintValidatorBase; use Drupal\user\UserStorageInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\comment\CommentInterface; @@ -46,40 +47,35 @@ public static function create(ContainerInterface $container) { /** * {@inheritdoc} */ - public function validate($items, Constraint $constraint) { - /** @var CommentNameConstraint $constraint */ - if (!isset($items)) { + public function validate($value, Constraint $constraint) { + if (!isset($value)) { return; } - /** @var CommentInterface $comment */ - $comment = $items->getEntity(); - if (!isset($comment)) { - // Looks like we are validating a field not being part of a comment, - // nothing we can do then. - return; - } - $author_name = $items->first()->value; + $entity = $value->getEntity(); + + $author_name = $entity->name->value; + $owner_id = (int) $entity->uid->target_id; // Do not allow unauthenticated comment authors to use a name that is // taken by a registered user. - if (isset($author_name) && $author_name !== '' && $comment->getOwnerId() === 0) { + if (isset($author_name) && $author_name !== '' && $owner_id === 0) { $users = $this->userStorage->loadByProperties(array('name' => $author_name)); if (!empty($users)) { $this->context->addViolation($constraint->messageNameTaken, array('%name' => $author_name)); } } // If an author name and owner are given, make sure they match. - elseif (isset($author_name) && $author_name !== '' && $comment->getOwnerId()) { - $owner = $comment->getOwner(); + elseif (isset($author_name) && $author_name !== '' && $owner_id) { + $owner = $this->userStorage->load($owner_id); if ($owner->getUsername() != $author_name) { $this->context->addViolation($constraint->messageMatch); } } // Anonymous account might be required - depending on field settings. - if ($comment->getOwnerId() === 0 && empty($author_name) && - $this->getAnonymousContactDetailsSetting($comment) === COMMENT_ANONYMOUS_MUST_CONTACT) { + if ($owner_id === 0 && empty($author_name) && + $this->getAnonymousContactDetailsSetting($entity) === COMMENT_ANONYMOUS_MUST_CONTACT) { $this->context->addViolation($constraint->messageRequired); } } diff --git a/core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php b/core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php index 4a56198..1be9696 100644 --- a/core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php +++ b/core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php @@ -37,7 +37,7 @@ protected function setUp() { */ public function testValidation() { $entity_type = 'entity_test_constraint_violation'; - $entity = entity_create($entity_type, array('id' => 1, 'revision_id' => 1)); + $entity = entity_create($entity_type, array('id' => 1, 'revision_id' => 1, 'name' => 'test', 'type' => 'test2')); $display = entity_get_form_display($entity_type, $entity_type, 'default'); $form = array(); $form_state = new FormState(); @@ -55,6 +55,7 @@ public function testValidation() { $errors = $form_state->getErrors(); $this->assertEqual($errors['name'], 'Widget constraint has failed.', 'Constraint violation is generated correctly'); + $this->assertEqual($errors['type'], 'Multiple fields are validated', 'Constraint violation is generated correctly'); } } diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraintViolation.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraintViolation.php index 48b27ab..9d83bed 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraintViolation.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestConstraintViolation.php @@ -44,6 +44,13 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { )); $fields['name']->addConstraint('FieldWidgetConstraint', array()); + $fields['type']->setDisplayOptions('form', array( + 'type' => 'string', + 'weight' => 0, + )); + + $fields['type']->addConstraint('EntityTestCompositeConstraint', array()); + return $fields; } diff --git a/core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/EntityTestCompositeConstraint.php b/core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/EntityTestCompositeConstraint.php new file mode 100644 index 0000000..8e6c4d5 --- /dev/null +++ b/core/modules/system/tests/modules/entity_test/src/Plugin/Validation/Constraint/EntityTestCompositeConstraint.php @@ -0,0 +1,31 @@ +getEntity(); + + if ($entity->name->value === 'test' && $entity->type->value === 'test2') { + $this->context->addViolation($constraint->message); + } + + } + +}