protected function DatabaseStorageControllerNG::saveRevision

Saves an entity revision.

Parameters

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

Return value

integer The revision id.

Overrides DatabaseStorageController::saveRevision

1 call to DatabaseStorageControllerNG::saveRevision()
DatabaseStorageControllerNG::save in drupal/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php
Overrides DatabaseStorageController::save().

File

drupal/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php, line 423
Contains \Drupal\Core\Entity\DatabaseStorageControllerNG.

Class

DatabaseStorageControllerNG
Implements Field API specific enhancements to the DatabaseStorageController class.

Namespace

Drupal\Core\Entity

Code

protected function saveRevision(EntityInterface $entity) {
  $return = $entity
    ->id();
  $default_langcode = $entity
    ->language()->langcode;
  if (!$entity
    ->isNewRevision()) {

    // Delete to handle removed values.
    $this->database
      ->delete($this->revisionTable)
      ->condition($this->idKey, $entity
      ->id())
      ->condition($this->revisionKey, $entity
      ->getRevisionId())
      ->execute();
  }
  $languages = $this->dataTable ? $entity
    ->getTranslationLanguages(TRUE) : array(
    $default_langcode => $entity
      ->language(),
  );
  foreach ($languages as $langcode => $language) {
    $translation = $entity
      ->getTranslation($langcode, FALSE);
    $record = $this
      ->mapToRevisionStorageRecord($translation);
    $record->langcode = $langcode;
    $record->default_langcode = $langcode == $default_langcode;

    // 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() && isset($record->{$this->revisionKey})) {
      $record->{$this->revisionKey} = NULL;
    }
    $this
      ->preSaveRevision($record, $entity);
    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, $record->{$this->idKey})
          ->execute();
      }
      $entity
        ->setNewRevision(FALSE);
    }
    else {

      // @todo Use multiple insertions to improve performance.
      drupal_write_record($this->revisionTable, $record);
    }

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