diff --git a/core/lib/Drupal/Core/Entity/Field/EntityReferenceField.php b/core/lib/Drupal/Core/Entity/Field/EntityReferenceField.php index b39220d..2755142 100644 --- a/core/lib/Drupal/Core/Entity/Field/EntityReferenceField.php +++ b/core/lib/Drupal/Core/Entity/Field/EntityReferenceField.php @@ -13,10 +13,11 @@ class EntityReferenceField extends Field { /** - * Returns all the entities referenced by this field. + * Returns all the entities referenced by this field, preserving field item + * deltas. * * @return array - * An array of entity objects. + * An array of entity objects indexed by field item deltas. */ public function targetEntities() { diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php index ee5e79a..378a34d 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/field_type/ConfigurableEntityReferenceItem.php @@ -31,6 +31,7 @@ * }, * default_widget = "entity_reference_autocomplete", * default_formatter = "entity_reference_label", + * list_class = "\Drupal\Core\Entity\Field\EntityReferenceField", * constraints = {"ValidReference" = TRUE} * ) * diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceFieldTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceFieldTest.php index 4119240..cac9208 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceFieldTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceFieldTest.php @@ -117,4 +117,65 @@ public function testEntityReferenceFieldValidation() { // https://drupal.org/node/2064191 is fixed } + /** + * Tests the multiple target entities loader. + */ + public function testTargetEntitiesMultipleLoad() { + // Set unlimited cardinality for 'entity_test.field_test' field. + $field = entity_load('field_entity', $this->entityType . '.' . $this->fieldName); + $field->cardinality = FIELD_CARDINALITY_UNLIMITED; + $field->save(); + + // Create the parent entity. + $entity = entity_create($this->entityType, array('type' => $this->bundle)); + + // Create three target entities and attach them to parent field. + $target_entities = array(); + $reference_field = array(); + for ($i = 0; $i < 3; $i++) { + $target_entity = entity_create($this->referencedEntityType, array('type' => $this->bundle)); + $target_entity->save(); + $target_entities[] = $target_entity; + $reference_field[]['target_id'] = $target_entity->id(); + } + + // Also attach a non-existent entity and a NULL target id. + $reference_field[3]['target_id'] = 99999; + $target_entities[3] = NULL; + $reference_field[4]['target_id'] = NULL; + $target_entities[4] = NULL; + + // Attach the first created target entity as the sixth item ($delta == 5) of + // the parent entity field. We want to test the case when the same target + // entity is referenced twice (or more times) in the same entity reference + // field. + $reference_field[5] = $reference_field[0]; + $target_entities[5] = $target_entities[0]; + + // Set the field value. + $entity->{$this->fieldName}->setValue($reference_field); + + // Check if ConfigurableEntityReferenceItem::getTargetType() works. + $this->assertEqual($entity->{$this->fieldName}[0]->getTargetType(), $this->referencedEntityType); + + // Load the target entities using EntityReferenceField::targetEntities(). + $entities = $entity->{$this->fieldName}->targetEntities(); + + // Test returned entities: + // - Deltas must be preserved. + // - Non-existent entities must not be retrieved in target entities result. + foreach ($target_entities as $delta => $target_entity) { + if (!empty($target_entity)) { + // There must be an entity in the loaded set having the same id for the + // same delta. + $this->assertEqual($target_entity->id(), $entities[$delta]->id()); + } + else { + // A non-existent or NULL entity target id must not return any item in + // the target entities set. + $this->assertFalse(isset($loaded_entities[$delta])); + } + } + } + }