Skip to content

Instantly share code, notes, and snippets.

@gregmercer
Created August 10, 2012 17:59
Show Gist options
  • Select an option

  • Save gregmercer/3316200 to your computer and use it in GitHub Desktop.

Select an option

Save gregmercer/3316200 to your computer and use it in GitHub Desktop.
Drupal: batch_process() example
$batch = array(
'title' => t('Moving files...'),
'operations' => array(),
'init_message' => t('Finding files to move'),
'progress_message' => t('Processed @current out of @total files.'),
'error_message' => t('An error occurred during processing.'),
'finished' => 'xxx_file_finished',
'progressive' => FALSE,
);
foreach ($file_ids as $fid) {
$batch['operations'][] = array(xxx_move_file', array($fid));
}
batch_set($batch);
batch_process('');
return '';
}
function xxx_move_file($fid, &$context) {
$file = file_load($fid);
$moved = file_move($file, 'public://restricted', FILE_EXISTS_ERROR);
}
function xxx_move_file_finished($success, $results, $operations, $time) {
if ($success) {
$message = 'it worked',
drupal_set_message($message);
}
else {
// Handle error case.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment