You are here

function _batch_progress_page in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/includes/batch.inc \_batch_progress_page()

Outputs a batch processing page.

See also

_batch_process()

1 call to _batch_progress_page()
_batch_page in core/includes/batch.inc
Renders the batch processing page based on the current state of the batch.

File

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

Code

function _batch_progress_page() {
  $batch =& batch_get();
  $current_set = _batch_current_set();
  $new_op = 'do_nojs';
  if (!isset($batch['running'])) {

    // This is the first page so we return some output immediately.
    $percentage = 0;
    $message = $current_set['init_message'];
    $label = '';
    $batch['running'] = TRUE;
  }
  else {

    // This is one of the later requests; do some processing first.
    // Error handling: if PHP dies due to a fatal error (e.g. a nonexistent
    // function), it will output whatever is in the output buffer, followed by
    // the error message.
    ob_start();
    $fallback = $current_set['error_message'] . '<br />' . $batch['error_message'];

    // We strip the end of the page using a marker in the template, so any
    // additional HTML output by PHP shows up inside the page rather than below
    // it. While this causes invalid HTML, the same would be true if we didn't,
    // as content is not allowed to appear after </html> anyway.
    $bare_html_page_renderer = \Drupal::service('bare_html_page_renderer');
    $response = $bare_html_page_renderer
      ->renderBarePage([
      '#markup' => $fallback,
    ], $current_set['title'], 'maintenance_page', array(
      '#show_messages' => FALSE,
    ));

    // Just use the content of the response.
    $fallback = $response
      ->getContent();
    list($fallback) = explode('<!--partial-->', $fallback);
    print $fallback;

    // Perform actual processing.
    list($percentage, $message, $label) = _batch_process($batch);
    if ($percentage == 100) {
      $new_op = 'finished';
    }

    // PHP did not die; remove the fallback output.
    ob_end_clean();
  }

  // Merge required query parameters for batch processing into those provided by
  // batch_set() or hook_batch_alter().
  $query_options = $batch['url']
    ->getOption('query');
  $query_options['id'] = $batch['id'];
  $query_options['op'] = $new_op;
  $batch['url']
    ->setOption('query', $query_options);
  $url = $batch['url']
    ->toString(TRUE)
    ->getGeneratedUrl();
  $build = array(
    '#theme' => 'progress_bar',
    '#percent' => $percentage,
    '#message' => array(
      '#markup' => $message,
    ),
    '#label' => $label,
    '#attached' => array(
      'html_head' => array(
        array(
          array(
            // Redirect through a 'Refresh' meta tag if JavaScript is disabled.
            '#tag' => 'meta',
            '#noscript' => TRUE,
            '#attributes' => array(
              'http-equiv' => 'Refresh',
              'content' => '0; URL=' . $url,
            ),
          ),
          'batch_progress_meta_refresh',
        ),
      ),
      // Adds JavaScript code and settings for clients where JavaScript is enabled.
      'drupalSettings' => [
        'batch' => [
          'errorMessage' => $current_set['error_message'] . '<br />' . $batch['error_message'],
          'initMessage' => $current_set['init_message'],
          'uri' => $url,
        ],
      ],
      'library' => array(
        'core/drupal.batch',
      ),
    ),
  );
  return $build;
}