You are here

public function NodeRevisionDeleteCommands::deletePriorRevisions in Node Revision Delete 8

Delete all revisions prior to a revision.

@usage nrd-delete-prior-revisions 1 3 Delete all revisions prior to revision id 3 of node id 1. @command nrd:delete-prior-revisions @aliases nrd-dpr,nrd-delete-prior-revisions

Parameters

int $nid: The id of the node which revisions will be deleted.

int $vid: The revision id, all prior revisions to this revision will be deleted.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Commands/NodeRevisionDeleteCommands.php, line 359

Class

NodeRevisionDeleteCommands
Class NodeRevisionDeleteCommands.

Namespace

Drupal\node_revision_delete\Commands

Code

public function deletePriorRevisions($nid = 0, $vid = 0) {

  // Get list of prior revisions.
  $previousRevisions = $this->nodeRevisionDelete
    ->getPreviousRevisions($nid, $vid);
  if (count($previousRevisions) === 0) {
    $this
      ->writeln(dt('<error>No prior revision(s) found to delete.</error>'));
    return;
  }
  if ($this
    ->io()
    ->confirm(dt("Confirm deleting @count revision(s)?", [
    '@count' => count($previousRevisions),
  ]))) {

    // Check if current revision should be deleted, too.
    if ($this
      ->io()
      ->confirm(dt("Additionally, do you want to delete the revision @vid? @count revision(s) will be deleted.", [
      '@vid' => $vid,
      '@count' => count($previousRevisions) + 1,
    ]))) {
      $this->entityTypeManager
        ->getStorage('node')
        ->deleteRevision($vid);
    }
    foreach ($previousRevisions as $revision) {
      $this->entityTypeManager
        ->getStorage('node')
        ->deleteRevision($revision
        ->getRevisionId());
    }
  }
}