You are here

function node_revision_delete_cron in Node Revision Delete 8

Same name and namespace in other branches
  1. 7.3 node_revision_delete.module \node_revision_delete_cron()
  2. 7 node_revision_delete.module \node_revision_delete_cron()
  3. 7.2 node_revision_delete.module \node_revision_delete_cron()

Implements hook_cron().

5 string references to 'node_revision_delete_cron'
AdminSettingsForm::buildForm in src/Form/AdminSettingsForm.php
Form constructor.
DefaultConfigurationTest::testDefaultConfigurationValues in tests/src/Kernel/DefaultConfigurationTest.php
Tests the default configuration values.
drush_node_revision_delete_nrd_delete_cron_run in ./node_revision_delete.drush.inc
Callback for the nrd-delete-cron-run command.
NodeRevisionDeleteAdminSettingsTest::testConfigurationForm in tests/src/Functional/NodeRevisionDeleteAdminSettingsTest.php
Tests the configuration form, the permission and the link.
NodeRevisionDeleteCommands::deleteCronRun in src/Commands/NodeRevisionDeleteCommands.php
Configures how many revisions delete per cron run.

File

./node_revision_delete.module, line 296
Contains node_revision_delete.module.

Code

function node_revision_delete_cron() {

  // Getting the config.
  $config = \Drupal::config('node_revision_delete.settings');

  // Getting the frequency to run cron.
  $period = $config
    ->get('node_revision_delete_time');

  // Getting the last execute.
  $last_execute = \Drupal::state()
    ->get('node_revision_delete.last_execute', 0);

  // Getting the current time.
  $current_time = \Drupal::time()
    ->getRequestTime();
  if ($period >= 0 && $current_time - $last_execute > $period) {

    // Getting the the maximum number of revisions to process.
    $revisions_to_process = $config
      ->get('node_revision_delete_cron');

    // Getting the revisions to delete.
    $revisions = \Drupal::service('node_revision_delete')
      ->getCandidatesRevisionsByNumber($revisions_to_process);

    // Checking if we have revisions to delete.
    if (count($revisions)) {

      // Getting the node storage.
      $node_storage = \Drupal::entityTypeManager()
        ->getStorage('node');

      // Delete the revisions.
      foreach ($revisions as $revision) {
        $node_storage
          ->deleteRevision($revision);
      }

      // The node_revision_delete.last_execute state variable stores the last
      // time a revision was deleted once cron run.
      \Drupal::state()
        ->set('node_revision_delete.last_execute', $current_time);
    }
  }
}