You are here

public function ConfigEntityRevisionsConfigEntityTrait::setNewRevision in Config Entity Revisions 8.2

Enforces an entity to be saved as a new revision.

Parameters

bool $value: (optional) Whether a new revision should be saved.

Throws

\LogicException Thrown if the entity does not support revisions.

See also

\Drupal\Core\Entity\EntityInterface::isNewRevision()

File

src/ConfigEntityRevisionsConfigEntityTrait.php, line 330

Class

ConfigEntityRevisionsConfigEntityTrait
Trait ConfigEntityRevisionsConfigEntityTrait.

Namespace

Drupal\config_entity_revisions

Code

public function setNewRevision($value = TRUE) {
  if (!$this
    ->getEntityType()
    ->hasKey('revision')) {
    throw new \LogicException("Entity type {$this->getEntityTypeId()} does not support revisions.");
  }
  if ($value && !$this->newRevision) {

    // When saving a new revision, set any existing revision ID to NULL so as
    // to ensure that a new revision will actually be created.
    $this
      ->set($this
      ->getEntityType()
      ->getKey('revision'), NULL);
  }
  elseif (!$value && $this->newRevision) {

    // If ::setNewRevision(FALSE) is called after ::setNewRevision(TRUE) we
    // have to restore the loaded revision ID.
    $this
      ->set($this
      ->getEntityType()
      ->getKey('revision'), $this
      ->getLoadedRevisionId());
  }
  $this->newRevision = $value;
}