EntityStorageBase::doPreSave

protected EntityStorageBase::doPreSave(EntityInterface $entity)

Performs presave entity processing.

Parameters

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

Return value

int|string The processed entity identifier.

Throws

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

File

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

Class

EntityStorageBase
A base entity storage class.

Namespace

Drupal\Core\Entity

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.