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 8909072..e429b05 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -192,7 +192,7 @@ public function getValue() { $values['entity'] = $this->entity; } // The NEW_ENTITY_MARKER is an implementation detail, don't return that. - if (isset($values['target_id']) && $this->target_id === static::$NEW_ENTITY_MARKER) { + if (isset($values['target_id']) && $this->getTargetId() === static::$NEW_ENTITY_MARKER) { unset($values['target_id']); } return $values; @@ -208,8 +208,8 @@ public function onChange($property_name, $notify = TRUE) { $target_id = $property->isTargetNew() ? static::$NEW_ENTITY_MARKER : $property->getTargetIdentifier(); $this->writePropertyValue('target_id', $target_id); } - elseif ($property_name == 'target_id' && $this->target_id != static::$NEW_ENTITY_MARKER) { - $this->writePropertyValue('entity', $this->target_id); + elseif ($property_name == 'target_id' && $this->getTargetId() != static::$NEW_ENTITY_MARKER) { + $this->writePropertyValue('entity', $this->getTargetId()); } parent::onChange($property_name, $notify); } @@ -219,7 +219,7 @@ public function onChange($property_name, $notify = TRUE) { */ public function isEmpty() { // Avoid loading the entity by first checking the 'target_id'. - if ($this->target_id !== NULL) { + if ($this->getTargetId() !== NULL) { return FALSE; } if ($this->entity && $this->entity instanceof EntityInterface) { @@ -266,7 +266,7 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin * TRUE if the item holds an unsaved entity. */ public function hasNewEntity() { - return $this->target_id === static::$NEW_ENTITY_MARKER; + return $this->getTargetId() === static::$NEW_ENTITY_MARKER; } /** @@ -290,4 +290,22 @@ public static function calculateDependencies(FieldDefinitionInterface $field_def return $dependencies; } + /** + * {@inheritdoc} + */ + public function __get($name) { + $value = parent::__get($name); + if ($name == 'target_id' && $value == static::$NEW_ENTITY_MARKER) { + return NULL; + } + return $value; + } + + /** + * Returns the target id, even if the value is static::$NEW_ENTITY_MARKER. + */ + protected function getTargetId() { + return $this->get('target_id')->getValue(); + } + } diff --git a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php index 0144eab..6f1a5a6 100644 --- a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php +++ b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php @@ -236,6 +236,8 @@ public function testEntitySaveOrder() { $term->save(); // Now get the field value. $value = $entity->get('field_test_taxonomy_term'); + $this->assertTrue(empty($value['target_id'])); + $this->assertNull($entity->field_test_taxonomy_term->target_id); // And then set it. $entity->field_test_taxonomy_term = $value; // And then the entity.