You are here

function _batch_page in Drupal 8

Same name and namespace in other branches
  1. 6 includes/batch.inc \_batch_page()
  2. 7 includes/batch.inc \_batch_page()
  3. 9 core/includes/batch.inc \_batch_page()
  4. 10 core/includes/batch.inc \_batch_page()

Renders the batch processing page based on the current state of the batch.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object.

See also

_batch_shutdown()

4 calls to _batch_page()
authorize.php in core/authorize.php
Administrative script for running authorized file operations.
BatchController::batchPage in core/modules/system/src/Controller/BatchController.php
Returns a system batch page.
DbUpdateController::handle in core/modules/system/src/Controller/DbUpdateController.php
Returns a database update page.
install_run_task in core/includes/install.core.inc
Runs an individual installation task.

File

core/includes/batch.inc, line 34
Batch processing API for processes to run in multiple HTTP requests.

Code

function _batch_page(Request $request) {
  $batch =& batch_get();
  if (!($request_id = $request->query
    ->get('id'))) {
    return FALSE;
  }

  // Retrieve the current state of the batch.
  if (!$batch) {
    $batch = \Drupal::service('batch.storage')
      ->load($request_id);
    if (!$batch) {
      \Drupal::messenger()
        ->addError(t('No active batch.'));
      return new RedirectResponse(Url::fromRoute('<front>', [], [
        'absolute' => TRUE,
      ])
        ->toString());
    }
  }

  // We need to store the updated batch information in the batch storage after
  // processing the batch. In order for the error page to work correctly this
  // needs to be done even in case of a PHP fatal error in which case the end of
  // this function is never reached. Therefore we register a shutdown function
  // to handle this case. Because with FastCGI and fastcgi_finish_request()
  // shutdown functions are called after the HTTP connection is closed, updating
  // the batch information in a shutdown function would lead to race conditions
  // between consecutive requests if the batch processing continues. In case of
  // a fatal error the processing stops anyway, so it works even with FastCGI.
  // However, we must ensure to only update in the shutdown phase in this
  // particular case we track whether the batch information still needs to be
  // updated.
  // @see _batch_shutdown()
  // @see \Symfony\Component\HttpFoundation\Response::send()
  drupal_register_shutdown_function('_batch_shutdown');
  _batch_needs_update(TRUE);
  $build = [];

  // Add batch-specific libraries.
  foreach ($batch['sets'] as $batch_set) {
    if (isset($batch_set['library'])) {
      foreach ($batch_set['library'] as $library) {
        $build['#attached']['library'][] = $library;
      }
    }
  }
  $op = $request->query
    ->get('op', '');
  switch ($op) {
    case 'start':
    case 'do_nojs':

      // Display the full progress page on startup and on each additional
      // non-JavaScript iteration.
      $current_set = _batch_current_set();
      $build['#title'] = $current_set['title'];
      $build['content'] = _batch_progress_page();
      $response = $build;
      break;
    case 'do':

      // JavaScript-based progress page callback.
      $response = _batch_do();
      break;
    case 'finished':

      // _batch_finished() returns a RedirectResponse.
      $response = _batch_finished();
      break;
  }
  if ($batch) {
    \Drupal::service('batch.storage')
      ->update($batch);
  }
  _batch_needs_update(FALSE);
  return $response;
}