diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php index 946645d..1cd854f 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -191,6 +191,9 @@ public function getValue() { if ($this->hasNewEntity()) { $values['entity'] = $this->entity; } + if ($this->target_id === static::$NEW_ENTITY_MARKER && $this->entity && !$this->entity->isNew()) { + $values['target_id'] = $this->entity->id(); + } return $values; } diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php index 0898f48..f7150d3 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php @@ -208,6 +208,12 @@ public function testEntitySaveOrder() { $entity->name->value = $this->randomMachineName(); // Now save the term. $term->save(); + // Now serialize it. + $entity = serialize($entity); + // And unserialize it. + $entity = unserialize($entity); + // Now attempt to get the entity value. + $entity->get('field_test_taxonomy_term')->entity; // And then the entity. $entity->save(); $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term->id());