diff --git a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php index 3ecce46..e184e9f 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php +++ b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php @@ -56,6 +56,18 @@ public function getTargetDefinition() { } /** + * Checks whether the target entity has not been saved yet. + * + * @return bool + * TRUE if the entity is new, FALSE otherwise. + */ + public function isTargetNew() { + // We assume that if the target entity is not set we cannot have a new + // entity. + return !isset($this->id) && isset($this->target) && $this->target->getValue()->isNew(); + } + + /** * {@inheritdoc} */ public function getTarget() { 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 3b7a5f1..946645d 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -200,11 +200,8 @@ public function getValue() { public function onChange($property_name, $notify = TRUE) { // Make sure that the target ID and the target property stay in sync. if ($property_name == 'entity') { - // We assume that if the target entity is not set we cannot have a new - // entity. $property = $this->get('entity'); - $has_new_entity = $property->hasTarget() && $property->getTarget()->getValue()->isNew(); - $target_id = $has_new_entity ? static::$NEW_ENTITY_MARKER : $property->getTargetIdentifier(); + $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) { diff --git a/core/lib/Drupal/Core/TypedData/DataReferenceBase.php b/core/lib/Drupal/Core/TypedData/DataReferenceBase.php index f7952c6..1b9aaa5 100644 --- a/core/lib/Drupal/Core/TypedData/DataReferenceBase.php +++ b/core/lib/Drupal/Core/TypedData/DataReferenceBase.php @@ -31,13 +31,6 @@ /** * {@inheritdoc} */ - public function hasTarget() { - return isset($this->target); - } - - /** - * {@inheritdoc} - */ public function getTarget() { return $this->target; } diff --git a/core/lib/Drupal/Core/TypedData/DataReferenceInterface.php b/core/lib/Drupal/Core/TypedData/DataReferenceInterface.php index 056b676..8fe88fc 100644 --- a/core/lib/Drupal/Core/TypedData/DataReferenceInterface.php +++ b/core/lib/Drupal/Core/TypedData/DataReferenceInterface.php @@ -23,14 +23,6 @@ public function getTarget(); /** - * Checks whether the reference is set. - * - * @return bool - * TRUE if the reference is set, FALSE otherwise. - */ - public function hasTarget(); - - /** * Gets the identifier of the referenced data. * * @return int|string|null