function batch_example_op_2
Same name in other branches
- 3.x modules/batch_example/batch_example.module \batch_example_op_2()
- 8.x-1.x batch_example/batch_example.module \batch_example_op_2()
- 6.x-1.x batch_example/batch_example.module \batch_example_op_2()
- 4.0.x modules/batch_example/batch_example.module \batch_example_op_2()
Batch operation for batch 2 : load all nodes, 5 by five.
After each group of 5 control is returned to the batch API for later continuation.
Related topics
1 string reference to 'batch_example_op_2'
- batch_example_batch_2 in batch_example/
batch_example.module - Batch 2 : Prepare a batch definition that will load all nodes 20 times.
File
-
batch_example/
batch_example.module, line 208
Code
function batch_example_op_2($operation_details, &$context) {
// Use the $context['sandbox'] at your convenience to store the
// information needed to track progression between successive calls.
if (empty($context['sandbox'])) {
$context['sandbox'] = array();
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_node'] = 0;
// Save node count for the termination message.
$context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
}
// 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.
$result = db_select('node', 'n')->fields('n', array(
'nid',
))
->orderBy('n.nid', 'ASC')
->where('n.nid > :nid', array(
':nid' => $context['sandbox']['current_node'],
))
->extend('PagerDefault')
->limit($limit)
->execute();
foreach ($result as $row) {
// Here we actually perform our dummy 'processing' on the current node.
$node = node_load($row->nid, NULL, TRUE);
// Store some results for post-processing in the 'finished' callback.
// The contents of 'results' will be available as $results in the
// 'finished' function (in this example, batch_example_finished()).
$context['results'][] = $node->nid . ' : ' . check_plain($node->title) . ' ' . $operation_details;
// Update our progress information.
$context['sandbox']['progress']++;
$context['sandbox']['current_node'] = $node->nid;
$context['message'] = check_plain($node->title);
}
// 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'];
}
_batch_example_update_http_requests();
}