diff --git a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php index eae736d..0a62850 100644 --- a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php +++ b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php @@ -28,7 +28,7 @@ class ValidDynamicReferenceConstraint extends Constraint { */ public $wrongIDMessage = 'The referenced entity (%type: %id) does not exist.'; public $wrongTypeMessage = 'The referenced entity type (%type) is not allowed for this field.'; - public $wrongBundleMessage = 'The entity must be of bundle %bundle.'; + public $wrongBundleMessage = 'The entity bundle must be one of %bundles, the selected entity is of bundle %bundle.'; /** * Validation message when the target_id or target_type is empty. diff --git a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php index 1829cc5..2734763 100644 --- a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php +++ b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php @@ -58,9 +58,13 @@ class ValidDynamicReferenceConstraintValidator extends ConstraintValidator { } // Get the handler settings for the target type of this item. $target_type_settings = $value->getParent()->getSetting($type); - if (isset($target_type_settings['handler_settings']['target_bundles'])) { + // No bundles selected = all bundles are valid. + if (!empty($target_type_settings['handler_settings']['target_bundles'])) { if (!in_array($referenced_entity->bundle(), $target_type_settings['handler_settings']['target_bundles'], TRUE)) { - $this->context->addViolation($constraint->wrongBundleMessage, array('%bundle' => $referenced_entity->bundle())); + $this->context->addViolation($constraint->wrongBundleMessage, array( + '%bundles' => implode(', ', $target_type_settings['handler_settings']['target_bundles']), + '%bundle' => $referenced_entity->bundle(), + )); return; } } diff --git a/src/Tests/DynamicEntityReferenceFieldTest.php b/src/Tests/DynamicEntityReferenceFieldTest.php index 188617e..67dbdc7 100644 --- a/src/Tests/DynamicEntityReferenceFieldTest.php +++ b/src/Tests/DynamicEntityReferenceFieldTest.php @@ -141,7 +141,7 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase { $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 (%type) is not allowed for this field.', array('%type' => $entity->getEntityTypeId()))); // Test an invalid bundle. $dud_referenced_entity = $entity_manager @@ -151,7 +151,10 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase { $entity->{$this->fieldName}->entity = $dud_referenced_entity; $violations = $entity->{$this->fieldName}->validate(); $this->assertEqual($violations->count(), 1, 'Validation throws a violation.'); - $this->assertEqual($violations[0]->getMessage(), t('The entity must be of bundle %bundle.', array('%bundle' => 'dud'))); + $this->assertEqual($violations[0]->getMessage(), t('The entity bundle must be one of %bundles, the selected entity is of bundle %bundle.', array( + '%bundle' => 'dud', + '%bundles' => $this->bundle, + ))); } /**