NodeRevisionGenerateBatch.php in Node Revision Delete 8
File
modules/node_revision_generate/src/NodeRevisionGenerateBatch.php
View source
<?php
namespace Drupal\node_revision_generate;
class NodeRevisionGenerateBatch {
public static function generateRevisions($nid, $revision_date, &$context) {
if (empty($context['results'])) {
$context['results']['revisions'] = 0;
}
$node_entity = \Drupal::entityTypeManager()
->getStorage('node')
->load($nid);
$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());
$node_entity
->setRevisionTranslationAffected(TRUE);
$node_entity
->save();
$context['results']['revisions']++;
}
public static function finish($success, array $results, array $operations) {
$messenger = \Drupal::messenger();
$logger = \Drupal::logger('node_revision_generate');
if ($success) {
$logger
->notice(t('@total revisions generated for nodes', [
'@total' => $results['revisions'],
]));
$success_message = \Drupal::translation()
->formatPlural($results['revisions'], 'One revision has been created for the selected content types.', 'A total of @count revisions were created for the selected content types.', [
'@total' => $results['revisions'],
]);
$messenger
->addMessage($success_message);
$messenger
->addWarning(t('Revisions were generated up to the current date, no revisions were generated with a date in the future. So, depending on this maybe we will not generate the number of revisions you expect.'));
}
else {
$error_operation = reset($operations);
$message = t('An error occurred while processing %error_operation with arguments: @arguments', [
'%error_operation' => $error_operation[0],
'@arguments' => print_r($error_operation[1], TRUE),
]);
$logger
->error($message);
$messenger
->addError($message);
}
}
}