diff --git a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php index 04a6d8c..ba54f66 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php @@ -289,18 +289,39 @@ protected function doSave($id, EntityInterface $entity) { */ protected function doPreSave(EntityInterface $entity) { /** @var \Drupal\Core\Entity\ContentEntityBase $entity */ - // Sync the changes made in the fields array to the internal values array. $entity->updateOriginalValues(); - $return = parent::doPreSave($entity); + $id = $entity->id(); + + // Track the original ID. + if ($entity->getOriginalId() !== NULL) { + $id = $entity->getOriginalId(); + } + + // Track if this entity exists already. + $id_exists = $this->has($id, $entity); + + // A new entity should not already exist. + if ($id_exists && $entity->isNew()) { + throw new EntityStorageException("'{$this->entityTypeId}' entity with ID '$id' already exists."); + } + + // Load the original entity, if any. + if ($id_exists && !isset($entity->original)) { + $entity->original = $this->loadUnchanged($id); + } // Reset the persistent cache if this entity type uses it. if (!$entity->isNew() && $this->entityType->isPersistentlyCacheable()) { $this->cacheBackend->delete($this->buildCacheId($entity->id())); } - return $return; + // Allow code to run before saving. + $entity->preSave($this); + $this->invokeHook('presave', $entity); + + return $id; } /**