Saves an entity revision.
Drupal\Core\Entity\EntityInterface $entity: The entity object.
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];
}