You are here

public function SqlContentEntityStorage::restore in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::restore()

Restores a previously saved entity.

Note that the entity is assumed to be in a valid state for the storage, so the restore process does not invoke any hooks, nor does it perform any pre or post-save operations.

@internal This method should never be used to perform a regular entity save. Its only use-case is to assist updating entity types when there are complex schema changes, for example, to make them revisionable. Note that overriding this method to fix data prior to restoring is a likely sign that the current data is corrupt.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to restore.

Throws

\Drupal\Core\Entity\EntityStorageException In case of failures, an exception is thrown.

Overrides EntityStorageBase::restore

File

core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php, line 817

Class

SqlContentEntityStorage
A content entity database storage implementation.

Namespace

Drupal\Core\Entity\Sql

Code

public function restore(EntityInterface $entity) {
  $transaction = $this->database
    ->startTransaction();
  try {

    // Insert the entity data in the base and data tables only for default
    // revisions.

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    if ($entity
      ->isDefaultRevision()) {
      $record = $this
        ->mapToStorageRecord($entity
        ->getUntranslated(), $this->baseTable);
      $this->database
        ->insert($this->baseTable)
        ->fields((array) $record)
        ->execute();
      if ($this->dataTable) {
        $this
          ->saveToSharedTables($entity);
      }
    }

    // Insert the entity data in the revision and revision data tables.
    if ($this->revisionTable) {
      $record = $this
        ->mapToStorageRecord($entity
        ->getUntranslated(), $this->revisionTable);
      $this->database
        ->insert($this->revisionTable)
        ->fields((array) $record)
        ->execute();
      if ($this->revisionDataTable) {
        $this
          ->saveToSharedTables($entity, $this->revisionDataTable);
      }
    }

    // Insert the entity data in the dedicated tables.
    $this
      ->saveToDedicatedTables($entity, FALSE, []);

    // Ignore replica server temporarily.
    \Drupal::service('database.replica_kill_switch')
      ->trigger();
  } catch (\Exception $e) {
    $transaction
      ->rollBack();
    watchdog_exception($this->entityTypeId, $e);
    throw new EntityStorageException($e
      ->getMessage(), $e
      ->getCode(), $e);
  }
}