6

I am creating a batch import and I would like to pass two different counts to my finished function. For example in my 'operations' function I check for an NID if it exists update the node if it doesn't then create it.

In my 'finished' function I want to do something like:

 batch_import_finished($success, $results, $operations) {
   if ($success) {
     $message ='Nodes created: '.$nodesCreated. '<br />';
     $message .='Nodes updated: '.$nodesUpdated;
   }
   else {
     $message = 'some errors';
   }

  drupal_set_message($message);
}

Hope this makes sense.

12

First, make sure you set the finished process in your batch process.

$batch = array(
      'title' => t('Merging content ...'),
      'operations' => array(
         array('batch_import_import', array($col1, $col2),  //Note we pass 2 variables here.
       ),
      'finished' => 'batch_import_finished', // set this!
      'init_message' => t('Connectng to the base site ...'),
      'progress_message' => t('Processed @current out of @total.'),
      'error_message' => t('An error occurred during processing'),
      'progressive' => FALSE
    );
...
batch_set('admin/content');

Then, in your worker function, you can take the last argument passed to it as $context.

For an example, from above example above, your worker function should be something like this:

function batch_import_import($col1, $col2, &$context) {
  //do the stuff.
  if (!isset($context['results']['created'], $context['results']['updated'])) {
    $context['results']['created'] = 0;
    $context['results']['updated'] = 0;
  }
  $context['results']['created']++; // is_new == TRUE or some logic.
  $context['results']['updated']++; // add some logic to determine created or updated.
}

and then in the finish function, you get $context['results'] as the second argument.

batch_import_finished($success, $results, $operations) {
   if ($success) {
     $message = t('Nodes created: @count', array('@count' => $results['created'])).'<br />';
     $message .= t('Nodes updated: @count', array('@count' => $results['updated']));
   }
   else {
     $message = t('some errors');
   }

  drupal_set_message($message);
}

I have also corrected the $message as you can use t() function here. Note that you pass $context by reference. See batch_api examples for more information. This is just some code copied from a module I was developing the other day.

In the finished function, $operations contain unprocessed items.

  • Thank you, I tried your example and I get an Notice: Undefined variable: col1, Notice: Undefined variable: col2. Here is my operations - array('node_import_worker', array($new_node, $col1, $col2)). Don't I need to set the value of $col1 and $col2 ? – Brian Jan 20 '13 at 14:41
  • I just set the values before the function and everything seems to work, Thanks! – Brian Jan 20 '13 at 15:02

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.