You are here

public static function NodeRevisionDeleteBatch::deleteRevision in Node Revision Delete 8

Delete revision.

Once the revision is deleted the context is updated with the total number of revisions deleted and the node object.

Parameters

\Drupal\node\Entity\Node|int $revision: The revision to delete.

bool $dry_run: Indicate if we need to delete or not the revision. TRUE for test purpose FALSE to delete the revision.

mixed $context: The context of the current batch.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/NodeRevisionDeleteBatch.php, line 29

Class

NodeRevisionDeleteBatch
Methods for delete revisions in a batch.

Namespace

Drupal\node_revision_delete

Code

public static function deleteRevision($revision, $dry_run, &$context) {
  if (empty($context['results'])) {
    $context['results']['revisions'] = 0;
    if ($revision instanceof Node) {

      // Update context of the current node.
      $context['results']['node'] = $revision;
    }
  }
  if ($revision instanceof Node) {
    $revision = $revision
      ->getRevisionId();
  }

  // Checking if this is a dry run or we really need to delete the variable.
  if (!$dry_run) {

    // Delete the revision.
    \Drupal::entityTypeManager()
      ->getStorage('node')
      ->deleteRevision($revision);
  }

  // Count the number of revisions deleted.
  $context['results']['revisions']++;

  // Adding a message for the actual revision being deleted.
  $context['message'] = t('Processing revision: @id', [
    '@id' => $revision,
  ]);
}