You are here

public function RecurRuleUpdate::processItem in Smart Date 3.x

Same name and namespace in other branches
  1. 8.2 modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate::processItem()
  2. 3.0.x modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate::processItem()
  3. 3.1.x modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate::processItem()
  4. 3.2.x modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate::processItem()
  5. 3.3.x modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate::processItem()
  6. 3.4.x modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php \Drupal\smart_date_recur\Plugin\QueueWorker\RecurRuleUpdate::processItem()

Works on a single queue item.

Parameters

mixed $data: The data that was passed to \Drupal\Core\Queue\QueueInterface::createItem() when the item was queued.

Throws

\Drupal\Core\Queue\RequeueException Processing is not yet finished. This will allow another process to claim the item immediately.

\Exception A QueueWorker plugin may throw an exception to indicate there was a problem. The cron process will log the exception, and leave the item in the queue to be processed again later.

\Drupal\Core\Queue\SuspendQueueException More specifically, a SuspendQueueException should be thrown when a QueueWorker plugin is aware that the problem will affect all subsequent workers of its queue. For example, a callback that makes HTTP requests may find that the remote server is not responding. The cron process will behave as with a normal Exception, and in addition will not attempt to process further items from the current item's queue during the current cron run.

Overrides QueueWorkerInterface::processItem

See also

\Drupal\Core\Cron::processQueues()

File

modules/smart_date_recur/src/Plugin/QueueWorker/RecurRuleUpdate.php, line 22

Class

RecurRuleUpdate
Updates a rule's instances.

Namespace

Drupal\smart_date_recur\Plugin\QueueWorker

Code

public function processItem($item) {

  // If we don't have rules or an entity, there's nothing to do.
  if (empty($item->data) || empty($item->entity_id)) {
    return;
  }
  $entity_manager = \Drupal::entityTypeManager($item->entity_type);
  $entity_storage = $entity_manager
    ->getStorage($item->entity_type);
  $entity = $entity_storage
    ->load($item->entity_id);

  // If we can't find the entity, there's nothing to do.
  if (empty($entity)) {
    return;
  }
  $rules_processed = [];
  foreach ($item->data as $field_name => $rules) {
    $field_values = $entity
      ->get($field_name)
      ->getValue();
    $processed = [];

    // Go through identified rules to see if new instances are needed.
    foreach ($rules as $rrid) {
      $rule = SmartDateRule::load($rrid);
      $new_instances = $rule
        ->getNewInstances()
        ->toArray();
      if (empty($new_instances)) {

        // No instances to add, so no need to process this rule.
        unset($rules[$rrid]);
        continue;
      }
      $instances = $rule
        ->getStoredInstances();
      $template = end($instances);
      foreach ($new_instances as $new_instance) {
        $template['value'] = $new_instance
          ->getStart()
          ->getTimestamp();
        $template['end_value'] = $new_instance
          ->getEnd()
          ->getTimestamp();
        $instances[] = $template;
      }

      // TODO: check for expired instances. Possible to keep indexes the same?
      $rule
        ->set('instances', [
        'data' => $instances,
      ]);
      $rule
        ->save();
      $rules[$rrid] = $instances;
    }
    foreach ($field_values as $delta => $row) {

      // Skip if this instance isn't in a rule or in one we've ruled out.
      if (empty($row['rrule']) || !isset($rules[$row['rrule']])) {

        // Add directly to our array.
        $processed[] = $row;
        continue;
      }
      if (isset($rules_processed[$row['rrule']])) {

        // Already handled this rule, so skip this row.
        continue;
      }
      $instances = $rules[$row['rrule']];
      foreach ($instances as $rrule_index => $instance) {
        $row['value'] = $instance['value'];
        $row['end_value'] = $instance['end_value'];
        $row['rrule_index'] = $rrule_index;
        $processed[] = $row;
      }

      // Track that this rule has been processed.
      $rules_processed[$row['rrule']] = $row['rrule'];
    }

    // Update the entity with our new values.
    $entity
      ->set($field_name, $processed);
  }
  if (!empty($rules_processed)) {
    $entity
      ->save();
  }
}