-
Notifications
You must be signed in to change notification settings - Fork 4
/
boilerplate.batch.inc
66 lines (59 loc) · 1.57 KB
/
boilerplate.batch.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* @file
* Batch related functions.
*
* Created by: Topsitemakers
* http://www.topsitemakers.com/
*/
/**
* Sample batch init function.
*
* Load and then resave all content on the site.
*/
function boilerplate_batch_init() {
$batch = array(
'title' => t('Resaving content.'),
'operations' => array(),
'init_message' => t('Process started'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('An error occurred during processing'),
'finished' => 'boilerplate_batch_finished',
'progressive' => TRUE,
'file' => drupal_get_path('module', 'boilerplate') . '/boilerplate.batch.inc',
);
$nids = db_select('node', 'n')
->fields('n', array('nid'))
->orderBy('n.nid', 'ASC')
->execute()
->fetchCol();
foreach ($nids as $nid) {
$batch['operations'][] = array('boilerplate_batch_worker', array($nid));
}
batch_set($batch);
batch_process('admin/config');
}
/**
* Batch worker function.
*
* This will be called for each batch item.
*/
function boilerplate_batch_worker($nid, &$context) {
$node = node_load($nid);
node_save($node);
$context['results']['processed']++;
}
/**
* Batch finish function.
*
* This will be called once all batch items are processed.
*/
function boilerplate_batch_finished($success, $results, $operations) {
if ($success) {
$message = format_plural($results['processed'], 'One node processed.', '@count nodes processed.');
}
else {
$message = t('An error occurred. Check database log for more details.');
}
drupal_set_message($message);
}