I have a number of custom modules I'm developing for a rather complex site. These modules need to create quite a few custom types, views, panels, imagecache presets, etc when they are enabled. Obviously doing that much configuration takes some time so I was wondering if it's possible to use the batch api in D6 during the hook_install of my module?

I've tried it out and can get the progress bar to show up during the module install but then the page goes back to the module list without enabling the module and none of the tasks in the batch were executed.

Are there any documentation on the Drupal site on how to do this or are there any other modules in the repository that do something similar that I could reference?

Thanks.

Comments

I think it's possible. Just try the following code:

mymodule.install

<?php
function mymodule_install() {
 
mymodule_bacth();
}
function
mymodule_batch() {
 
$nid = db_result(db_query_range("SELECT nid FROM {node} ORDER BY nid ASC", 0, 1));
 
$operations = array();
  for (
$i = 0; $i<10; $i++) {
   
$operations[] = array('mymodule_batch_op', array($nid));
  }
 
$batch = array(
   
'operations' => $operations,
   
'finished' => 'mymodule_batch_finished',
   
'title' => t('My Module batch'),
   
'init_message' => t('Batch is starting...'),
   
'progress_message' => t('Processed @current out of @total.'),
   
'error_message' => t('Batch has encountered an error.'),
   
'file' => drupal_get_path('module', 'mymodule') . '/mymodule.install',
  );
 
batch_set($batch);
}
function
mymodule_batch_op($nid, &$context) {
 
$node = node_load($nid, NULL, TRUE);
 
// Store some result for post-processing in the finished callback.
 
$context['results'][] = $node->nid . ' : ' . check_plain($node->title);
 
// Optional message displayed under the progressbar.
 
$context['message'] = t('Loading @title', array('@title' => $node->title));
}
function
mymodule_batch_finished($success, $results, $operations) {
  if (
$success) {
     
$message = t('Batch processing is succeed...');
  }
  else {
   
$error_operation = reset($operations);
   
$message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments :' . print_r($error_operation[0], TRUE);
  }
 
drupal_set_message($message);
}
?>

I can get this code working via drush, but I can't get the progress bar working through the browser (Drupal 6). I wind up with a WSOD and no entries in watch dog. I have exactly the same use case as shawn_smiley. The Batch API is really cool -- too bad I can't quite get it working!

#ASIDE: It's a little hard to see in the documentation exactly how one should go about getting the percentage bar to increase. It is hidden inside the comments in the sample code here using &$context['finished'] as a float between 0 and 1. (1 means 100% finished).

#ASIDE 2: I noticed that I was unable to a module to be recognized by Drupal unless it had at least an info file and a (blank) module file.

#EDIT: Also, note the typo in line 3 in the above code. mymodule_bacth(); should instead be mymodule_batch();.

Cheers,
Bryce