diff --git a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php index 0449c6b..a6ce9b2 100644 --- a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php +++ b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php @@ -59,6 +59,13 @@ class ValidDynamicReferenceConstraint extends Constraint { public $wrongBundleMessage = 'Referenced entity %label does not belong to one of the supported bundles (%bundles).'; /** + * Validate message if no bundle is allowed. + * + * @var string + */ + public $noBundleAllowed = 'No bundle is allowed for (%type)'; + + /** * Validation message when the target_id or target_type is empty. * * @var string diff --git a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php index 66cf329..f8b4b75 100644 --- a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php +++ b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php @@ -153,6 +153,17 @@ class ValidDynamicReferenceConstraintValidator extends ConstraintValidator imple if (array_key_exists('target_bundles', $value->getFieldDefinition()->getSetting($target_types[$delta])['handler_settings'])) { $valid_bundle = FALSE; $target_bundles = $value->getFieldDefinition()->getSetting($target_types[$delta])['handler_settings']['target_bundles']; + if ($target_bundles === []) { + $this->context->buildViolation($constraint->noBundleAllowed) + ->setParameter('%type', $target_types[$delta]) + ->atPath((string) $delta . '.entity') + ->setInvalidValue($entity) + ->addViolation(); + if (isset($target_ids[$delta])) { + unset($target_ids[$delta]); + } + continue; + } if ($target_bundles === NULL || in_array($entity->bundle(), $target_bundles)) { $valid_bundle = TRUE; } diff --git a/tests/src/Kernel/DynamicEntityReferenceFieldTest.php b/tests/src/Kernel/DynamicEntityReferenceFieldTest.php index 3139629..e728d31 100644 --- a/tests/src/Kernel/DynamicEntityReferenceFieldTest.php +++ b/tests/src/Kernel/DynamicEntityReferenceFieldTest.php @@ -168,7 +168,7 @@ class DynamicEntityReferenceFieldTest extends EntityKernelTestBase { $entity->{$this->fieldName}->target_id = $referenced_entity->id(); $violations = $entity->{$this->fieldName}->validate(); $this->assertEquals($violations->count(), 1, 'Validation throws a violation.'); - $this->assertEquals($violations[0]->getMessage(), t('Referenced entity %label does not belong to one of the supported bundles (%bundles).', ['%label' => $referenced_entity->label(), '%bundles' => ''])); + $this->assertEquals($violations[0]->getMessage(), t('No bundle is allowed for (%type)', ['%type' => $this->referencedEntityType])); // Test with wrong bundle. $bundle = EntityTestBundle::create([