function SqlContentEntityStorage::saveRevision

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::saveRevision()
  2. 8.9.x core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::saveRevision()
  3. 11.x core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php \Drupal\Core\Entity\Sql\SqlContentEntityStorage::saveRevision()

Saves an entity revision.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity object.

Return value

int The revision id.

1 call to SqlContentEntityStorage::saveRevision()
SqlContentEntityStorage::doSaveFieldItems in core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php
Writes entity field values to the storage.

File

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

Class

SqlContentEntityStorage
A content entity database storage implementation.

Namespace

Drupal\Core\Entity\Sql

Code

protected function saveRevision(ContentEntityInterface $entity) {
    $record = $this->mapToStorageRecord($entity->getUntranslated(), $this->revisionTable);
    $entity->preSaveRevision($this, $record);
    if ($entity->isNewRevision()) {
        // @todo Remove the 'return' option in Drupal 11.
        // @see https://www.drupal.org/project/drupal/issues/3256524
        $insert_id = $this->database
            ->insert($this->revisionTable, [
            'return' => Database::RETURN_INSERT_ID,
        ])
            ->fields((array) $record)
            ->execute();
        // Even if this is a new revision, the revision ID key might have been
        // set in which case we should not override the provided revision ID.
        if (!isset($record->{$this->revisionKey})) {
            $record->{$this->revisionKey} = $insert_id;
        }
        if ($entity->isDefaultRevision()) {
            $this->database
                ->update($this->baseTable)
                ->fields([
                $this->revisionKey => $record->{$this->revisionKey},
            ])
                ->condition($this->idKey, $record->{$this->idKey})
                ->execute();
        }
        // Make sure to update the new revision key for the entity.
        $entity->{$this->revisionKey}->value = $record->{$this->revisionKey};
    }
    else {
        // Remove the revision ID from the record to enable updates on SQL
        // variants that prevent updating serial columns, for example,
        // mssql.
        unset($record->{$this->revisionKey});
        $this->database
            ->update($this->revisionTable)
            ->fields((array) $record)
            ->condition($this->revisionKey, $entity->getRevisionId())
            ->execute();
    }
    return $entity->getRevisionId();
}

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