diff --git a/src/Plugin/DataType/DynamicEntityReference.php b/src/Plugin/DataType/DynamicEntityReference.php index 7793a14..285aeca 100644 --- a/src/Plugin/DataType/DynamicEntityReference.php +++ b/src/Plugin/DataType/DynamicEntityReference.php @@ -65,7 +65,19 @@ class DynamicEntityReference extends EntityReference { * {@inheritdoc} */ public function getConstraints() { - $this->definition->addConstraint('Bundle', ['bundle' => $this->parent->parent->getSetting($this->parent->getValue()['target_type'])['handler_settings']['target_bundles']]); + $field = $this->parent->parent; + $values = $this->parent->getValue(); + // We cannot validate the bundle until we know the target-type of this + // item. + if (isset($values['target_type'])) { + $target_type = $values['target_type']; + // Get the handler settings for the target type of this item. + $target_type_settings = $field->getSettings($target_type); + if (isset($target_type_settings['handler_settings']['target_bundles'])) { + // Add the constraint to limit the bundles of this item. + $this->definition->addConstraint('Bundle', ['bundle' => $target_type_settings['handler_settings']['target_bundles']]); + } + } $constraints = parent::getConstraints(); return $constraints; }