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 e5e6143..e106e47 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -148,26 +148,27 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) */ public function setValue($values, $notify = TRUE) { if (isset($values) && !is_array($values)) { - if ($values !== static::NEW_ENTITY_MARKER) { - // Directly update the property instead of invoking the parent, so it - // can handle objects and IDs. - $this->properties['entity']->setValue($values, $notify); - // If notify was FALSE, ensure the target_id property gets synched. - if (!$notify) { - $target_id = $values instanceof EntityInterface && $values->isNew() ? static::NEW_ENTITY_MARKER : $this->properties['entity']->getTargetIdentifier(); - $this->set('target_id', $target_id, FALSE); - } - } + $values = $values instanceof EntityInterface ? array('entity' => $values) : array('target_id' => $values); } - else { - // Make sure that the 'entity' property gets set as 'target_id'. - if (isset($values['entity'])) { - $values['target_id'] = $values['entity']->isNew() ? self::NEW_ENTITY_MARKER : $values['entity']->id(); - } - elseif (isset($values['target_id']) && $values['target_id'] !== static::NEW_ENTITY_MARKER) { - $values['entity'] = $values['target_id']; + // Make sure that the 'entity' property gets set as 'target_id'. + if (!isset($values['entity']) && isset($values['target_id'])) { + $values['entity'] = $values['target_id']; + } + + // Update any existing property objects. + $this->values = $values; + foreach ($this->properties as $name => $property) { + $value = NULL; + if (isset($values[$name])) { + $value = $values[$name]; } - parent::setValue($values, $notify); + $this->writePropertyValue($name, $value); + unset($this->values[$name]); + } + + // Notify the parent of any changes. + if ($notify && isset($this->parent)) { + $this->parent->onChange($this->name); } } @@ -178,9 +179,7 @@ public function set($property_name, $value, $notify = TRUE) { parent::set($property_name, $value, $notify); if (!$notify && $property_name == 'entity') { - $entity = $this->properties['entity']->getTarget()->getValue(); - $target_id = $entity->isNew() ? static::NEW_ENTITY_MARKER : $entity->id(); - $this->set('target_id', $target_id, FALSE); + $this->writePropertyValue($property_name, $value); } } @@ -204,19 +203,35 @@ public function getValue() { public function onChange($property_name) { // Make sure that the target ID and the target property stay in sync. if ($property_name == 'target_id') { - if ($this->target_id !== static::NEW_ENTITY_MARKER) { - $this->properties['entity']->setValue($this->target_id, FALSE); - } + $this->writePropertyValue($property_name, $this->target_id); } elseif ($property_name == 'entity') { - $entity = $this->properties['entity']->getTarget()->getValue(); - $target_id = $entity->isNew() ? static::NEW_ENTITY_MARKER : $entity->id(); - $this->set('target_id', $target_id, FALSE); + $this->writePropertyValue($property_name, $this->properties['entity']->getTarget()->getValue()); } parent::onChange($property_name); } /** + * Writes the value of a property without handling changes. + * + * @param string $property_name + * The name of the property to be written. + * @param $value + * The value to set. + */ + protected function writePropertyValue($property_name, $value) { + if ($property_name == 'entity') { + parent::set('entity', $value, FALSE); + $target_id = $value instanceof EntityInterface && $value->isNew() ? static::NEW_ENTITY_MARKER : $this->properties['entity']->getTargetIdentifier(); + parent::set('target_id', $target_id, FALSE); + } + elseif ($property_name == 'target_id' && $value !== static::NEW_ENTITY_MARKER) { + parent::set('target_id', $value, FALSE); + parent::set('entity', $value, FALSE); + } + } + + /** * {@inheritdoc} */ public function isEmpty() {