You are here

function background_batch_process_batch in Background Process 8

Same name and namespace in other branches
  1. 6 background_batch/background_batch.module \background_batch_process_batch()
  2. 7.2 background_batch/background_batch.module \background_batch_process_batch()
  3. 7 background_batch/background_batch.module \background_batch_process_batch()

Implements to Processes the batch.

File

background_batch/background_batch.module, line 178
This module adds background processing to Drupals batch API.

Code

function background_batch_process_batch($redirect = NULL, $url = 'batch', $redirect_callback = '') {
  $batch =& batch_get();
  if (isset($batch)) {

    // Add process information.
    $process_info = [
      'current_set' => 0,
      'progressive' => TRUE,
      'url' => $url,
      'url_options' => [],
      'source_url' => $_GET['q'],
      'redirect' => $redirect,
      'theme' => \Drupal::theme()
        ->getActiveTheme()
        ->getName(),
      'redirect_callback' => isset($redirect_callback) ? RedirectResponse('/admin/config/system/batch/overview') : '',
    ];
    $batch += $process_info;

    // The batch is now completely built.
    // Allow other modules to make changes.
    \Drupal::moduleHandler()
      ->alter('batch', $batch);

    // Assign an arbitrary id: don't rely
    // on a serial column in the 'batch'.
    $batch['id'] = db_next_id();

    // Move operations to a job queue.
    // Non-progressive batches will use a memory-based queue.
    foreach ($batch['sets'] as $key => $batch_set) {
      _batch_populate_queue($batch, $key);
    }
    $t = 't';
    $batch['error_message'] = $t('Please continue to <a href="@error_url">the error page</a>', [
      '@error_url' => Url::fromUri($url, [
        'query' => [
          'id' => $batch['id'],
          'op' => 'finished',
        ],
      ]),
    ]);
    if (isset($_GET['destination'])) {
      $batch['destination'] = $_GET['destination'];
      unset($_GET['destination']);
    }

    // Store the batch.
    db_insert('batch')
      ->fields([
      'bid' => $batch['id'],
      'timestamp' => REQUEST_TIME,
      'token' => drupal_get_token($batch['id']),
      'batch' => serialize($batch),
    ])
      ->execute();

    // Set the batch number in the session
    // to guarantee that it will stay alive.
    $_SESSION['batches'][$batch['id']] = TRUE;

    // Redirect for processing.
    $function = $batch['redirect_callback'];
    return $function;
  }
  background_process_start('_background_batch_process_callback', $batch);
}