function ContentEntityStorageBase::doPreSave

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php \Drupal\Core\Entity\ContentEntityStorageBase::doPreSave()
  2. 8.9.x core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php \Drupal\Core\Entity\ContentEntityStorageBase::doPreSave()
  3. 11.x core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php \Drupal\Core\Entity\ContentEntityStorageBase::doPreSave()

Performs presave entity processing.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The saved entity.

Return value

int|string|null The processed entity identifier, or null for new entities.

Overrides EntityStorageBase::doPreSave

File

core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php, line 740

Class

ContentEntityStorageBase
Base class for content entity storage handlers.

Namespace

Drupal\Core\Entity

Code

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();
  if ($entity->getEntityType()
    ->isRevisionable() && !$entity->isNew() && empty($entity->getLoadedRevisionId())) {
    // Update the loaded revision id for rare special cases when no loaded
    // revision is given when updating an existing entity. This for example
    // happens when calling save() in hook_entity_insert().
    $entity->updateLoadedRevisionId();
  }
  $id = parent::doPreSave($entity);
  if (!$entity->isNew()) {
    // If the ID changed then original can't be loaded, throw an exception
    // in that case.
    if (empty($entity->original) || $entity->id() != $entity->original
      ->id()) {
      throw new EntityStorageException("Update existing '{$this->entityTypeId}' entity while changing the ID is not supported.");
    }
    // Do not allow changing the revision ID when resaving the current
    // revision.
    if (!$entity->isNewRevision() && $entity->getRevisionId() != $entity->getLoadedRevisionId()) {
      throw new EntityStorageException("Update existing '{$this->entityTypeId}' entity revision while changing the revision ID is not supported.");
    }
  }
  return $id;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.