diff --git a/src/DynamicEntityReferenceFieldItemList.php b/src/DynamicEntityReferenceFieldItemList.php index 5f3cdcb..751bf2c 100644 --- a/src/DynamicEntityReferenceFieldItemList.php +++ b/src/DynamicEntityReferenceFieldItemList.php @@ -135,9 +135,14 @@ class DynamicEntityReferenceFieldItemList extends EntityReferenceFieldItemList { * {@inheritdoc} */ public function validate() { - $return = parent::validate(); + $return = NULL; foreach ($this->list as $item) { - $return->addAll($item->validate()); + if (!$return) { + $return = $item->validate(); + } + else { + $return->addAll($item->validate()); + } } return $return; } diff --git a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php index 0fba979..baeda4f 100644 --- a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php +++ b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php @@ -35,6 +35,7 @@ class ValidDynamicReferenceConstraintValidator extends ConstraintValidator { } if (!$valid_type) { $this->context->addViolation($constraint->wrongTypeMessage, array('%type' => $type, '%id' => $id)); + return; } $referenced_entity = $value->get('entity')->getValue(); if (!$referenced_entity) { diff --git a/src/Tests/DynamicEntityReferenceFieldTest.php b/src/Tests/DynamicEntityReferenceFieldTest.php index 8d26469..001c510 100644 --- a/src/Tests/DynamicEntityReferenceFieldTest.php +++ b/src/Tests/DynamicEntityReferenceFieldTest.php @@ -119,13 +119,13 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase { $entity->{$this->fieldName}->target_id = $referenced_entity->id(); $violations = $entity->{$this->fieldName}->validate(); $this->assertEqual($violations->count(), 1, 'Validation throws a violation.'); - $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', array('%type' => $this->entityType, '%id' => $referenced_entity->id()))); + $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) is not allowed for this field.', array('%type' => $this->entityType, '%id' => $referenced_entity->id()))); // Test an invalid entity. $entity->{$this->fieldName}->entity = $entity; $violations = $entity->{$this->fieldName}->validate(); $this->assertEqual($violations->count(), 1, 'Validation throws a violation.'); - $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) does not exist.', array('%type' => $entity->getEntityTypeId(), '%id' => -1))); + $this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%type: %id) is not allowed for this field.', array('%type' => $entity->getEntityTypeId(), '%id' => -1))); // @todo Implement a test case for invalid bundle references after // https://drupal.org/node/2064191 is fixed