diff --git a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraintValidator.php b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraintValidator.php index 6a8f7cd..8459c52 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraintValidator.php +++ b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraintValidator.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Entity\Plugin\Validation\Constraint; +use Drupal\Core\Entity\ContentEntityInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; @@ -33,5 +34,13 @@ public function validate($value, Constraint $constraint) { $type = $value->getFieldDefinition()->getSetting('target_type'); $this->context->addViolation($constraint->message, array('%type' => $type, '%id' => $id)); } + elseif ($referenced_entity->isNew() && $referenced_entity instanceof ContentEntityInterface) { + $violations = $referenced_entity->validate(); + /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */ + foreach ($violations as $violation) { + $this->context->addViolation($violation->getMessageTemplate(), $violation->getMessageParameters()); + } + } } + } diff --git a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php index c1179f0..36f1e10 100644 --- a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php +++ b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php @@ -214,4 +214,40 @@ public function testEntitySaveOrder() { $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term->id()); } + /** + * Tests if an auto-created reference entity is validated when validating the + * host. + */ + public function testValidateAutoCreatedEntityReference() { + $checks = array( + 'The username cannot begin with a space.', + 'The username cannot end with a space.', + 'The username cannot contain multiple spaces in a row.', + 'The username %name is too long: it must be %max characters or less.', + ); + + entity_reference_create_field('entity_test', 'entity_test', 'field_test_author', 'Test user entity reference', 'user'); + + $account = entity_create('user', array( + // We create an user account with a wrong username. + 'name' => ' wrong username01234567890123456789012345678901234567890123456789 ', + 'mail' => $this->randomMachineName() . '@example.com', + )); + + // Create the host entity and attach the term. + $entity = entity_create('entity_test'); + $entity->name->value = $this->randomMachineName(); + $entity->field_test_author->entity = $account; + + // Validate the host entity. + $violations = $entity->validate(); + + $this->assertEqual(count($violations), 4); + + /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */ + foreach ($violations as $delta => $violation) { + $this->assertEqual($violation->getMessageTemplate(), $checks[$delta]); + } + } + }