Created
August 10, 2012 17:59
-
-
Save gregmercer/3316200 to your computer and use it in GitHub Desktop.
Drupal: batch_process() example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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