-
Notifications
You must be signed in to change notification settings - Fork 3
/
taxonomy_menu.batch.inc
129 lines (118 loc) · 3.83 KB
/
taxonomy_menu.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* @file
* Batch API Code
*/
/**
* Starts a batch operation.
*
* @param int $vocabulary
* Vocabulary machine name.
*/
function taxonomy_menu_insert_link_items_batch($vocabulary) {
$config = config('taxonomy_menu.vocabulary.' . $vocabulary);
$depth = $config->get('max_depth');
if ($depth == 0) {
$depth = NULL;
}
$terms = taxonomy_get_tree($vocabulary, 0, $depth, TRUE);
$menu_name = $config->get('vocab_menu');
$batch = array(
// An array of callbacks and arguments for the callbacks.
'operations' => array(
array('taxonomy_menu_insert_link_items_process', array($terms, $menu_name)),
),
// A callback to be used when the batch finishes.
'finished' => 'taxonomy_menu_insert_link_items_success',
// A title to be displayed to the end user when the batch starts.
'title' => t('Rebuilding Taxonomy Menu'),
// An initial message to be displayed to the end user when the batch starts.
'init_message' => t('The menu items have been deleted, and are about to be regenerated.'),
// A progress message for the end user. Placeholders are available.
// Placeholders include: @current, @remaining, @total and @percentage
'progress_message' => t('Import progress: Completed @current of @total stages.'),
// The message that will be displayed to the end user if the batch fails.
'error_message' => t('The Taxonomy Menu rebuild process encountered an error.'),
'redirect' => 'admin/structure/taxonomy',
);
batch_set($batch);
batch_process();
}
/**
* Batch operation callback function: inserts 10 menu link items.
*
* @param array $terms
* Taxonomy terms as from taxonomy_get_tree().
* @param string $menu_name
* Setting for the menu item name assigned to the vocabulary.
* @param array $context
* Batch context array.
*
* @see taxonomy_menu_insert_link_items_batch().
*/
function taxonomy_menu_insert_link_items_process($terms, $menu_name, &$context) {
_taxonomy_menu_batch_init_context($context, $start, $end, 10);
// Loop through $terms to process each term.
for ($i=$start; $i<count($terms) && $i<$end; $i++) {
$args = array(
'term' => $terms[$i],
'menu_name' => $menu_name,
);
$mlid = taxonomy_menu_handler('insert', $args);
}
_taxonomy_menu_batch_update_context($context, $end, count($terms), 'Creating Menu Items');
}
/**
* Batch finished callback: sets a message stating the menu has been updated.
*
* @see taxonomy_menu_insert_link_items_batch().
*/
function taxonomy_menu_insert_link_items_success() {
// @todo state menu name here.
backdrop_set_message(t('The Taxonomy Menu has been updated.'));
}
/**
* Initialise the batch context.
*
* @param array $context
* Batch context array.
* @param int $start
* The item to start on in this pass.
* @param int $end
* The end item of this pass.
* @param int $items
* The number of items to process in this pass.
*
* @see taxonomy_menu_insert_link_items_process().
*/
function _taxonomy_menu_batch_init_context(&$context, &$start, &$end, $items) {
// Initialize sandbox the first time through.
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
}
$start = $context['sandbox']['progress'];
$end = $start + $items;
}
/**
* Update the batch context.
*
* @param array $context
* Batch context array.
* @param int $end
* The end point of the most recent pass.
* @param int $total
* The total number of items to process in this batch.
* @param str $msg
* Message for the progress bar.
*
* @see taxonomy_menu_insert_link_items_process().
*/
function _taxonomy_menu_batch_update_context(&$context, $end, $total, $msg) {
if ($end > $total) {
$context['finished'] = 1;
return;
}
$context['message'] = "{$msg}: {$end} of {$total}";
$context['sandbox']['progress'] = $end;
$context['finished'] = $end/$total;
}