1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* It generates menu links for all selected taxonomy terms
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
//include the database layer
|
9 |
|
|
require_once(drupal_get_path('module', 'taxonomy_menu') . '/taxonomy_menu.database.inc');
|
10 |
|
|
|
11 |
|
|
//include the batch functions
|
12 |
|
|
require_once(drupal_get_path('module', 'taxonomy_menu') . '/taxonomy_menu.batch.inc');
|
13 |
|
|
|
14 |
|
|
/**
|
15 |
|
|
* Implements hook_form_alter().
|
16 |
|
|
*
|
17 |
|
|
* Modify the form at admin/content/taxonomy/edit/vocabulary/xx. We add
|
18 |
|
|
* our taxonomy_menu options in here on a per-vocab basis.
|
19 |
|
|
*/
|
20 |
|
|
function taxonomy_menu_form_alter(&$form, &$form_state, $form_id) {
|
21 |
|
|
if ($form_id == 'taxonomy_form_vocabulary') {
|
22 |
|
|
// do not alter on deletion
|
23 |
|
|
if (isset($form_state['confirm_delete']) && isset($form_state['values']['vid'])) {
|
24 |
|
|
return;
|
25 |
|
|
}
|
26 |
|
|
// Choose a menu to add link items to.
|
27 |
|
|
$menus = menu_get_menus();
|
28 |
|
|
array_unshift($menus, t('- Disabled -'));
|
29 |
|
|
|
30 |
|
|
// Options for path if tokens are not enabled.
|
31 |
|
|
$paths = _taxonomy_menu_get_paths();
|
32 |
|
|
|
33 |
|
|
$form['taxonomy_menu'] = array(
|
34 |
|
|
'#type' => 'fieldset',
|
35 |
|
|
'#collapsible' => TRUE,
|
36 |
|
|
'#title' => t('Taxonomy menu'),
|
37 |
|
|
'#weight' => 10,
|
38 |
|
|
'#tree' => TRUE,
|
39 |
|
|
);
|
40 |
|
|
// This turns the vocab terms into menu items.
|
41 |
|
|
$item['mlid'] = 0;
|
42 |
|
|
|
43 |
|
|
$menu_items = menu_parent_options(menu_get_menus(), $item);
|
44 |
|
|
|
45 |
|
|
array_unshift($menu_items, '= DISABLED =');
|
46 |
|
|
|
47 |
|
|
// The vid isn't set when a new vocabulary is being created.
|
48 |
|
|
if (isset($form['vid']['#value'])) {
|
49 |
|
|
$default = variable_get(_taxonomy_menu_build_variable('vocab_menu', $form['vid']['#value']), NULL) . ':' .
|
50 |
|
|
variable_get(_taxonomy_menu_build_variable('vocab_parent', $form['vid']['#value']), NULL);
|
51 |
|
|
if (!isset($menu_items[$default])) {
|
52 |
|
|
$default = 0;
|
53 |
|
|
}
|
54 |
|
|
}
|
55 |
|
|
else {
|
56 |
|
|
$default = 0;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
$form['taxonomy_menu']['vocab_parent'] = array(
|
60 |
|
|
'#type' => 'select',
|
61 |
|
|
'#title' => t('Menu location'),
|
62 |
|
|
'#default_value' => $default,
|
63 |
|
|
'#options' => $menu_items,
|
64 |
|
|
'#description' => t('The menu and parent under which to insert taxonomy menu items.'),
|
65 |
|
|
'#attributes' => array('class' => array('menu-title-select')),
|
66 |
|
|
);
|
67 |
|
|
|
68 |
|
|
$form['taxonomy_menu']['path'] = array(
|
69 |
|
|
'#type' => 'select',
|
70 |
|
|
'#title' => t('Menu path type'),
|
71 |
|
|
'#default_value' => isset($form['vid']['#value']) ? variable_get(_taxonomy_menu_build_variable('path', $form['vid']['#value']), 0) : 0,
|
72 |
|
|
'#options' => $paths,
|
73 |
|
|
'#description' => t('The path will be taxonomy/term/tid if <em>Default</em> has been selected.<br />The menu path will be passed through drupal_get_path_alias() function so all aliases will be applied.'),
|
74 |
|
|
);
|
75 |
|
|
|
76 |
|
|
//get taxonomy menu form options
|
77 |
|
|
if (isset($form['vid']) && $form['vid']['#value']) {
|
78 |
|
|
$vid = $form['vid']['#value'];
|
79 |
|
|
}
|
80 |
|
|
else {
|
81 |
|
|
$vid = 0;
|
82 |
|
|
}
|
83 |
|
|
$form['taxonomy_menu']['options'] = _taxonomy_menu_create_options($vid);
|
84 |
|
|
|
85 |
|
|
//rebuild the menu
|
86 |
|
|
$form['taxonomy_menu']['options']['rebuild'] = array(
|
87 |
|
|
'#type' => 'checkbox',
|
88 |
|
|
'#title' => t('Select to rebuild the menu on submit.'),
|
89 |
|
|
'#default_value' => 0,
|
90 |
|
|
'#weight' => 20,
|
91 |
|
|
'#description' => t('Rebuild the menu on submit. <strong>Warning</strong>: This will delete then re-create all of the menu items. Only use this option if you are experiencing issues like missing menu items or other inconsistencies.'),
|
92 |
|
|
);
|
93 |
|
|
// move the buttons to the bottom of the form
|
94 |
|
|
$form['submit']['#weight'] = 49;
|
95 |
|
|
$form['delete']['#weight'] = 50;
|
96 |
|
|
|
97 |
|
|
// add an extra submit handler to save these settings
|
98 |
|
|
$form['#submit'][] = 'taxonomy_menu_vocab_submit';
|
99 |
|
|
|
100 |
|
|
}
|
101 |
|
|
elseif ($form_id == "taxonomy_overview_terms") {
|
102 |
|
|
// add an extra submit handler to sync the rearranged terms with menu
|
103 |
|
|
// @ TODO: using hook_taxonomy_vocabulary_update is nicer then callback,
|
104 |
|
|
// but gives less info and does not always fire.
|
105 |
|
|
$form['#submit'][] = 'taxonomy_menu_overview_submit';
|
106 |
|
|
}
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
/**
|
110 |
|
|
* Submit handler for the extra settings added to the taxonomy vocab form.
|
111 |
|
|
*
|
112 |
|
|
* Check to see if the user has selected a different menu, and only rebuild
|
113 |
|
|
* if this is the case.
|
114 |
|
|
*/
|
115 |
|
|
function taxonomy_menu_vocab_submit($form, &$form_state) {
|
116 |
|
|
$vid = $form_state['values']['vid'];
|
117 |
|
|
$changed = FALSE;
|
118 |
|
|
|
119 |
|
|
if (is_numeric($form_state['values']['taxonomy_menu']['vocab_parent'])) {
|
120 |
|
|
// Menu location has been set to disabled, don't want to throw notices
|
121 |
|
|
$form_state['values']['taxonomy_menu']['vocab_parent'] = '0:0';
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
// Split the menu location into menu name and menu item id.
|
125 |
|
|
list($vocab_parent['vocab_menu'], $vocab_parent['vocab_parent']) = explode(':', $form_state['values']['taxonomy_menu']['vocab_parent']);
|
126 |
|
|
|
127 |
|
|
// Init flag variables to avoid notices if changes haven't happened
|
128 |
|
|
$changed_menu = FALSE;
|
129 |
|
|
$change_vocab_item = FALSE;
|
130 |
|
|
$changed_path = FALSE;
|
131 |
|
|
|
132 |
|
|
// Set the menu name and check for changes
|
133 |
|
|
$variable_name = _taxonomy_menu_build_variable('vocab_menu', $vid);
|
134 |
|
|
if (_taxonomy_menu_check_variable($variable_name, $vocab_parent['vocab_menu'])) {
|
135 |
|
|
$changed_menu = TRUE;
|
136 |
|
|
}
|
137 |
|
|
variable_set($variable_name, $vocab_parent['vocab_menu']);
|
138 |
|
|
|
139 |
|
|
// Set the menu parent item and check for changes
|
140 |
|
|
$variable_name = _taxonomy_menu_build_variable('vocab_parent', $vid);
|
141 |
|
|
if (_taxonomy_menu_check_variable($variable_name, $vocab_parent['vocab_parent'])) {
|
142 |
|
|
$changed_menu = TRUE;
|
143 |
|
|
}
|
144 |
|
|
variable_set($variable_name, $vocab_parent['vocab_parent']);
|
145 |
|
|
|
146 |
|
|
// Set the path and check for changes
|
147 |
|
|
$variable_name = _taxonomy_menu_build_variable('path', $vid);
|
148 |
|
|
if (_taxonomy_menu_check_variable($variable_name, $form_state['values']['taxonomy_menu']['path'])) {
|
149 |
|
|
$changed_path = TRUE;
|
150 |
|
|
}
|
151 |
|
|
variable_set($variable_name, $form_state['values']['taxonomy_menu']['path']);
|
152 |
|
|
|
153 |
|
|
foreach ($form_state['values']['taxonomy_menu']['options'] as $key => $value) {
|
154 |
|
|
// Create the variable name
|
155 |
|
|
$variable_name = _taxonomy_menu_build_variable($key, $vid);
|
156 |
|
|
|
157 |
|
|
// Check to see if the vocab enable options has changed
|
158 |
|
|
if ($key == 'voc_item') {
|
159 |
|
|
if (_taxonomy_menu_check_variable($variable_name, $value)) {
|
160 |
|
|
$change_vocab_item = TRUE;
|
161 |
|
|
}
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
// If $changed is alreayd set to true, then don't bother checking any others.
|
165 |
|
|
if (!$changed) {
|
166 |
|
|
// Check to see if the variable has changed.
|
167 |
|
|
if (_taxonomy_menu_check_variable($variable_name, $value)) {
|
168 |
|
|
$changed = TRUE;
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
// Save variable.
|
172 |
|
|
variable_set($variable_name, $value);
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
// If the menu hasn't changed and the menu is disabled then do not do anything else.
|
176 |
|
|
if ($form_state['values']['taxonomy_menu']['options']['rebuild'] ||
|
177 |
|
|
$changed_menu ||
|
178 |
|
|
(!$changed_menu && variable_get(_taxonomy_menu_build_variable('vocab_menu', $vid), FALSE) == 0)) {
|
179 |
|
|
// Rebuild if rebuild is selected, menu has changed or vocabulary option changed.
|
180 |
|
|
if ($form_state['values']['taxonomy_menu']['options']['rebuild'] || $changed_menu || $change_vocab_item || $changed_path) {
|
181 |
|
|
$message = _taxonomy_menu_rebuild($vid);
|
182 |
|
|
}
|
183 |
|
|
// If setting has changed and a menu item is enabled, then update all of the menu items.
|
184 |
|
|
elseif ($changed && variable_get(_taxonomy_menu_build_variable('vocab_menu', $vid), FALSE)) {
|
185 |
|
|
$message = _taxonomy_menu_update_link_items($vid);
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
// Do a full menu rebuild in case we have removed the menu or moved it between menus.
|
189 |
|
|
variable_set('menu_rebuild_needed', TRUE);
|
190 |
|
|
// Only send a message if one has been created.
|
191 |
|
|
if (isset($message) && $message) {
|
192 |
|
|
// $message is sanitized coming out of its source function,
|
193 |
|
|
// no need to reclean it here
|
194 |
|
|
drupal_set_message($message, 'status');
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
/**
|
200 |
|
|
* Submit handler, reacting on form ID: taxonomy_overview_terms
|
201 |
|
|
*/
|
202 |
|
|
function taxonomy_menu_overview_submit(&$form, &$form_state) {
|
203 |
|
|
// Only sync if taxonomy_menu is enabled for this vocab and the 'sync'
|
204 |
|
|
// option has been checked.
|
205 |
|
|
|
206 |
|
|
// This form has the following flow of buttons:
|
207 |
|
|
// 1. [Save] --> rebuild taxonomy_menu
|
208 |
|
|
// 2. [Reset to alphabetical] --> no rebuild yet
|
209 |
|
|
// 3. [Reset to alphabetical][Reset to alphabetical] --> rebuild
|
210 |
|
|
// 4. [Reset to alphabetical][Cancel] --> no rebuild
|
211 |
|
|
// The code below avoids rebuilding after situation 2.
|
212 |
|
|
|
213 |
|
|
if ($form_state['rebuild'] == FALSE && isset($form['#vocabulary']->vid) ) {
|
214 |
|
|
// Try to catch the 'Save' button.
|
215 |
|
|
$vid = $form['#vocabulary']->vid;
|
216 |
|
|
}
|
217 |
|
|
elseif ($form_state['rebuild'] == TRUE && isset($form['#vocabulary']->vid) ) {
|
218 |
|
|
// Try to catch the 'Reset to alphabetical' button
|
219 |
|
|
$vid = NULL;
|
220 |
|
|
}
|
221 |
|
|
elseif ($form_state['rebuild'] == FALSE && isset($form['vid']['#value']) ) {
|
222 |
|
|
// Try to catch the second (confirming) 'Reset to alphabetical' button.
|
223 |
|
|
$vid = $form['vid']['#value'];
|
224 |
|
|
}
|
225 |
|
|
else {
|
226 |
|
|
// The button [Reset to alphabetical] [Cancel] does not call this page.
|
227 |
|
|
$vid = NULL;
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
if (isset($vid)) {
|
231 |
|
|
$menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $vid), 0);
|
232 |
|
|
$sync = variable_get(_taxonomy_menu_build_variable('sync', $vid), 0);
|
233 |
|
|
if ($menu_name && $sync) {
|
234 |
|
|
// Update all menu items (do not rebuild the menu).
|
235 |
|
|
$message = _taxonomy_menu_update_link_items($vid);
|
236 |
|
|
|
237 |
|
|
// Report status.
|
238 |
|
|
if (isset($message)) {
|
239 |
|
|
// message is sanitized coming out of _taxonomy_menu_update_link_items
|
240 |
|
|
// no need to reclean it here
|
241 |
|
|
drupal_set_message($message, 'status');
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
// Rebuild the menu.
|
245 |
|
|
menu_cache_clear($menu_name);
|
246 |
|
|
}
|
247 |
|
|
}
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
/**
|
251 |
|
|
* rebuilds a menu
|
252 |
|
|
*
|
253 |
|
|
* @param $vid
|
254 |
|
|
* @return $message
|
255 |
|
|
* message that is displayed
|
256 |
|
|
*/
|
257 |
|
|
function _taxonomy_menu_rebuild($vid) {
|
258 |
|
|
// Remove all of the menu items for this vocabulary
|
259 |
|
|
_taxonomy_menu_delete_all($vid);
|
260 |
|
|
|
261 |
|
|
// Only insert the links if a menu is set
|
262 |
|
|
if (variable_get(_taxonomy_menu_build_variable('vocab_menu', $vid), FALSE)) {
|
263 |
|
|
_taxonomy_menu_insert_link_items($vid);
|
264 |
|
|
menu_rebuild();
|
265 |
|
|
return t('The Taxonomy Menu has been rebuilt.');
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
menu_rebuild();
|
269 |
|
|
return t('The Taxonomy Menu has been removed.');
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
/**
|
273 |
|
|
* Checks to see if the variable has changed.
|
274 |
|
|
*
|
275 |
|
|
* @param $variable
|
276 |
|
|
* name of variable
|
277 |
|
|
* @return Boolean
|
278 |
|
|
* TRUE if it has changed
|
279 |
|
|
*/
|
280 |
|
|
function _taxonomy_menu_check_variable($variable, $new_value) {
|
281 |
|
|
if ($new_value != variable_get($variable, FALSE)) {
|
282 |
|
|
return TRUE;
|
283 |
|
|
}
|
284 |
|
|
return FALSE;
|
285 |
|
|
}
|
286 |
|
|
|
287 |
|
|
/**
|
288 |
|
|
* Update the menu items
|
289 |
|
|
*
|
290 |
|
|
* @param $vid
|
291 |
|
|
* vocab id
|
292 |
|
|
*/
|
293 |
|
|
function _taxonomy_menu_update_link_items($vid) {
|
294 |
|
|
$menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $vid), FALSE);
|
295 |
|
|
|
296 |
|
|
// Get a list of the current tid - menu_link combinations
|
297 |
|
|
$menu_links = _taxonomy_menu_get_menu_items($vid);
|
298 |
|
|
|
299 |
|
|
// Cycle through the menu links
|
300 |
|
|
foreach ($menu_links as $tid => $mlid) {
|
301 |
|
|
// $args must be reset each time through.
|
302 |
|
|
$args = array(
|
303 |
|
|
'menu_name' => $menu_name,
|
304 |
|
|
'mlid' => $mlid,
|
305 |
|
|
);
|
306 |
|
|
|
307 |
|
|
if ($tid == 0) {
|
308 |
|
|
$args['vid'] = $vid;
|
309 |
|
|
}
|
310 |
|
|
else {
|
311 |
|
|
$args['term'] = taxonomy_term_load($tid);
|
312 |
|
|
}
|
313 |
|
|
|
314 |
|
|
//update the menu link
|
315 |
|
|
taxonomy_menu_handler('update', $args);
|
316 |
|
|
}
|
317 |
|
|
|
318 |
|
|
return t('The Taxonomy Menu %menu_name has been updated.', array('%menu_name' => $menu_name));
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
/**
|
322 |
|
|
* Creates new link items for the vocabulary
|
323 |
|
|
*
|
324 |
|
|
* @param $vid
|
325 |
|
|
*/
|
326 |
|
|
function _taxonomy_menu_insert_link_items($vid) {
|
327 |
|
|
$menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $vid), FALSE);
|
328 |
|
|
// Check to see if we should had a vocab item
|
329 |
|
|
if (variable_get(_taxonomy_menu_build_variable('voc_item', $vid), FALSE)) {
|
330 |
|
|
$args = array(
|
331 |
|
|
'vid' => $vid,
|
332 |
|
|
'menu_name' => $menu_name,
|
333 |
|
|
);
|
334 |
|
|
taxonomy_menu_handler('insert', $args);
|
335 |
|
|
}
|
336 |
|
|
// Let batch api take care of inserting the menu items
|
337 |
|
|
_taxonomy_menu_insert_link_items_batch($vid);
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
/**
|
341 |
|
|
* Implements hook_taxonomy_vocabulary_delete().
|
342 |
|
|
*/
|
343 |
|
|
function taxonomy_menu_taxonomy_vocabulary_delete($vocabulary) {
|
344 |
|
|
//delete the menu items
|
345 |
|
|
_taxonomy_menu_delete_all($vocabulary->vid);
|
346 |
|
|
$menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $vocabulary->vid), 0);
|
347 |
|
|
menu_cache_clear($menu_name);
|
348 |
|
|
}
|
349 |
|
|
|
350 |
|
|
/**
|
351 |
|
|
* Implements hook_taxonomy_term_insert($term).
|
352 |
|
|
*/
|
353 |
|
|
|
354 |
|
|
function taxonomy_menu_taxonomy_term_insert($term) {
|
355 |
|
|
_taxonomy_menu_taxonomy_termapi_helper($term, 'insert');
|
356 |
|
|
}
|
357 |
|
|
|
358 |
|
|
/**
|
359 |
|
|
* Implements hook_taxonomy_term_update().
|
360 |
|
|
*/
|
361 |
|
|
|
362 |
|
|
function taxonomy_menu_taxonomy_term_update($term) {
|
363 |
|
|
_taxonomy_menu_taxonomy_termapi_helper($term, 'update');
|
364 |
|
|
}
|
365 |
|
|
|
366 |
|
|
/**
|
367 |
|
|
* Implements hook_taxonomy_term_delete().
|
368 |
|
|
*/
|
369 |
|
|
|
370 |
|
|
function taxonomy_menu_taxonomy_term_delete($term) {
|
371 |
|
|
_taxonomy_menu_taxonomy_termapi_helper($term, 'delete');
|
372 |
|
|
}
|
373 |
|
|
|
374 |
|
|
/**
|
375 |
|
|
* Implements hook_node_insert().
|
376 |
|
|
*/
|
377 |
|
|
function taxonomy_menu_node_insert($node) {
|
378 |
|
|
$terms_old = &drupal_static('taxonomy_menu_terms_old');
|
379 |
|
|
// We use this direct table pull to avoid the cache and because
|
380 |
|
|
// free tags are not formated in a matter where extrating the
|
381 |
|
|
// tid's is easy.
|
382 |
|
|
$terms_new = _taxonomy_menu_get_node_terms($node);
|
383 |
|
|
|
384 |
|
|
// Merge current terms and previous terms to update both menu items.
|
385 |
|
|
$terms = array_unique(array_merge((array)$terms_new, (array)$terms_old));
|
386 |
|
|
_taxonomy_menu_nodeapi_helper('insert', $terms, $node);
|
387 |
|
|
}
|
388 |
|
|
|
389 |
|
|
/**
|
390 |
|
|
* Implements hook_node_update().
|
391 |
|
|
*/
|
392 |
|
|
function taxonomy_menu_node_update($node) {
|
393 |
|
|
$terms_old = &drupal_static('taxonomy_menu_terms_old');
|
394 |
|
|
//we use this direct table pull to avoid the cache and because
|
395 |
|
|
//free tags are not formated in a matter where extrating the
|
396 |
|
|
//tid's is easy
|
397 |
|
|
$terms_new = _taxonomy_menu_get_node_terms($node);
|
398 |
|
|
|
399 |
|
|
//merge current terms and previous terms to update both menu items.
|
400 |
|
|
$terms = array_unique(array_merge((array)$terms_new, (array)$terms_old));
|
401 |
|
|
_taxonomy_menu_nodeapi_helper('update', $terms, $node);
|
402 |
|
|
}
|
403 |
|
|
|
404 |
|
|
/**
|
405 |
|
|
* Implements hook_node_presave().
|
406 |
|
|
*/
|
407 |
|
|
function taxonomy_menu_node_presave($node) {
|
408 |
|
|
$terms_old = &drupal_static('taxonomy_menu_terms_old');
|
409 |
|
|
//get the terms from the database before the changes are made.
|
410 |
|
|
//these will be used to update the menu item's name if needed
|
411 |
|
|
//we go directly to the db to bypass any caches
|
412 |
|
|
if (isset($node->nid)) {
|
413 |
|
|
$node_old = node_load($node->nid);
|
414 |
|
|
$terms_old = _taxonomy_menu_get_node_terms($node_old);
|
415 |
|
|
}
|
416 |
|
|
else {
|
417 |
|
|
$terms_old = array();
|
418 |
|
|
}
|
419 |
|
|
}
|
420 |
|
|
|
421 |
|
|
/**
|
422 |
|
|
* Implements hook_node_delete().
|
423 |
|
|
*/
|
424 |
|
|
function taxonomy_menu_node_delete($node) {
|
425 |
|
|
// since the delete operation is run after the data is deleted
|
426 |
|
|
// pull the terms from the node object
|
427 |
|
|
$terms = _taxonomy_menu_get_node_terms($node);
|
428 |
|
|
_taxonomy_menu_nodeapi_helper('delete', $terms, $node);
|
429 |
|
|
}
|
430 |
|
|
|
431 |
|
|
/**
|
432 |
|
|
* Abstraction of hook_taxonomy_term_<operation>()
|
433 |
|
|
*/
|
434 |
|
|
function _taxonomy_menu_taxonomy_termapi_helper($term, $operation) {
|
435 |
|
|
// Only sync if taxonomy_menu is enabled for this vocab and the 'sync'
|
436 |
|
|
// option has been checked.
|
437 |
|
|
$menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $term->vid), 0);
|
438 |
|
|
$sync = variable_get(_taxonomy_menu_build_variable('sync', $term->vid), 0);
|
439 |
|
|
|
440 |
|
|
if ($menu_name && $sync) {
|
441 |
|
|
$item = array(
|
442 |
|
|
'tid' => $term->tid,
|
443 |
|
|
'vid' => $term->vid,
|
444 |
|
|
'term' => $term,
|
445 |
|
|
'menu_name' => $menu_name,
|
446 |
|
|
'mlid' => _taxonomy_menu_get_mlid($term->tid, $term->vid),
|
447 |
|
|
);
|
448 |
|
|
|
449 |
|
|
switch ($operation) {
|
450 |
|
|
case 'insert':
|
451 |
|
|
$text = 'Added term %term to taxonomy menu %menu_name.';
|
452 |
|
|
break;
|
453 |
|
|
case 'update':
|
454 |
|
|
$text = 'Updated term %term in taxonomy menu %menu_name.';
|
455 |
|
|
break;
|
456 |
|
|
case 'delete':
|
457 |
|
|
$text = 'Deleted term %term from taxonomy menu %menu_name.';
|
458 |
|
|
break;
|
459 |
|
|
}
|
460 |
|
|
$message = t($text, array('%term' => $term->name, '%menu_name' => $menu_name));
|
461 |
|
|
|
462 |
|
|
// run function
|
463 |
|
|
taxonomy_menu_handler($operation, $item);
|
464 |
|
|
|
465 |
|
|
// report status
|
466 |
|
|
drupal_set_message($message, 'status');
|
467 |
|
|
|
468 |
|
|
// rebuild the menu
|
469 |
|
|
menu_cache_clear($menu_name);
|
470 |
|
|
}
|
471 |
|
|
}
|
472 |
|
|
|
473 |
|
|
/**
|
474 |
|
|
* Builds argument arrays calls taxonomy_menu_handler.
|
475 |
|
|
*
|
476 |
|
|
* @param $op
|
477 |
|
|
* A string of the operation to be performed [update|insert|delete]
|
478 |
|
|
* @param $terms
|
479 |
|
|
* An array of tids.
|
480 |
|
|
* @param $node
|
481 |
|
|
*/
|
482 |
|
|
function _taxonomy_menu_nodeapi_helper($op, $terms = array(), $node) {
|
483 |
|
|
foreach ($terms as $key => $tid) {
|
484 |
|
|
$term = taxonomy_term_load($tid);
|
485 |
|
|
|
486 |
|
|
// update the menu for each term if necessary
|
487 |
|
|
$menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $term->vid), FALSE);
|
488 |
|
|
$vocb_sync = variable_get(_taxonomy_menu_build_variable('sync', $term->vid), TRUE);
|
489 |
|
|
$menu_num = variable_get(_taxonomy_menu_build_variable('display_num', $term->vid), TRUE);
|
490 |
|
|
$hide_empty = variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $term->vid), FALSE);
|
491 |
|
|
if ($menu_name && $vocb_sync && ($menu_num || $hide_empty)) {
|
492 |
|
|
//build argument array to save menu_item
|
493 |
|
|
$args = array(
|
494 |
|
|
'tid' => $term->tid,
|
495 |
|
|
'vid' => $term->vid,
|
496 |
|
|
'term' => $term,
|
497 |
|
|
'menu_name' => $menu_name,
|
498 |
|
|
'mlid' => _taxonomy_menu_get_mlid($term->tid, $term->vid),
|
499 |
|
|
);
|
500 |
|
|
|
501 |
|
|
if ($op == 'delete') {
|
502 |
|
|
/* Turn the op to 'update' here since we really do want to update the item
|
503 |
|
|
* and not delete/recreate it, since the latter will break hierarchy and
|
504 |
|
|
* customizations.
|
505 |
|
|
*/
|
506 |
|
|
$op = 'update';
|
507 |
|
|
}
|
508 |
|
|
|
509 |
|
|
taxonomy_menu_handler($op, $args, $node);
|
510 |
|
|
if (variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $term->vid), FALSE)) {
|
511 |
|
|
_taxonomy_menu_update_all_parents($term, $menu_name);
|
512 |
|
|
}
|
513 |
|
|
}
|
514 |
|
|
}
|
515 |
|
|
}
|
516 |
|
|
|
517 |
|
|
/**
|
518 |
|
|
* Update all parent items
|
519 |
|
|
*
|
520 |
|
|
* @param $op
|
521 |
|
|
* options are 'insert', 'update', 'delete' or path
|
522 |
|
|
*
|
523 |
|
|
* @param $node
|
524 |
|
|
* The node object.
|
525 |
|
|
*/
|
526 |
|
|
function _taxonomy_menu_update_all_parents($term, $menu_name) {
|
527 |
|
|
$parents = taxonomy_get_parents($term->tid);
|
528 |
|
|
if ($parents) {
|
529 |
|
|
foreach ($parents as $parent) {
|
530 |
|
|
$parent->parents = array_keys(taxonomy_get_parents($parent->tid));
|
531 |
|
|
$item = array(
|
532 |
|
|
'term' => $parent,
|
533 |
|
|
'menu_name' => $menu_name,
|
534 |
|
|
'mlid' => _taxonomy_menu_get_mlid($parent->tid, $parent->vid),
|
535 |
|
|
'remove' => FALSE,
|
536 |
|
|
);
|
537 |
|
|
taxonomy_menu_handler('update', $item);
|
538 |
|
|
_taxonomy_menu_update_all_parents($parent, $menu_name);
|
539 |
|
|
}
|
540 |
|
|
}
|
541 |
|
|
}
|
542 |
|
|
|
543 |
|
|
/**
|
544 |
|
|
* HANDLING
|
545 |
|
|
*
|
546 |
|
|
* @param $op
|
547 |
|
|
* options are 'insert', 'update', 'delete' or path
|
548 |
|
|
*
|
549 |
|
|
* @param $node
|
550 |
|
|
* The node object.
|
551 |
|
|
*
|
552 |
|
|
* @param $args
|
553 |
|
|
* if $op == 'insert' then
|
554 |
|
|
* array with the following key/value pairs:
|
555 |
|
|
* 'term' => term object,
|
556 |
|
|
* 'menu_name' => menu that the item is set to apply to
|
557 |
|
|
* if $op == 'delete' then
|
558 |
|
|
* array(
|
559 |
|
|
* 'tid' => TermID
|
560 |
|
|
* 'mlid => Menu ID
|
561 |
|
|
* )
|
562 |
|
|
* if $op == 'update' then
|
563 |
|
|
* 'term' => term object,
|
564 |
|
|
* 'menu_name' => menu that the item is set to apply to
|
565 |
|
|
* 'mlid' => Menu ID
|
566 |
|
|
*/
|
567 |
|
|
function taxonomy_menu_handler($op, $args = array(), $node = NULL, $item = array()) {
|
568 |
|
|
// Get the initial $item
|
569 |
|
|
if (empty($item)) {
|
570 |
|
|
$item = _taxonomy_menu_create_item($args, $node);
|
571 |
|
|
}
|
572 |
|
|
|
573 |
|
|
// Let other modules make edits
|
574 |
|
|
$hook = 'taxonomy_menu_' . $op;
|
575 |
|
|
foreach (module_implements($hook) as $module) {
|
576 |
|
|
$function = $module . '_' . $hook;
|
577 |
|
|
$function($item);
|
578 |
|
|
}
|
579 |
|
|
|
580 |
|
|
// Update the menu and return the mlid if the remove element is not true
|
581 |
|
|
if ($op != 'delete') {
|
582 |
|
|
return _taxonomy_menu_save($item);
|
583 |
|
|
}
|
584 |
|
|
}
|
585 |
|
|
|
586 |
|
|
/**
|
587 |
|
|
* Adds/Updates a taxonomy menu item.
|
588 |
|
|
*
|
589 |
|
|
* We use a custom data array $item as a parameter, instead of using a
|
590 |
|
|
* standard taxonomy $term object. This is because this function is also
|
591 |
|
|
* called from hook_taxonomy(), which doesn't have a $term object. Rather
|
592 |
|
|
* have one consistent method of passing the data.
|
593 |
|
|
*
|
594 |
|
|
* @param $item
|
595 |
|
|
* array with the following key/value pairs:
|
596 |
|
|
* 'tid' => the term id (if 0 then adding the vocab as an item)
|
597 |
|
|
* 'name' => the term's name
|
598 |
|
|
* 'description' => term description, used as to build the title attribute
|
599 |
|
|
* 'weight' => term weight
|
600 |
|
|
* (This will be overriden by the order created from taxonomy_get_tree which respects the correct wight)
|
601 |
|
|
* 'vid' => the vocabulary's id
|
602 |
|
|
* 'ptid' => the term's parent's term id
|
603 |
|
|
* 'menu_name' => the menu that the link item will be inserted into
|
604 |
|
|
* 'mlid' => if this is filled in then the mlid will be updated
|
605 |
|
|
*/
|
606 |
|
|
function _taxonomy_menu_save($item) {
|
607 |
|
|
$insert = TRUE;
|
608 |
|
|
|
609 |
|
|
$flatten_menu = variable_get(_taxonomy_menu_build_variable('flat',
|
610 |
|
|
$item['vid']));
|
611 |
|
|
// Child items should appear around the parent/root, so set their weight
|
612 |
|
|
// equal to the root term's
|
613 |
|
|
if ($flatten_menu) {
|
614 |
|
|
$item['weight'] = $item['root_term_weight'];
|
615 |
|
|
}
|
616 |
|
|
|
617 |
|
|
// create the path.
|
618 |
|
|
$path = taxonomy_menu_create_path($item['vid'], $item['tid']);
|
619 |
|
|
// get the parent mlid: this is either:
|
620 |
|
|
// - the parent tid's mlid
|
621 |
|
|
// - the vocab menu item's mlid
|
622 |
|
|
// - the menu parent setting for this vocab
|
623 |
|
|
$plid = _taxonomy_menu_get_mlid($item['ptid'], $item['vid']);
|
624 |
|
|
if (!$plid || $flatten_menu) {
|
625 |
|
|
$plid = variable_get(_taxonomy_menu_build_variable('vocab_parent', $item['vid']), NULL);
|
626 |
|
|
}
|
627 |
|
|
|
628 |
|
|
// Make sure the path has less then 256 characters
|
629 |
|
|
if (drupal_strlen($path) > 256) {
|
630 |
|
|
preg_match('/(.{256}.*?)\b/', $path, $matches);
|
631 |
|
|
$path = rtrim($matches[1]);
|
632 |
|
|
}
|
633 |
|
|
|
634 |
|
|
$link = array(
|
635 |
|
|
'link_title' => $item['name'],
|
636 |
|
|
'menu_name' => $item['menu_name'],
|
637 |
|
|
'plid' => $plid,
|
638 |
|
|
'options' => array('attributes' => array('title' => trim($item['description'])
|
639 |
|
|
? $item['description'] : $item['name'])),
|
640 |
|
|
'weight' => $item['weight'],
|
641 |
|
|
'module' => 'taxonomy_menu',
|
642 |
|
|
'expanded' => variable_get(_taxonomy_menu_build_variable('expanded', $item['vid']), TRUE),
|
643 |
|
|
'link_path' => $path,
|
644 |
|
|
);
|
645 |
|
|
|
646 |
|
|
// Add setup the query paramater in the URL correctly
|
647 |
|
|
if (strpos($path, '?') !== FALSE) {
|
648 |
|
|
$split = explode('?', $path);
|
649 |
|
|
if (strpos($split[1], '?') !== FALSE) {
|
650 |
|
|
// the query split didn't work, too many question marks
|
651 |
|
|
// error?
|
652 |
|
|
}
|
653 |
|
|
else {
|
654 |
|
|
parse_str($split[1], $link['options']['query']);
|
655 |
|
|
$link['link_path'] = $split[0];
|
656 |
|
|
}
|
657 |
|
|
}
|
658 |
|
|
|
659 |
|
|
// If passed a mlid then add it
|
660 |
|
|
if (isset($item['mlid']) && $item['mlid']) {
|
661 |
|
|
$link['mlid'] = $item['mlid'];
|
662 |
|
|
$insert = FALSE;
|
663 |
|
|
}
|
664 |
|
|
|
665 |
|
|
// FIXME: i18nmenu need to be cleaned up to allow translation from other menu module
|
666 |
|
|
if (module_exists('i18n_menu')) {
|
667 |
|
|
$link['options']['alter'] = TRUE;
|
668 |
|
|
$link['language'] = $item['language'];
|
669 |
|
|
$link['customized'] = 1;
|
670 |
|
|
}
|
671 |
|
|
|
672 |
|
|
// set the has_children property
|
673 |
|
|
// if tid=0 then adding a vocab item and had children
|
674 |
|
|
// if the term has any children then set it to true
|
675 |
|
|
if ($item['tid'] == 0) {
|
676 |
|
|
$link['has_children'] = 1;
|
677 |
|
|
}
|
678 |
|
|
else {
|
679 |
|
|
$children = taxonomy_get_children($item['tid']);
|
680 |
|
|
if (!empty($children)) {
|
681 |
|
|
$link['has_children'] = 1;
|
682 |
|
|
}
|
683 |
|
|
}
|
684 |
|
|
|
685 |
|
|
// If remove is true then set hidden to 1
|
686 |
|
|
$link['hidden'] = (isset($item['remove']) && $item['remove']) ? 1 : 0;
|
687 |
|
|
|
688 |
|
|
// Save the menu item
|
689 |
|
|
if ($mlid = menu_link_save($link)) {
|
690 |
|
|
// if inserting a new menu item then insert a record into the table
|
691 |
|
|
if ($insert) {
|
692 |
|
|
_taxonomy_menu_insert_menu_item($mlid, $item['tid'], $item['vid']);
|
693 |
|
|
}
|
694 |
|
|
return $mlid;
|
695 |
|
|
}
|
696 |
|
|
else {
|
697 |
|
|
drupal_set_message(t('Could not save the menu link for the taxonomy menu.'), 'error');
|
698 |
|
|
return FALSE;
|
699 |
|
|
}
|
700 |
|
|
}
|
701 |
|
|
|
702 |
|
|
/**
|
703 |
|
|
* Create the path to use in the menu item
|
704 |
|
|
*
|
705 |
|
|
* @return array
|
706 |
|
|
* path selections
|
707 |
|
|
*/
|
708 |
|
|
function _taxonomy_menu_get_paths() {
|
709 |
|
|
return module_invoke_all('taxonomy_menu_path');
|
710 |
|
|
}
|
711 |
|
|
|
712 |
|
|
/**
|
713 |
|
|
* Creates the path for the vid/tid combination.
|
714 |
|
|
*
|
715 |
|
|
* @param $vid
|
716 |
|
|
* @param $tid
|
717 |
|
|
* @return string
|
718 |
|
|
* path
|
719 |
|
|
*/
|
720 |
|
|
function taxonomy_menu_create_path($vid, $tid) {
|
721 |
|
|
// get the path function for this vocabulary
|
722 |
|
|
$function = variable_get(_taxonomy_menu_build_variable('path', $vid), 'taxonomy_menu_path_default');
|
723 |
|
|
// run the function
|
724 |
|
|
return $function($vid, $tid);
|
725 |
|
|
}
|
726 |
|
|
|
727 |
|
|
/**
|
728 |
|
|
* Implements hook_taxonomy_menu_path().
|
729 |
|
|
*
|
730 |
|
|
* Invoked from _taxonomy_menu_get_paths.
|
731 |
|
|
*
|
732 |
|
|
* @return array
|
733 |
|
|
* function name => Display Title
|
734 |
|
|
* a list of the path options.
|
735 |
|
|
*/
|
736 |
|
|
function taxonomy_menu_taxonomy_menu_path() {
|
737 |
|
|
$output = array(
|
738 |
|
|
'taxonomy_menu_path_default' => t('Default'),
|
739 |
|
|
);
|
740 |
|
|
|
741 |
|
|
return $output;
|
742 |
|
|
}
|
743 |
|
|
|
744 |
|
|
/**
|
745 |
|
|
* Callback for hook_taxonomy_menu_path
|
746 |
|
|
*/
|
747 |
|
|
function taxonomy_menu_path_default($vid, $tid) {
|
748 |
|
|
// if tid = 0 then we are creating the vocab menu item format will be taxonomy/term/$tid+$tid+$tid....
|
749 |
|
|
if ($tid == 0) {
|
750 |
|
|
// get all of the terms for the vocab
|
751 |
|
|
$vtids = _taxonomy_menu_get_terms($vid);
|
752 |
|
|
$end = implode(' ', $vtids);
|
753 |
|
|
$path = "taxonomy/term/$end";
|
754 |
|
|
}
|
755 |
|
|
else {
|
756 |
|
|
$path = 'taxonomy/term/' . $tid;
|
757 |
|
|
if (variable_get(_taxonomy_menu_build_variable('display_decendants', $vid), FALSE)) {
|
758 |
|
|
// Use 'all' at the end of the path
|
759 |
|
|
if (variable_get(_taxonomy_menu_build_variable('end_all', $vid), FALSE)) {
|
760 |
|
|
$path .= '/all';
|
761 |
|
|
}
|
762 |
|
|
else {
|
763 |
|
|
// we wait to run this instead of during the if above
|
764 |
|
|
// because we only wan to run it once.
|
765 |
|
|
$terms = taxonomy_get_tree($vid, $tid);
|
766 |
|
|
foreach ($terms as $term) {
|
767 |
|
|
$tids[] = $term->tid;
|
768 |
|
|
}
|
769 |
|
|
if ($tids) {
|
770 |
|
|
$end = implode(' ', $tids);
|
771 |
|
|
$path .= ' ' . $end;
|
772 |
|
|
}
|
773 |
|
|
}
|
774 |
|
|
}
|
775 |
|
|
}
|
776 |
|
|
|
777 |
|
|
return $path;
|
778 |
|
|
}
|
779 |
|
|
|
780 |
|
|
/**
|
781 |
|
|
* hook_taxonomy_menu_delete
|
782 |
|
|
*
|
783 |
|
|
* @param $args
|
784 |
|
|
* array(
|
785 |
|
|
* 'vid' => Vocab ID
|
786 |
|
|
* 'tid' => TermID
|
787 |
|
|
* 'mlid' => Menu ID
|
788 |
|
|
* )
|
789 |
|
|
*
|
790 |
|
|
*/
|
791 |
|
|
function taxonomy_menu_taxonomy_menu_delete(&$item) {
|
792 |
|
|
menu_link_delete($item['mlid']);
|
793 |
|
|
_taxonomy_menu_delete_item($item['vid'], $item['tid']);
|
794 |
|
|
unset($item['mlid']);
|
795 |
|
|
|
796 |
|
|
}
|
797 |
|
|
|
798 |
|
|
/**
|
799 |
|
|
* Create the initial $item array
|
800 |
|
|
*
|
801 |
|
|
* @param $args
|
802 |
|
|
* array with the following key/value pairs:
|
803 |
|
|
* 'term' => term object, if updating a term
|
804 |
|
|
* 'menu_name' => menu that the item is set to apply to
|
805 |
|
|
* 'vid' => vocab id. if editing vocab item
|
806 |
|
|
* 'mlid' => menu id
|
807 |
|
|
*
|
808 |
|
|
* @param $node
|
809 |
|
|
* The node object.
|
810 |
|
|
*/
|
811 |
|
|
function _taxonomy_menu_create_item($args = array(), $node) {
|
812 |
|
|
|
813 |
|
|
// If tid = 0, then we are creating a vocab item
|
814 |
|
|
if (isset($args['tid']) && isset($args['vid']) && $args['tid'] == 0 && variable_get(_taxonomy_menu_build_variable('voc_item', $args['vid']), 0)) {
|
815 |
|
|
$vocab = taxonomy_vocabulary_load($args['vid']);
|
816 |
|
|
$item = array(
|
817 |
|
|
'tid' => 0,
|
818 |
|
|
'name' => $vocab->name,
|
819 |
|
|
'description' => variable_get(_taxonomy_menu_build_variable('voc_item_description', $args['vid']), 0) ? $vocab->description : '',
|
820 |
|
|
'weight' => $vocab->weight,
|
821 |
|
|
'vid' => $args['vid'],
|
822 |
|
|
'ptid' => 0,
|
823 |
|
|
'root_term_weight' => $vocab->weight,
|
824 |
|
|
'menu_name' => $args['menu_name'],
|
825 |
|
|
'language' => $vocab->language,
|
826 |
|
|
);
|
827 |
|
|
}
|
828 |
|
|
else {
|
829 |
|
|
// If tid <> 0 then we are creating a term item.
|
830 |
|
|
$term = $args['term'];
|
831 |
|
|
|
832 |
|
|
// Sometimes $term->parents is not set so we find it.
|
833 |
|
|
if (empty($term->parents)) {
|
834 |
|
|
$term->parents = _taxonomy_menu_get_parents($term->tid);
|
835 |
|
|
if (empty($term->parents)) {
|
836 |
|
|
// even without parents, create one with $ptid = 0
|
837 |
|
|
$term->parents = array(0 => '0');
|
838 |
|
|
}
|
839 |
|
|
}
|
840 |
|
|
|
841 |
|
|
// Find the weight of the root taxonomy term; we'll need it in case we want
|
842 |
|
|
// a flat taxonomy menu.
|
843 |
|
|
if (is_object($term)) {
|
844 |
|
|
$term_parents = taxonomy_get_parents_all($term->tid);
|
845 |
|
|
$root_term_weight = $term_parents[count($term_parents) - 1]->weight;
|
846 |
|
|
}
|
847 |
|
|
else {
|
848 |
|
|
$root_term_weight = 0;
|
849 |
|
|
}
|
850 |
|
|
|
851 |
|
|
foreach ($term->parents as $parent) {
|
852 |
|
|
$ptid = $parent;
|
853 |
|
|
// turn the term into the correct $item array form
|
854 |
|
|
$item = array(
|
855 |
|
|
'tid' => $term->tid,
|
856 |
|
|
'name' => $term->name,
|
857 |
|
|
'description' => variable_get(_taxonomy_menu_build_variable('term_item_description', $term->vid), 0) ? $term->description : '',
|
858 |
|
|
'weight' => $term->weight,
|
859 |
|
|
'vid' => $term->vid,
|
860 |
|
|
'ptid' => $ptid,
|
861 |
|
|
'root_term_weight' => $root_term_weight,
|
862 |
|
|
'menu_name' => $args['menu_name'],
|
863 |
|
|
'language' => isset($term->language) ? $term->language : ($node ? $node->language : $GLOBALS['language']->language),
|
864 |
|
|
);
|
865 |
|
|
if (isset($args['mlid'])) {
|
866 |
|
|
$item['mlid'] = $args['mlid'];
|
867 |
|
|
}
|
868 |
|
|
// Mutiple parents are not supported yet, hence this break.
|
869 |
|
|
// without the break, the item is inserted multiple under one parent, instead once under each parent.
|
870 |
|
|
break;
|
871 |
|
|
}
|
872 |
|
|
}
|
873 |
|
|
|
874 |
|
|
return $item;
|
875 |
|
|
}
|
876 |
|
|
|
877 |
|
|
/**
|
878 |
|
|
* Helper function to see if any of the children have any nodes
|
879 |
|
|
*
|
880 |
|
|
* @param $tid
|
881 |
|
|
* @param $vid
|
882 |
|
|
* @return boolean
|
883 |
|
|
*/
|
884 |
|
|
function _taxonomy_menu_children_has_nodes($tid, $vid, $return = FALSE) {
|
885 |
|
|
$children = taxonomy_get_children($tid, $vid);
|
886 |
|
|
foreach ($children as $tid => $term) {
|
887 |
|
|
if (_taxonomy_menu_term_count($tid) > 0) {
|
888 |
|
|
$return = TRUE;
|
889 |
|
|
}
|
890 |
|
|
else {
|
891 |
|
|
$return = _taxonomy_menu_children_has_nodes($tid, $vid, $return);
|
892 |
|
|
}
|
893 |
|
|
}
|
894 |
|
|
return $return;
|
895 |
|
|
}
|
896 |
|
|
|
897 |
|
|
/**
|
898 |
|
|
* Helper function for insert and update hooks
|
899 |
|
|
*
|
900 |
|
|
* @param $item
|
901 |
|
|
* @return array
|
902 |
|
|
*/
|
903 |
|
|
function _taxonomy_menu_item($item) {
|
904 |
|
|
// if tid is 0 then do not change any settings
|
905 |
|
|
if ($item['tid'] > 0) {
|
906 |
|
|
// get the number of node attached to this term
|
907 |
|
|
$num = _taxonomy_menu_term_count($item['tid']);
|
908 |
|
|
|
909 |
|
|
// if hide menu is selected and the term count is 0 and the term has no children then do not create the menu item
|
910 |
|
|
if ($num == 0 &&
|
911 |
|
|
variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $item['vid']), FALSE) &&
|
912 |
|
|
!_taxonomy_menu_children_has_nodes($item['tid'], $item['vid'])) {
|
913 |
|
|
|
914 |
|
|
$item['remove'] = TRUE;
|
915 |
|
|
return $item;
|
916 |
|
|
}
|
917 |
|
|
|
918 |
|
|
// if display number is selected and $num > 0 then change the title
|
919 |
|
|
if (variable_get(_taxonomy_menu_build_variable('display_num', $item['vid']), FALSE)) {
|
920 |
|
|
// if number > 0 and display decendants, then count all of the children
|
921 |
|
|
if (variable_get(_taxonomy_menu_build_variable('display_descendants', $item['vid']), FALSE)) {
|
922 |
|
|
$num = taxonomy_menu_term_count_nodes($item['tid'], $item['vid']);
|
923 |
|
|
}
|
924 |
|
|
$item['name'] .= " ($num)";
|
925 |
|
|
}
|
926 |
|
|
}
|
927 |
|
|
elseif ($item['tid'] == 0) {
|
928 |
|
|
// if custom name is provided, use that name
|
929 |
|
|
$custom_name = variable_get(_taxonomy_menu_build_variable('voc_name', $item['vid']), '');
|
930 |
|
|
if (!empty($custom_name)) {
|
931 |
|
|
$item['name'] = $custom_name;
|
932 |
|
|
}
|
933 |
|
|
}
|
934 |
|
|
|
935 |
|
|
return $item;
|
936 |
|
|
}
|
937 |
|
|
|
938 |
|
|
|
939 |
|
|
/**
|
940 |
|
|
* Calculates the number of nodes linked to the term and all children
|
941 |
|
|
* @param $tid
|
942 |
|
|
* @param $vid
|
943 |
|
|
* @return integer
|
944 |
|
|
*/
|
945 |
|
|
function taxonomy_menu_term_count_nodes($tid, $vid, $count = 0) {
|
946 |
|
|
$count += _taxonomy_menu_term_count($tid);
|
947 |
|
|
$children = taxonomy_get_children($tid, $vid);
|
948 |
|
|
foreach ($children as $tid => $term) {
|
949 |
|
|
$count = taxonomy_menu_term_count_nodes($term->tid, $term->vid, $count);
|
950 |
|
|
}
|
951 |
|
|
return $count;
|
952 |
|
|
}
|
953 |
|
|
|
954 |
|
|
|
955 |
|
|
/**
|
956 |
|
|
* Implements hook_taxonomy_menu_insert().
|
957 |
|
|
*
|
958 |
|
|
* @param $item
|
959 |
|
|
* array with the following key/value pairs:
|
960 |
|
|
* 'tid' => the term id (if 0 then updating the vocab as an item)
|
961 |
|
|
* 'name' => new menu name
|
962 |
|
|
* 'description' => new menu description, used as to build the title attribute
|
963 |
|
|
* 'weight' => new menu weight
|
964 |
|
|
* 'vid' => the new vocabulary's id
|
965 |
|
|
* 'ptid' => the new parent tid
|
966 |
|
|
* 'remove' => if this is set to TRUE then the $item is not added as a menu
|
967 |
|
|
*
|
968 |
|
|
* @return $item
|
969 |
|
|
*/
|
970 |
|
|
function taxonomy_menu_taxonomy_menu_insert(&$item) {
|
971 |
|
|
$item = _taxonomy_menu_item($item);
|
972 |
|
|
}
|
973 |
|
|
|
974 |
|
|
/**
|
975 |
|
|
* Implements hook_taxonomy_menu_update().
|
976 |
|
|
*
|
977 |
|
|
* @param $item
|
978 |
|
|
* array with the following key/value pairs:
|
979 |
|
|
* 'tid' => the term id (if 0 then updating the vocab as an item)
|
980 |
|
|
* 'name' => new menu name
|
981 |
|
|
* 'description' => new menu description, used as to build the title attribute
|
982 |
|
|
* 'weight' => new menu weight
|
983 |
|
|
* 'vid' => the new vocabulary's id
|
984 |
|
|
* 'ptid' => the new parent tid
|
985 |
|
|
* 'mlid' => mlid that needs to be edited
|
986 |
|
|
* 'remove' => if this is set to TRUE then the $item is not added as a menu
|
987 |
|
|
*
|
988 |
|
|
*/
|
989 |
|
|
function taxonomy_menu_taxonomy_menu_update(&$item) {
|
990 |
|
|
$item = _taxonomy_menu_item($item);
|
991 |
|
|
}
|
992 |
|
|
|
993 |
|
|
/**
|
994 |
|
|
* Used to create a form array of taxonomy menu options
|
995 |
|
|
* invokes hook_taxonomy_menu_options().
|
996 |
|
|
*
|
997 |
|
|
* @return $form array
|
998 |
|
|
*/
|
999 |
|
|
function _taxonomy_menu_create_options($vid) {
|
1000 |
|
|
$options = module_invoke_all('taxonomy_menu_options');
|
1001 |
|
|
|
1002 |
|
|
// cycle through field
|
1003 |
|
|
foreach ($options as $field_name => $field_elements) {
|
1004 |
|
|
// cycle through each value of the field
|
1005 |
|
|
$variable_name = _taxonomy_menu_build_variable($field_name, $vid);
|
1006 |
|
|
|
1007 |
|
|
// if the variable is set then use that, if the default key is set then use that, otherwise use false
|
1008 |
|
|
$options[$field_name]['#default_value'] =
|
1009 |
|
|
variable_get($variable_name,
|
1010 |
|
|
!empty($options[$field_name]['default']) ? $options[$field_name]['default'] : FALSE);
|
1011 |
|
|
|
1012 |
|
|
// set the type to checkbox if it is empty
|
1013 |
|
|
if (empty($options[$field_name]['#type'])) {
|
1014 |
|
|
$options[$field_name]['#type'] = 'checkbox';
|
1015 |
|
|
}
|
1016 |
|
|
|
1017 |
|
|
// set the option feildset values
|
1018 |
|
|
$options['#type'] = 'fieldset';
|
1019 |
|
|
$options['#title'] = t('Options');
|
1020 |
|
|
$options['#collapsible'] = TRUE;
|
1021 |
|
|
|
1022 |
|
|
// remove the default value from the array so we don't pass it to the form
|
1023 |
|
|
unset($options[$field_name]['default']);
|
1024 |
|
|
}
|
1025 |
|
|
|
1026 |
|
|
return $options;
|
1027 |
|
|
}
|
1028 |
|
|
|
1029 |
|
|
/**
|
1030 |
|
|
* Builds a variable from the supplied name and machine name of the vocabulary.
|
1031 |
|
|
*
|
1032 |
|
|
* @param $name
|
1033 |
|
|
* String to be added to the returned variable.
|
1034 |
|
|
* @param $vid
|
1035 |
|
|
* VID of the vocabulary from which the machine name will be taken.
|
1036 |
|
|
*
|
1037 |
|
|
* @return bool|string
|
1038 |
|
|
*/
|
1039 |
|
|
function _taxonomy_menu_build_variable($name, $vid) {
|
1040 |
|
|
$vocabulary = taxonomy_vocabulary_load($vid);
|
1041 |
|
|
if ($vocabulary) {
|
1042 |
|
|
return 'taxonomy_menu_' . $name . '_' . $vocabulary->machine_name;
|
1043 |
|
|
}
|
1044 |
|
|
else {
|
1045 |
|
|
return FALSE;
|
1046 |
|
|
}
|
1047 |
|
|
|
1048 |
|
|
}
|
1049 |
|
|
|
1050 |
|
|
/**
|
1051 |
|
|
* Implements hook_taxonomy_menu_options().
|
1052 |
|
|
*
|
1053 |
|
|
* @return array
|
1054 |
|
|
* Uses the value to set the variable taxonomy_menu_<value>_<machine_name>
|
1055 |
|
|
* $options[value]
|
1056 |
|
|
* default - optional. this is what will be used if the varialbe is not set. if empty then FALSE is used
|
1057 |
|
|
* #title - required.
|
1058 |
|
|
* any other form element
|
1059 |
|
|
*/
|
1060 |
|
|
function taxonomy_menu_taxonomy_menu_options() {
|
1061 |
|
|
|
1062 |
|
|
$options['sync'] = array(
|
1063 |
|
|
'#title' => t('Synchronise changes to this vocabulary'),
|
1064 |
|
|
'#description' => t('Every time a term is added/deleted/modified, the corresponding menu link will be altered too.'),
|
1065 |
|
|
'default' => TRUE,
|
1066 |
|
|
);
|
1067 |
|
|
|
1068 |
|
|
$options['display_num'] = array(
|
1069 |
|
|
'#title' => t('Display number of items'),
|
1070 |
|
|
'#description' => t('Display the number of items per taxonomy terms. Will not show up for vocabulary menu items.'),
|
1071 |
|
|
'default' => FALSE,
|
1072 |
|
|
);
|
1073 |
|
|
|
1074 |
|
|
$options['hide_empty_terms'] = array(
|
1075 |
|
|
'#title' => t('Hide empty terms'),
|
1076 |
|
|
'#description' => t('Hide terms with no items attached to them.'),
|
1077 |
|
|
'default' => FALSE,
|
1078 |
|
|
);
|
1079 |
|
|
|
1080 |
|
|
$options['voc_item'] = array(
|
1081 |
|
|
'#title' => t('Add item for vocabulary'),
|
1082 |
|
|
'#description' => t('Shows the vocabulary name as the top level menu item of the taxonomy menu.'),
|
1083 |
|
|
'default' => FALSE,
|
1084 |
|
|
'#disabled' => TRUE,
|
1085 |
|
|
);
|
1086 |
|
|
|
1087 |
|
|
$options['voc_item_description'] = array(
|
1088 |
|
|
'#title' => t('Add description for vocabulary'),
|
1089 |
|
|
'#description' => t('Add the vocabulary description to the vocabulary menu item.'),
|
1090 |
|
|
'default' => FALSE,
|
1091 |
|
|
);
|
1092 |
|
|
|
1093 |
|
|
$options['term_item_description'] = array(
|
1094 |
|
|
'#title' => t('Add description for terms'),
|
1095 |
|
|
'#description' => t('Add the term description to the term menu item.'),
|
1096 |
|
|
'default' => FALSE,
|
1097 |
|
|
);
|
1098 |
|
|
|
1099 |
|
|
$options['expanded'] = array(
|
1100 |
|
|
'#title' => t('Auto expand menu items'),
|
1101 |
|
|
'#description' => t('Automatically show all menu items as expanded.'),
|
1102 |
|
|
'default' => TRUE,
|
1103 |
|
|
);
|
1104 |
|
|
|
1105 |
|
|
$options['flat'] = array(
|
1106 |
|
|
'#title' => t('Flatten the taxonomy\'s hierarchy in the menu'),
|
1107 |
|
|
'#description' => t('Add all menu items to the same level rather than hierarchically.'),
|
1108 |
|
|
'default' => FALSE,
|
1109 |
|
|
);
|
1110 |
|
|
|
1111 |
|
|
$options['voc_name'] = array(
|
1112 |
|
|
'#type' => 'textfield',
|
1113 |
|
|
'#title' => t('Custom name for vocabulary item'),
|
1114 |
|
|
'#description' => t('Changes the name of the vocabulary item (if enabled above). Leave blank to use the name of the vocabulary.'),
|
1115 |
|
|
'default' => '',
|
1116 |
|
|
'#disabled' => TRUE,
|
1117 |
|
|
);
|
1118 |
|
|
|
1119 |
|
|
$options['display_descendants'] = array(
|
1120 |
|
|
'#title' => t('Display descendants'),
|
1121 |
|
|
'#description' => t('Changes the default path to taxonomy/term/tid+tid+tid for all terms thave have child terms.'),
|
1122 |
|
|
'default' => FALSE,
|
1123 |
|
|
);
|
1124 |
|
|
|
1125 |
|
|
$options['end_all'] = array(
|
1126 |
|
|
'#title' => t("Use 'all' at the end of URL"),
|
1127 |
|
|
'default' => FALSE,
|
1128 |
|
|
'#description' => t('This changes tid+tid+tid to "All" in term when <em>Display descendants</em> has been selected.<br />Only used if <em>Menu path type</em> is "Default path".<br />Works with default taxonomy page.'),
|
1129 |
|
|
'#disabled' => TRUE,
|
1130 |
|
|
);
|
1131 |
|
|
|
1132 |
|
|
return $options;
|
1133 |
|
|
}
|
1134 |
|
|
|
1135 |
|
|
|
1136 |
|
|
/**
|
1137 |
|
|
* Implements hook_translated_menu_link_alter().
|
1138 |
|
|
*
|
1139 |
|
|
* Translate menu links on the fly by using term translations.
|
1140 |
|
|
*
|
1141 |
|
|
*/
|
1142 |
|
|
function taxonomy_menu_translated_menu_link_alter(&$item, $map) {
|
1143 |
|
|
if (module_exists('i18n_taxonomy')) {
|
1144 |
|
|
// In case of localized terms, use term translation for menu title.
|
1145 |
|
|
if ($item['module'] == 'taxonomy_menu') {
|
1146 |
|
|
$t = _taxonomy_menu_get_item($item['mlid']);
|
1147 |
|
|
// Only translate when term exist (may per example occur with stray menu item)
|
1148 |
|
|
if ($t) {
|
1149 |
|
|
// Only translate when translation mode is set to localize
|
1150 |
|
|
if (i18n_taxonomy_vocabulary_mode($t->vid, I18N_MODE_LOCALIZE)) {
|
1151 |
|
|
// this is a term
|
1152 |
|
|
if ($t->tid > 0) {
|
1153 |
|
|
$term = taxonomy_term_load($t->tid);
|
1154 |
|
|
$display_num = '';
|
1155 |
|
|
$num = _taxonomy_menu_term_count($t->tid);
|
1156 |
|
|
|
1157 |
|
|
// If hide menu is selected and the term count is 0 and the term has no children then do not create the menu item
|
1158 |
|
|
if ($num == 0 && variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $t->vid), FALSE) && !_taxonomy_menu_children_has_nodes($t->tid, $t->vid)) {
|
1159 |
|
|
$display_num = '';
|
1160 |
|
|
}
|
1161 |
|
|
// if display number is selected and $num > 0 then change the title
|
1162 |
|
|
elseif (variable_get(_taxonomy_menu_build_variable('display_num', $t->vid), FALSE)) {
|
1163 |
|
|
// if number > 0 and display decendants, then count all of the children
|
1164 |
|
|
if (variable_get(_taxonomy_menu_build_variable('display_descendants', $t->vid), FALSE)) {
|
1165 |
|
|
$num = taxonomy_menu_term_count_nodes($t->tid, $t->vid);
|
1166 |
|
|
}
|
1167 |
|
|
$display_num = " ($num)";
|
1168 |
|
|
}
|
1169 |
|
|
|
1170 |
|
|
if ($item['title'] != ($term->name . $display_num)) {
|
1171 |
|
|
// Should not happen
|
1172 |
|
|
watchdog('error', 'Menu and taxonomy name mismatch: @title != @name', array('@title' => $item['title'], '@name' => $term->name . $display_num));
|
1173 |
|
|
}
|
1174 |
|
|
|
1175 |
|
|
$term = i18n_taxonomy_localize_terms($term);
|
1176 |
|
|
$item['title'] = $item['link_title'] = $term->name . $display_num;
|
1177 |
|
|
if ($term->description) {
|
1178 |
|
|
$item['options']['attributes']['title'] = $term->description;
|
1179 |
|
|
}
|
1180 |
|
|
}
|
1181 |
|
|
// is a vocabulary
|
1182 |
|
|
else {
|
1183 |
|
|
$vocab = taxonomy_vocabulary_load($t->vid);
|
1184 |
|
|
$item['title'] = i18n_string('taxonomy:vocabulary:' . $vocab->vid . ':name', $vocab->name);
|
1185 |
|
|
}
|
1186 |
|
|
}
|
1187 |
|
|
}
|
1188 |
|
|
// no term, add a watchdog entry to help
|
1189 |
|
|
else {
|
1190 |
|
|
watchdog('taxonomy_menu', 'Error with menu entry "%me" in menu "%mt"', array('%me' => $item['title'], '%mt' => $item['menu_name']));
|
1191 |
|
|
}
|
1192 |
|
|
}
|
1193 |
|
|
}
|
1194 |
|
|
} |