function EntityStorageBase::doPreSave

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityStorageBase.php \Drupal\Core\Entity\EntityStorageBase::doPreSave()
  2. 8.9.x core/lib/Drupal/Core/Entity/EntityStorageBase.php \Drupal\Core\Entity\EntityStorageBase::doPreSave()
  3. 11.x core/lib/Drupal/Core/Entity/EntityStorageBase.php \Drupal\Core\Entity\EntityStorageBase::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.

Throws

\Drupal\Core\Entity\EntityStorageException If the entity identifier is invalid.

2 calls to EntityStorageBase::doPreSave()
ContentEntityStorageBase::doPreSave in core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php
Performs presave entity processing.
EntityStorageBase::save in core/lib/Drupal/Core/Entity/EntityStorageBase.php
Saves the entity permanently.
1 method overrides EntityStorageBase::doPreSave()
ContentEntityStorageBase::doPreSave in core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php
Performs presave entity processing.

File

core/lib/Drupal/Core/Entity/EntityStorageBase.php, line 506

Class

EntityStorageBase
A base entity storage class.

Namespace

Drupal\Core\Entity

Code

protected function doPreSave(EntityInterface $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);
    }
    // Allow code to run before saving.
    $entity->preSave($this);
    $this->invokeHook('presave', $entity);
    return $id;
}

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