protected function DatabaseStorageController::saveRevision

Saves an entity revision.

Parameters

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

1 call to DatabaseStorageController::saveRevision()
1 method overrides DatabaseStorageController::saveRevision()
DatabaseStorageControllerNG::saveRevision in drupal/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php
Saves an entity revision.

File

drupal/core/lib/Drupal/Core/Entity/DatabaseStorageController.php, line 496
Contains \Drupal\Core\Entity\DatabaseStorageController.

Class

DatabaseStorageController
Defines a base entity controller class.

Namespace

Drupal\Core\Entity

Code

protected function saveRevision(EntityInterface $entity) {

  // Convert the entity into an array as it might not have the same properties
  // as the entity, it is just a raw structure.
  $record = (array) $entity;

  // When saving a new revision, set any existing revision ID to NULL so as to
  // ensure that a new revision will actually be created.
  if ($entity
    ->isNewRevision() && $record[$this->revisionKey]) {
    $record[$this->revisionKey] = NULL;
  }

  // Cast to object as preSaveRevision() expects one to be compatible with the
  // upcoming NG storage controller.
  $record = (object) $record;
  $this
    ->preSaveRevision($record, $entity);
  $record = (array) $record;
  if ($entity
    ->isNewRevision()) {
    drupal_write_record($this->revisionTable, $record);
    if ($entity
      ->isDefaultRevision()) {
      $this->database
        ->update($this->entityInfo['base_table'])
        ->fields(array(
        $this->revisionKey => $record[$this->revisionKey],
      ))
        ->condition($this->idKey, $entity
        ->id())
        ->execute();
    }
    $entity
      ->setNewRevision(FALSE);
  }
  else {
    drupal_write_record($this->revisionTable, $record, $this->revisionKey);
  }

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