You are here

public static function NodeRevisionGenerateBatch::generateRevisions in Node Revision Delete 8

Implements callback_batch_operation().

Generate revision for the node with $nid.

Parameters

int $nid: The node ID.

int $revision_date: Date to create the revision for the $nid node.

mixed $context: An array of contextual key/values.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

modules/node_revision_generate/src/NodeRevisionGenerateBatch.php, line 26

Class

NodeRevisionGenerateBatch
Methods for generate revisions in a batch.

Namespace

Drupal\node_revision_generate

Code

public static function generateRevisions($nid, $revision_date, &$context) {
  if (empty($context['results'])) {
    $context['results']['revisions'] = 0;
  }

  // Load the node to generate revision.

  /** @var \Drupal\node\NodeInterface $node_entity */
  $node_entity = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->load($nid);

  // Generate revision.
  $node_entity
    ->setNewRevision();
  $node_entity
    ->setRevisionCreationTime($revision_date);
  $node_entity
    ->setRevisionLogMessage(t('Revision generated by Node Revision Generate module.'));
  $node_entity
    ->setRevisionUserId(\Drupal::currentUser()
    ->id());

  // Calling this function let show revision in the user interface.
  $node_entity
    ->setRevisionTranslationAffected(TRUE);
  $node_entity
    ->save();

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