Created
December 18, 2012 16:04
-
-
Save revagomes/4329270 to your computer and use it in GitHub Desktop.
Drupal Batch API Example
This file contains 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
<?php | |
function skeleton_menu() { | |
$items['admin/skeleton/batch'] = array( | |
'title' => 'Skeleton Batch', | |
'page callback' => 'skeleton_batch_init', | |
'access arguments' => array('Administer content'), | |
'type' => MENU_NORMAL_ITEM, | |
); | |
return $items; | |
} | |
function skeleton_batch_init() { | |
$batch = array( | |
'title' => t('Importing Content ...'), | |
'operations' => array(), | |
'init_message' => t('Commencing'), | |
'progress_message' => t('Processed @current out of @total.'), | |
'error_message' => t('An error occurred during processing'), | |
'finished' => 'skeleton_batch_finished', | |
'progressive' => FALSE | |
); | |
$nodes = array( | |
'one', | |
'two', | |
'three', | |
'four' | |
); | |
foreach ($nodes as $new_node) { | |
$batch['operations'][] = array('skeleton_batch_worker', array($new_node)); | |
} | |
batch_set($batch); | |
batch_process('<front>'); | |
} | |
function skeleton_batch_worker($new_node, &$context) { | |
/* | |
$node = new stdClass(); | |
$node->type = 'article'; | |
node_object_prepare($node); | |
$node->title = $new_node; | |
$node->language = LANGUAGE_NONE; | |
$node->body[$node->language][0]['value'] = $new_node; | |
$node->body[$node->language][0]['summary'] = $new_node; | |
$node->status = 1; | |
node_save($node); | |
*/ | |
$context['results']['processed']++; | |
$context['message'] = $new_node; | |
} | |
function skeleton_batch_finished($success, $results, $operations) { | |
if ($success) { | |
$message = format_plural($results['processed'], 'One node processed.', '@count nodes processed.'); | |
} | |
else { | |
$message = 'some errors'; | |
} | |
drupal_set_message($message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment