W3cubDocs

/Drupal 8

protected function SqlContentEntityStorage::saveRevision

protected SqlContentEntityStorage::saveRevision(ContentEntityInterface $entity)

Saves an entity revision.

Parameters

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

Return value

int The revision id.

File

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

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()) {
    $insert_id = $this->database
      ->insert($this->revisionTable, array('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->entityType->getBaseTable())
        ->fields(array($this->revisionKey => $record->{$this->revisionKey}))
        ->condition($this->idKey, $record->{$this->idKey})
        ->execute();
    }
  }
  else {
    $this->database
      ->update($this->revisionTable)
      ->fields((array) $record)
      ->condition($this->revisionKey, $record->{$this->revisionKey})
      ->execute();
  }

  // Make sure to update the new revision key for the entity.
  $entity->{$this->revisionKey}->value = $record->{$this->revisionKey};

  return $record->{$this->revisionKey};
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Sql!SqlContentEntityStorage.php/function/SqlContentEntityStorage::saveRevision/8.1.x