Last active
August 29, 2015 14:03
-
-
Save yannickoo/cba63c78d2b0c0286a47 to your computer and use it in GitHub Desktop.
Drupal batch 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
<?php | |
/** | |
* @file | |
* Administrative tasks for My module. | |
*/ | |
/** | |
* Page callback for Batch example form. | |
*/ | |
function mymodule_batch_example_form($form, &$form_state) { | |
$nodes_query = new EntityFieldQuery(); | |
$nodes_query ->entityCondition('entity_type', 'node') | |
->entityCondition('bundle', 'recording') | |
->count(); | |
$nodes_count = $nodes_query->execute(); | |
if (!$nodes_count) { | |
drupal_set_message('You cannot submit this form because there are no article nodes available.', 'error'); | |
} | |
$form['submit'] = array( | |
'#type' => 'submit', | |
'#value' => t('Start batch'), | |
'#disabled' => $nodes_count ? FALSE : TRUE, | |
); | |
return $form; | |
} | |
/** | |
* Submit function for batch form. | |
*/ | |
function mymodule_batch_example_form_submit($form, &$form_state) { | |
batch_set(mymodule_override_node_titles_batch()); | |
} | |
function mymodule_override_node_titles_batch() { | |
$batch = array( | |
'title' => t('Loading nodes'), | |
'operations' => array( | |
array('mymodule_override_node_titles_batch_op', array()), | |
), | |
'finished' => 'mymodule_override_node_titles_batch_finished', | |
// 'file' is optional and can be useful for outsourcing batch code. | |
'file' => drupal_get_path('module', 'mymodule') . '/mymodule.admin.inc', | |
); | |
return $batch; | |
} | |
/** | |
* Batch operation for overriding node titles. | |
*/ | |
function mymodule_override_node_titles_batch_op(&$context) { | |
if (empty($context['sandbox'])) { | |
$context['sandbox'] = array(); | |
$context['sandbox']['progress'] = 0; | |
$context['sandbox']['current_node'] = 0; | |
$nodes_query = new EntityFieldQuery(); | |
$nodes_query ->entityCondition('entity_type', 'node') | |
->entityCondition('bundle', 'article'); | |
$nodes = $nodes_query->execute(); | |
$max = count($nodes['node']); | |
// Save node count for the termination message. | |
$context['sandbox']['max'] = $max; | |
} | |
// Process nodes by groups of 5 (arbitrary value). | |
// When a group of five is processed, the batch update engine determines | |
// whether it should continue processing in the same request or provide | |
// progress feedback to the user and wait for the next request. | |
// That way even though we're already processing at the operation level | |
// the operation itself is interruptible. | |
$limit = 5; | |
// Retrieve the next group of nids. | |
$next_query = new EntityFieldQuery(); | |
$next_query->entityCondition('entity_type', 'node') | |
->entityCondition('bundle', 'article') | |
->propertyCondition('nid', $context['sandbox']['current_node'], '>') | |
->pager($limit); | |
$next = $next_query->execute(); | |
$next = node_load_multiple(array_keys($next['node'])); | |
foreach ($next as $node) { | |
// Here we can do the actual operation like setting a field value | |
// and saving the node. | |
// Set a random node title. | |
$node->title = user_password(); | |
node_save($node); | |
$context['results'][] = $node->title; | |
// Update our progress information. | |
$context['sandbox']['progress']++; | |
$context['sandbox']['current_node'] = $node->nid; | |
$context['message'] = t('Fetching information for @remaining of @total songs.', array( | |
'@remaining' => $context['sandbox']['max'] - $context['sandbox']['progress'], | |
'@total' => $context['sandbox']['max'], | |
)); | |
} | |
// Inform the batch engine that we are not finished, | |
// and provide an estimation of the completion level we reached. | |
if ($context['sandbox']['progress'] != $context['sandbox']['max']) { | |
$context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']); | |
} | |
} | |
/** | |
* Function which is called when batch is finished. | |
*/ | |
function mymodule_override_node_titles_batch_finished($success, $results, $operations) { | |
if ($success) { | |
$message = format_plural(count($results), 'One title has been overriden.', '@count titles have been overridden.'); | |
} | |
else { | |
$message = t('Finished with an error.'); | |
} | |
drupal_set_message($message); | |
} |
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
<?php | |
/** | |
* Implements hook_menu(). | |
*/ | |
function mymodule_dev_menu() { | |
$items = array(); | |
$items['admin/config/development/mymodule'] = array( | |
'title' => 'My module', | |
'page callback' => 'drupal_get_form', | |
'page arguments' => array('mymodule_batch_example_form'), | |
'access arguments' => array('administer site configuration'), | |
'file' => 'mymodule.admin.inc', | |
'type' => MENU_NORMAL_ITEM, | |
); | |
return $items; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment