Advertisement
Guest User

Drupal 7 Batch API example

a guest
Sep 8th, 2011
2,324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. <?php
  2.  
  3. function skeleton_menu() {
  4.   $items['admin/skeleton/batch'] = array(
  5.     'title' => 'Skeleton Batch',
  6.     'page callback' => 'skeleton_batch_init',
  7.      'access arguments' => array('Administer content'),
  8.     'type' => MENU_NORMAL_ITEM,
  9.   );
  10.  
  11.   return $items;
  12. }
  13.  
  14.  
  15. function skeleton_batch_init() {
  16.   $batch = array(
  17.     'title' => t('Importing Content ...'),
  18.     'operations' => array(),
  19.     'init_message' => t('Commencing'),
  20.     'progress_message' => t('Processed @current out of @total.'),
  21.     'error_message' => t('An error occurred during processing'),
  22.     'finished' => 'skeleton_batch_finished',
  23.     'progressive' => FALSE
  24.   );
  25.  
  26.   $nodes = array(
  27.     'one',
  28.     'two',
  29.     'three',
  30.     'four'
  31.   );  
  32.  
  33.   foreach ($nodes as $new_node) {
  34.    $batch['operations'][] = array('skeleton_batch_worker', array($new_node));
  35.   }
  36.  
  37.   batch_set($batch);
  38.  
  39.   batch_process('<front>');
  40. }
  41.  
  42. function skeleton_batch_worker($new_node, &$context) {
  43. /*
  44.   $node = new stdClass();
  45.   $node->type = 'article';
  46.   node_object_prepare($node);  
  47.  
  48.   $node->title = $new_node;
  49.   $node->language = LANGUAGE_NONE;
  50.   $node->body[$node->language][0]['value'] = $new_node;
  51.   $node->body[$node->language][0]['summary'] = $new_node;
  52.    
  53.   $node->status = 1;
  54.  
  55.   node_save($node);
  56. */  
  57.   $context['results']['processed']++;
  58.   $context['message'] = $new_node;
  59. }
  60.  
  61. function skeleton_batch_finished($success, $results, $operations) {
  62.   if ($success) {
  63.     $message = format_plural($results['processed'], 'One node processed.', '@count nodes processed.');
  64.   }
  65.   else {
  66.     $message = 'some errors';
  67.   }
  68.  
  69.   drupal_set_message($message);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement