You are here

public function ConfigEntityRevisionsRevisionStorageHandler::deleteSingleRevision in Config Entity Revisions 8.2

Delete a single revision.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $revision: The revision to be deleted.

Overrides ConfigEntityRevisionsRevisionStorageHandlerInterface::deleteSingleRevision

File

src/Entity/Handler/ConfigEntityRevisionsRevisionStorageHandler.php, line 508

Class

ConfigEntityRevisionsRevisionStorageHandler
Class ConfigEntityRevisionsRevisionStorageHandler.

Namespace

Drupal\config_entity_revisions\Entity\Handler

Code

public function deleteSingleRevision(ContentEntityInterface $revision) {
  $was_default = $revision
    ->isDefaultRevision();
  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.
    $revisions = $this
      ->getRevisionIds($revision
      ->id());
    $revision_to_use = $revisions[0] == $revision
      ->getRevisionId() ? $revisions[1] : $revisions[0];
    $new_default = $this->entityTypeManager
      ->getStorage('config_entity_revisions')
      ->loadRevision($revision_to_use);
    $new_default
      ->enforceIsNew(FALSE);
    $new_default
      ->isDefaultRevision(TRUE);
    $new_default
      ->save();
    $content_moderation_state_storage = $this->entityTypeManager
      ->getStorage('content_moderation_state');
    if ($content_moderation_state_storage) {
      $content_moderation_ids = $content_moderation_state_storage
        ->getQuery()
        ->allRevisions()
        ->condition('content_entity_type_id', 'config_entity_revisions')
        ->condition('content_entity_revision_id', $revision_to_use)
        ->execute();

      // Revision default is whether the revision WAS default when created.
      // The content moderation module doesn't provide a method to update the
      // default revision so we need to do a direct update query and clear
      // caches.
      $target_entity_type = $this->entityTypeManager
        ->getDefinition('content_moderation_state');
      $this->connection
        ->update($target_entity_type
        ->getBaseTable())
        ->condition('id', array_values($content_moderation_ids)[0])
        ->fields([
        'revision_id' => array_keys($content_moderation_ids)[0],
      ])
        ->execute();
      $content_moderation_state_storage
        ->resetCache();
    }
  }
  $this->entityTypeManager
    ->getStorage('config_entity_revisions')
    ->deleteRevision($revision
    ->getRevisionId());
}