Skip to content

Instantly share code, notes, and snippets.

@revagomes
Created December 18, 2012 16:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revagomes/4329270 to your computer and use it in GitHub Desktop.
Save revagomes/4329270 to your computer and use it in GitHub Desktop.
Drupal Batch API Example
<?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