1

I am trying to run a batch job of importing items from a csv file. When I click submit on my form and my batch process starts, I do get a batch process bar which says its progressing, but it never calls my callback function or my finished function. I've done a dpm() of th $batch array before I run it, and it seems to be in order. What am I missing?

$count = 1;
  while($count <= count($csv) ) {
    $operations[] = array('ons_institutional_subscriber_importer_import', array(array_slice($csv, $count, $count + 10)));
    $count = $count + 10;
  }
  $batch = array(
    'title' => t('Importing Institutional Subscribers'),
    'operations' => $operations,
    'finished' => 'ons_institutional_subscriber_importer_finished',
    'init_message' => t('Import is starting.'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('Import has encountered an error.'),
  );
  dpm($batch);
  batch_set($batch);
  batch_process();

and my callback functions

function ons_institutional_subscriber_importer_import($csv, &$context) {
  dpm('hello');
  //more code here...
}
function ons_institutional_subscriber_importer_finished($success, $results, $operations) {
  if ($success) {
    $message = format_plural(count($results), 'One post processed.', '@count posts processed.');
  }
  else {
    $message = t('Finished with an error.');
  }
  drupal_set_message($message);
}
5
  • Please use code syntax for code. <pre> prevents syntax highlight and makes reading code harder than it needs to be.
    – Mołot
    Commented Nov 26, 2014 at 15:12
  • oh, interesting. I always use pre so that I can save time and not indent each line.... Commented Nov 26, 2014 at 15:22
  • There is a button on the editor's toolbar for that... one click.
    – Mołot
    Commented Nov 26, 2014 at 15:24
  • You sure the function is in the .module file? Otherwise you have to use $batch['file'] to specify the file.
    – AKS
    Commented Nov 26, 2014 at 15:51
  • That was it! Thanks! I added in the file name and it worked. Please post this as an answer and I will accept it Commented Nov 26, 2014 at 16:29

1 Answer 1

1

This Problem happens when we put our file in different folder like as includes/form_test.inc.In this scenario batch function can't call.So to handle this we put 'file' => 'path_to_file_containing_myfunctions' in our submit function.I wish this code will help you in understanding.you are missing 'file' in your batch array.

$batch = array(
  'title' => t('Custom File uploader'),
  'operations' => array(
    array('test_feeds_import_form_batch', array($uri)),
  ),
  'progress_message' => t('Uploading Multiple Products...'),
  'error_message' => t('Error!'),
  'finished' => 'test_feeds_import_form_batch_finish',
  'file' => drupal_get_path('module', 'test') . '/includes/test_form.inc',
);

batch_set($batch);
batch_process('admin/import-data'); //Work like as drupal goto

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