You are here

public function ConfigEntityRevisionsControllerBase::deleteRevision in Config Entity Revisions 8

Same name and namespace in other branches
  1. 1.x src/ConfigEntityRevisionsControllerBase.php \Drupal\config_entity_revisions\ConfigEntityRevisionsControllerBase::deleteRevision()

Delete a single revision.

Parameters

ContentEntityInterface $revision: The revision to be deleted.

File

src/ConfigEntityRevisionsControllerBase.php, line 360

Class

ConfigEntityRevisionsControllerBase
Controller to make library functions available to various consumers.

Namespace

Drupal\config_entity_revisions

Code

public function deleteRevision($revision) {
  $was_default = $revision
    ->isDefaultRevision();
  $revisions = $this
    ->getRevisionIds($revision
    ->id());
  if ($was_default) {

    // Change the default to the next newer (if we're deleting the default,
    // there must be no published revisions so it doesn't matter which we
    // choose. Ensure revision_default isn't set on our revision in
    // config_entity_revisions_revision - $was_default can return FALSE
    // even when that value is 1, and that will cause the content moderation
    // module (which does look at that field) to throw an exception.
    $this->connection
      ->update("config_entity_revisions_revision")
      ->condition('id', $revision
      ->id())
      ->fields([
      'revision_default' => 0,
    ])
      ->execute();
    $revision_to_use = $revisions[0] == $this->revision
      ->getRevisionId() ? $revisions[1] : $revisions[0];
    $new_default = $this->configEntityRevisionsStorage
      ->loadRevision($revision_to_use);
    $new_default
      ->enforceIsNew(FALSE);
    $new_default
      ->isDefaultRevision(TRUE);
    $new_default
      ->save();
  }
  $this->entityTypeManager
    ->getStorage('config_entity_revisions')
    ->deleteRevision($revision
    ->getRevisionId());
}