array(TRUE)); } function batch_test_node_access_records($node) { // if (in_array('batch_test', module_disabling())) { // return; // } $grants = array(); $grants[] = array( 'realm' => 'example', 'gid' => TRUE, 'grant_view' => TRUE, 'grant_update' => TRUE, 'grant_delete' => FALSE, 'priority' => 0, ); return $grants; } function batch_test_menu() { $items = array(); $items['batch_test'] = array( 'title' => 'Batch Test', 'page callback' => 'drupal_get_form', 'page arguments' => array('batch_test_simple_form'), 'access callback' => TRUE, ); $items['batch_test/test_1'] = array( 'title' => 'Simple', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => 0, ); $items['batch_test/test_2'] = array( 'title' => 'Chained', 'page callback' => 'drupal_get_form', 'page arguments' => array('batch_test_chained_form'), 'access callback' => TRUE, 'type' => MENU_LOCAL_TASK, 'weight' => 1, ); $items['batch_test/test_3'] = array( 'title' => 'Multistep', 'page callback' => 'drupal_get_form', 'page arguments' => array('batch_test_multistep_form'), 'access callback' => TRUE, 'type' => MENU_LOCAL_TASK, 'weight' => 2, ); $items['batch_test/test_4'] = array( 'title' => 'Programmatic', 'page callback' => 'drupal_get_form', 'page arguments' => array('batch_test_programmatic_form'), 'access callback' => TRUE, 'type' => MENU_LOCAL_TASK, 'weight' => 3, ); $items['batch_test/test_5'] = array( 'title' => 'No form', 'page callback' => 'batch_test_page', 'access callback' => TRUE, 'type' => MENU_LOCAL_TASK, 'weight' => 4, ); return $items; } /* * Test 1 : * Simple form */ function batch_test_simple_form() { $form['batch'] = array( '#type' => 'select', '#title' => 'Choose batch', '#options' => array('batch_1' => 'batch 1', 'batch_2' => 'batch 2') ); $form['param'] = array( '#type' => 'textfield', '#title' => 'Node ID', '#description' => 'Enter a node id for batch 1' ); $form['submit'] = array( '#type' => 'submit', '#value' => 'Go', ); $form['#redirect'] = 'batch_test/test_2'; return $form; } function batch_test_simple_form_submit(&$form_values, $form, &$form_state) { $function = 'batch_test_'.$form_values['batch']; $function($form_values['param']); $form_state['redirect'] = 'batch_test/test_2'; } /* * Test 2 : * Multiple submit callbacks */ function batch_test_chained_form() { $form['param'] = array( '#type' => 'textfield', '#title' => 'Node ID', '#description' => 'Enter a node id for batch 1' ); $form['submit'] = array( '#type' => 'submit', '#value' => 'Go', ); $form['#redirect'] = 'batch_test/test_1'; $form['#submit'] = array( 'batch_test_chained_form_submit_1', 'batch_test_chained_form_submit_2', 'batch_test_chained_form_submit_3', 'batch_test_chained_form_submit_4', ); return $form; } function batch_test_chained_form_submit_1(&$form_values, $form, &$form_state) { batch_test_batch_1($form_values['param']); $form_values['param'] = 1; } function batch_test_chained_form_submit_2(&$form_values, $form, &$form_state) { batch_test_batch_2(); drupal_set_message('$form_values[\'param\'] = '. $form_values['param']); $form_state['values']['param'] = 2; $form_state['redirect'] = 'batch_test/test_1'; } function batch_test_chained_form_submit_3(&$form_values, $form, &$form_state) { drupal_set_message('hello world'); drupal_set_message('$form_values[\'param\'] = '. $form_values['param']); $form_values['param'] = 3; $form_state['redirect'] = 'batch_test/test_3'; } function batch_test_chained_form_submit_4(&$form_values, $form, &$form_state) { // batch_test_batch_2(); drupal_set_message('$form_values[\'param\'] = '. $form_values['param']); } /* * Multistep form */ function batch_test_multistep_form($form_state = NULL) { $step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 1; $form['step_display'] = array( '#type' => 'item', '#value' => 'batch '. $step, ); if ($step == 1) { $form['param'] = array( '#type' => 'textfield', '#title' => 'Node ID', '#description' => 'Enter a node id for batch 1' ); } if ($step < 3) { $form['submit'] = array( '#type' => 'submit', '#value' => 'Go', ); } return $form; } function batch_test_multistep_form_submit(&$form_values, $form, &$form_state) { $step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 1; switch ($step) { case 1: drupal_set_message('step 1 submitted'); batch_test_batch_1($form_values['param']); break; case 2: drupal_set_message('step 2 submitted'); batch_test_batch_2(); // TODO : this does not seem to work ? $form_state['redirect'] = 'batch_test/test_4'; break; } $form_state['storage']['step'] = $step + 1; } /* * Programmatic form */ function batch_test_programmatic_form() { $form['param'] = array( '#type' => 'textfield', '#title' => 'Node ID', '#description' => 'Enter a node id for batch 1' ); $form['submit'] = array( '#type' => 'submit', '#value' => 'Go', ); return $form; } function batch_test_programmatic_form_submit($form_values, $form, $form_state) { $form_state_copy = array('values' => $form_values); drupal_execute('batch_test_chained_form', $form_state_copy); drupal_set_message('hey, we got out of a programmatic batched form !'); $form_state['redirect'] = 'batch_test/test_1'; } function batch_test_page() { batch_test_batch_1(1); batch_process('batch_test/test_1'); } // // Examples of batch declarations... // /** * Load 100 times a given node */ function batch_test_batch_1($nid) { $operations = array(); for ($i = 0; $i<100; $i++) { $operations[] = array('batch_test_op_1', array($nid)); } $batch = array( 'operations' => $operations, 'title' => t('batch_title'), 'init_message' => t('batch_init_message'), 'progress_message' => t('Remaining @remaining of @total.'), 'error_message' => t('batch_message_error'), 'finished' => 'batch_test_finished', ); batch_set($batch); } function batch_test_op_1($nid, &$a) { $node = node_load($nid, NULL, TRUE); $a['results'][] = $node->nid .' : '. $node->title; } /** * Iterative ops : load all nodes 5 by 5, 20 times */ function batch_test_batch_2() { $operations = array(); for ($i = 0; $i<20; $i++) { $operations[] = array('batch_test_op_2', array()); } $batch = array( 'operations' => $operations, 'title' => t('batch_title'), 'init_message' => t('batch_init_message'), 'progress_message' => t('Remaining @remaining of @total.'), 'error_message' => t('batch_message_error'), 'finished' => 'batch_test_finished', ); batch_set($batch); } // Example of an iterative operation : // stores the needed info in $sandbox function batch_test_op_2(&$context) { if (!isset($context['sandbox']['progress'])) { $context['sandbox']['progress'] = 0; $context['sandbox']['current_node'] = 0; $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT nid) FROM {node}')); } $limit = 5; $result = db_query_range("SELECT nid FROM {node} WHERE nid > %d ORDER BY nid ASC", $context['sandbox']['current_node'], 0, $limit); while ($row = db_fetch_array($result)) { $node = node_load($row['nid'], NULL, TRUE); $context['results'][] = $node->nid .' : '. $node->title; $context['sandbox']['progress']++; $context['sandbox']['current_node'] = $node->nid; $context['message'] = $node->title; } if ($context['sandbox']['progress'] != $context['sandbox']['max']) { $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; } } function batch_test_finished($success, $results) { if ($success) { $message = count($results) .' processed.'; } else { $message = 'finished with an error'; } drupal_set_message($message); }