You are here

function views_natural_sort_rebuild_index in Views Natural Sort 8.2

Same name and namespace in other branches
  1. 6 views_natural_sort.admin.inc \views_natural_sort_rebuild_index()
  2. 7.2 views_natural_sort.admin.inc \views_natural_sort_rebuild_index()
  3. 7 views_natural_sort.admin.inc \views_natural_sort_rebuild_index()
1 string reference to 'views_natural_sort_rebuild_index'
views_natural_sort_queue_data_for_rebuild in ./views_natural_sort.module
Views_natural_sort_queue_data_for_rebuild description.

File

./views_natural_sort.module, line 174
Contains views_natural_sort.module..

Code

function views_natural_sort_rebuild_index($queue_name, &$context) {

  /** @var QueueInterface $queue */
  $queue = \Drupal::queue($queue_name);

  /** @var QueueWorkerInterface $queue_worker */
  $queue_worker = \Drupal::service('plugin.manager.queue_worker')
    ->createInstance($queue_name);
  $config = \Drupal::configFactory()
    ->get('views_natural_sort.settings');

  // Alias sandbox for easier referencing.
  $sandbox =& $context['sandbox'];

  // Alias results for easier referencing.
  $results =& $context['results'];
  if (empty($sandbox)) {
    $sandbox['current'] = 0;
    $sandbox['max'] = $queue
      ->numberOfItems();
    $sandbox['items_per_batch'] = $config
      ->get('rebuild_items_per_batch');
  }
  for ($i = 0; $i < $sandbox['items_per_batch'] && $sandbox['current'] < $sandbox['max']; $i++) {
    if ($item = $queue
      ->claimItem(10)) {
      try {
        $queue_worker
          ->processItem($item->data);
        $queue
          ->deleteItem($item);
      } catch (SuspendQueueException $e) {
        $queue
          ->releaseItem($item);
        break;
      } catch (\Exception $e) {
        watchdog_exception('views_natural_sort', $e);
      }
    }
    $sandbox['current']++;
  }
  $results['entries'] = $sandbox['current'];
  if ($sandbox['current'] != $sandbox['max']) {
    $context['finished'] = $sandbox['current'] / $sandbox['max'];
    $context['message'] = t('Processed %current out of %max records.', [
      '%current' => $sandbox['current'],
      '%max' => $sandbox['max'],
    ]);
  }
}