batch_process

Versions
6
batch_process($redirect = NULL, $url = NULL)
7
batch_process($redirect = NULL, $url = 'batch', $redirect_callback = 'drupal_goto')

Processes the batch.

Unless the batch has been marked with 'progressive' = FALSE, the function issues a drupal_goto and thus ends page execution.

This function is generally not needed in form submit handlers; Form API takes care of batches that were set during form submission.

Parameters

$redirect (optional) Path to redirect to when the batch has finished processing.

$url (optional - should only be used for separate scripts like update.php) URL of the batch processing page.

$redirect_callback (optional) Specify a function to be called to redirect to the progressive processing page. By default drupal_goto() will be used to redirect to a page which will do the progressive page. Specifying another function will allow the progressive processing to be processed differently.

Related topics

▾ 10 functions call batch_process()

batch_example_page in developer/examples/batch_example.module
drupal_process_form in includes/form.inc
Processes a form submission.
form_test_drupal_form_submit_batch_api in modules/simpletest/tests/form_test.module
Page callback for the batch/drupal_form_submit interaction test.
install_run_task in ./install.php
Run an individual installation task.
simpletest_run_tests in modules/simpletest/simpletest.module
Actually runs tests.
system_authorized_batch_process in modules/system/system.module
Use authorize.php to run batch_process().
system_test_batch_theme in modules/simpletest/tests/system_test.module
Menu callback; start a new batch for testing the batch progress page theme.
update_batch in includes/update.inc
Start the database update batch process.
update_manual_status in modules/update/update.fetch.inc
Callback to manually check the update status without cron.
user_cancel_confirm in modules/user/user.pages.inc
Menu callback; Cancel a user account via e-mail confirmation link.

Code

includes/form.inc, line 3102

<?php
function batch_process($redirect = NULL, $url = 'batch', $redirect_callback = 'drupal_goto') {
  $batch =& batch_get();

  drupal_theme_initialize();
  
  if (isset($batch)) {
    // Add process information
    $process_info = array(
      'current_set' => 0,
      'progressive' => TRUE,
      'url' => $url,
      'url_options' => array(),
      'source_url' => $_GET['q'],
      'redirect' => $redirect,
      'theme' => $GLOBALS['theme_key'],
      'redirect_callback' => $redirect_callback,
    );
    $batch += $process_info;

    // The batch is now completely built. Allow other modules to make changes to the 
    // batch so that it is easier to reuse batch processes in other enviroments.
    drupal_alter('batch', $batch);

    if ($batch['progressive']) {
      // Clear the way for the drupal_goto() redirection to the batch processing
      // page, by saving and unsetting the 'destination', if there is any.
      if (isset($_GET['destination'])) {
        $batch['destination'] = $_GET['destination'];
        unset($_GET['destination']);
      }

      // Initiate db storage in order to get a batch id. We have to provide
      // at least an empty string for the (not null) 'token' column.
      $batch['id'] = db_insert('batch')
        ->fields(array(
          'token' => '',
          'timestamp' => REQUEST_TIME,
        ))
        ->execute();

      // Now that we have a batch id, we can generate the redirection link in
      // the generic error message.
      $t = get_t();
      $batch['error_message'] = $t('Please continue to <a href="@error_url">the error page</a>', array('@error_url' => url($url, array('query' => array('id' => $batch['id'], 'op' => 'finished')))));

      // Actually store the batch data and the token generated form the batch id.
      db_update('batch')
        ->condition('bid', $batch['id'])
        ->fields(array(
          '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;

      $function = $batch['redirect_callback'];
      if (function_exists($function)) {
        $function($batch['url'], array('query' => array('op' => 'start', 'id' => $batch['id'])));
      }
    }
    else {
      // Non-progressive execution: bypass the whole progressbar workflow
      // and execute the batch in one pass.
      require_once DRUPAL_ROOT . '/includes/batch.inc';
      _batch_process();
    }
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.