2

I'm writing a batch module and for some reason every time I load any page on my site, the batch is running. The other weird thing that may be related is that the $context['sandbox'] is not being preserved between iterations, so every time around it resets the progress count and starts over. I can't figure out what I'm doing wrong. This is a batch module that worked fine under D6, I made some changes based on what I thought was different in D7, but apparently i've really broken something.

here's my code for the entire module:

function batch_add_freetags_perm() {
  return array('batch: add editorial free tags to nodes');
}

function batch_add_freetags_menu() {
  $items['admin/content/batch_add_freetags'] = array(     
  'access callback' => TRUE,
  'page callback' => 'drupal_get_form',
  'page arguments' => array('batch_add_freetags_form'),
);
return $items;
}

function batch_add_freetags_form() {
      $form = array();
      $form['import'] = array(
        '#type' => 'fieldset',
        '#title' => t('Import Free Tags'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );

      $form['import']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Import'),
      );
      return $form;
}

function batch_add_freetags_form_submit($form, &$form_state) {
      batch_add_freetags_start();
}

function batch_add_freetags_start() {
  $batch = array(
    'title' => t('Tagging all Nodes'),
    'operations' => array(
        array('batch_add_freetags_process', array())
    ),
    'finished' => 'batch_add_freetags_finished',
    'init_message' => t('tag processing has started.'),
    'progress_message' => t('Processed @current out of @total nodes.'),
    'error_message' => t('Batch has encountered an error.'),
  );
  batch_set($batch);
}

This is what seems to run every time (I only want it to run when a certain form is submitted, of course.) :

function batch_add_freetags_process( &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox'] = array();
    $context['results'] = array();
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_node'] = 0;

    // get data from the old d6 database
    $query = Database::getConnection(SOURCE_DB_KEY, SOURCE_DB_KEY)
      ->select('term_node', 'tn');
    $query->fields('tn', array('nid', 'tid'));
    $query->join('term_data', 'td', 'td.tid = tn.tid');
    $query->fields('td', array('vid'));
    $query->condition('td.vid', 15);
    $result = $query->execute();

    // set the maximum number of items from the results of the db call.
    $context['sandbox']['max'] = count($result->fetchAllAssoc('nid'));

    $context['results']['updated'] = 0;
    $context['results']['failures'] = 0;
    $context['results']['processed'] = 0;
    $context['sandbox']['max'] = 20;   // this is just for testing

  }

  // For this case, we decide that we can safely process
  // N nodes at a time without a timeout.
  $limit = 5;

  // bunch of actual operations commented out just to make sure this for loop is just a test while i'm figuring this out...

  for($i = 0; $i<5; $i++) {
    // Update our progress information.
    $context['sandbox']['progress']++;
    $context['sandbox']['current_node'] = $i;
    $context['results']['processed']++;
    $context['message'] = t('Now processing tag "%name" for nid: %nid, <br/> completed tags for %current out of %max nodes', array('%name' => $term, '%nid' => $context['sandbox']['current_node'], '%current' => $context['sandbox']['progress'], '%max' => $context['sandbox']['max']));
  }  

  // Inform the batch engine that we are not finished,
  // and provide an estimation of the completion level we reached.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    // $context['finished'] = 1;   // make it stop!
  }
}

/**
 * Batch 'finished' callback
 */
function batch_add_freetags_finished($success, $results, $operations) {
  if ($success) {
    // Here we do something meaningful with the results.
    $message = count($results) .' processed.';
    $message .= theme('item_list', $results);
  }
  else {
    // An error occurred.
    // $operations contains the operations that remained unprocessed.
    $error_operation = reset($operations);
    $message = t('An error occurred while processing %error_operation with arguments: @arguments', array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)));
  }
  drupal_set_message($message);
}

thanks for any tips!

1

You need to change this function name batch_add_freetags_process to something else like batch_add_freetags_batch because hook_process() is utilized by Drupal and used to process theme variables for theme templates hook_process

Your code should look like the following:

function batch_add_freetags_start() {
  $batch = array(
    'title' => t('Tagging all Nodes'),
    'operations' => array(
        array('batch_add_freetags_batch', array())
    ),
    'finished' => 'batch_add_freetags_finished',
    'init_message' => t('tag processing has started.'),
    'progress_message' => t('Processed @current out of @total nodes.'),
    'error_message' => t('Batch has encountered an error.'),
  );
  batch_set($batch);  
}

function batch_add_freetags_batch( &$context) {
  // body
}
0

Well, I figured it out. There's a new D7 hook, hook_process(). So since my batch function was called "batch_add_freetags_process", it was getting called with every page load. I changed the name of the function and now things work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.