You are here

function smart_date_recur_cron in Smart Date 3.1.x

Same name and namespace in other branches
  1. 8.2 modules/smart_date_recur/smart_date_recur.module \smart_date_recur_cron()
  2. 3.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_cron()
  3. 3.0.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_cron()
  4. 3.2.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_cron()
  5. 3.3.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_cron()
  6. 3.4.x modules/smart_date_recur/smart_date_recur.module \smart_date_recur_cron()

Implements hook_cron().

Queues rules without defined limits to have more instances generated.

File

modules/smart_date_recur/smart_date_recur.module, line 739
Field hooks for a field that stores a start and end date as timestamps.

Code

function smart_date_recur_cron() {

  // Check a time variable to control how often this runs.
  $time_check = \Drupal::state()
    ->get('smart_date_recur_check');
  if ($time_check && $time_check > time()) {

    // Wait until a week has lapsed since the last check.
    return;
  }

  /** @var QueueFactory $queue_factory */
  $queue_factory = \Drupal::service('queue');

  /** @var QueueInterface $queue */
  $queue = $queue_factory
    ->get('smart_date_recur_rules');
  $queue
    ->createQueue();
  $to_process = [];

  // Get the data we need, and group it by impacted entity.
  $ids = \Drupal::entityTypeManager()
    ->getStorage('smart_date_rule')
    ->getRuleIdsToCheck();
  foreach (SmartDateRule::loadMultiple($ids) as $rule) {
    $entity_type = $rule->entity_type
      ->getString();

    // $bundle = $rule->bundle->getString();
    $entity_id = $rule
      ->getParentEntity(TRUE);
    $field_name = $rule->field_name
      ->getString();
    $rrid = $rule
      ->id();

    // Group the collected data by impacted entity.
    $to_process[$entity_type][$entity_id][$field_name][$rrid] = $rrid;
  }
  foreach ($to_process as $entity_type => $type_items) {
    foreach ($type_items as $entity_id => $data) {

      // Create new queue item.
      $item = new \stdClass();
      $item->entity_type = $entity_type;
      $item->entity_id = $entity_id;
      $item->data = $data;

      // Add our item to the queue.
      $queue
        ->createItem($item);
    }
  }

  // Set the time to next extend instances.
  $wait = "+7 days";
  \Drupal::state()
    ->set('smart_date_recur_check', strtotime($wait));
}