diff --git a/src/DynamicEntityReferenceFieldItemList.php b/src/DynamicEntityReferenceFieldItemList.php index 751bf2c..a95fb28 100644 --- a/src/DynamicEntityReferenceFieldItemList.php +++ b/src/DynamicEntityReferenceFieldItemList.php @@ -133,6 +133,10 @@ class DynamicEntityReferenceFieldItemList extends EntityReferenceFieldItemList { /** * {@inheritdoc} + * + * When we validate the list, we make sure to give each item a chance to + * validate itself, as depending on the selected target type, we might need + * to apply bundle validation. */ public function validate() { $return = NULL; diff --git a/src/Plugin/DataType/DynamicEntityReference.php b/src/Plugin/DataType/DynamicEntityReference.php index 285aeca..62ddcec 100644 --- a/src/Plugin/DataType/DynamicEntityReference.php +++ b/src/Plugin/DataType/DynamicEntityReference.php @@ -63,6 +63,10 @@ class DynamicEntityReference extends EntityReference { /** * {@inheritdoc} + * + * The field-item list and the field-item refer validation of bundles to the + * entity property, as depending on the selected target-type, the bundle may + * need validation. */ public function getConstraints() { $field = $this->parent->parent; @@ -72,7 +76,7 @@ class DynamicEntityReference extends EntityReference { 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); + $target_type_settings = $field->getSetting($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']]); diff --git a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php index 55ed36a..2485154 100644 --- a/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php +++ b/src/Plugin/Field/FieldType/DynamicEntityReferenceItem.php @@ -474,6 +474,10 @@ class DynamicEntityReferenceItem extends ConfigurableEntityReferenceItem { /** * {@inheritdoc} + * + * When we validate the item, we make sure to validate the entity property, as + * depending on the selected target type, we might need to apply bundle + * validation. */ public function validate() { $return = parent::validate(); diff --git a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php index d898cef..63a74c0 100644 --- a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php +++ b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraint.php @@ -27,6 +27,6 @@ class ValidDynamicReferenceConstraint extends Constraint { * @var string */ public $wrongIDMessage = 'The referenced entity (%type: %id) does not exist.'; - public $wrongTypeMessage = 'The referenced entity (%type: %id) is not allowed for this field.'; + public $wrongTypeMessage = 'The referenced entity type (%type) is not allowed for this field.'; } diff --git a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php index baeda4f..70e822b 100644 --- a/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php +++ b/src/Plugin/Validation/Constraint/ValidDynamicReferenceConstraintValidator.php @@ -34,7 +34,7 @@ class ValidDynamicReferenceConstraintValidator extends ConstraintValidator { return; } if (!$valid_type) { - $this->context->addViolation($constraint->wrongTypeMessage, array('%type' => $type, '%id' => $id)); + $this->context->addViolation($constraint->wrongTypeMessage, array('%type' => $type)); return; } $referenced_entity = $value->get('entity')->getValue(); diff --git a/src/Tests/DynamicEntityReferenceFieldTest.php b/src/Tests/DynamicEntityReferenceFieldTest.php index 001c510..563dab4 100644 --- a/src/Tests/DynamicEntityReferenceFieldTest.php +++ b/src/Tests/DynamicEntityReferenceFieldTest.php @@ -78,13 +78,22 @@ class DynamicEntityReferenceFieldTest extends EntityUnitTestBase { ), ))->save(); - FieldConfig::create(array( + FieldConfig::create([ 'field_name' => $this->fieldName, 'entity_type' => $this->entityType, 'bundle' => $this->bundle, 'label' => 'Field test', - 'settings' => array(), - ))->save(); + 'settings' => [ + 'entity_test_rev' => [ + 'handler' => 'default', + 'handler_settings' => [ + 'target_bundles' => [ + 'entity_test', + ], + ], + ], + ], + ])->save(); } @@ -119,16 +128,21 @@ 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) is not allowed for this field.', array('%type' => $this->entityType, '%id' => $referenced_entity->id()))); + $this->assertEqual($violations[0]->getMessage(), t('The referenced entity type (%type) 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) is not allowed for this field.', 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(), '%id' => -1))); - // @todo Implement a test case for invalid bundle references after - // https://drupal.org/node/2064191 is fixed + // Test an invalid bundle. + $dud_referenced_entity = entity_create($this->referencedEntityType, array('type' => 'dud')); + $dud_referenced_entity->save(); + $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' => 'entity_test'))); } /**