2 |
2 |
|
3 |
3 |
/**
|
4 |
4 |
* @file
|
5 |
|
* It generates menu links for all selected taxonomy terms
|
|
5 |
* Adds links to taxonomy terms into a menu.
|
6 |
6 |
*/
|
7 |
7 |
|
8 |
|
//include the database layer
|
9 |
|
require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'taxonomy_menu') . '/taxonomy_menu.database.inc');
|
|
8 |
// Include the database layer.
|
|
9 |
module_load_include('inc', 'taxonomy_menu', 'taxonomy_menu.database');
|
10 |
10 |
|
11 |
|
//include the batch functions
|
12 |
|
require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'taxonomy_menu') . '/taxonomy_menu.batch.inc');
|
|
11 |
// Include the batch functions.
|
|
12 |
module_load_include('inc', 'taxonomy_menu', 'taxonomy_menu.batch');
|
|
13 |
|
|
14 |
/**
|
|
15 |
* Implements hook_help().
|
|
16 |
*/
|
|
17 |
function taxonomy_menu_help($path, $arg) {
|
|
18 |
switch ($path) {
|
|
19 |
case 'admin/help#taxonomy_menu':
|
|
20 |
$output = '';
|
|
21 |
$output .= '<p>' . t('The Taxonomy Menu module transforms your taxonomy vocabularies into menus.') . '</p>';
|
|
22 |
$output .= '<p>' . t('For more information, please visit the official <a href="@url">project page</a> on Drupal.org.', array('@url' => 'https://www.drupal.org/project/taxonomy_menu')) . '</p>';
|
|
23 |
return $output;
|
|
24 |
}
|
|
25 |
}
|
13 |
26 |
|
14 |
27 |
/**
|
15 |
28 |
* Implements hook_form_alter().
|
... | ... | |
19 |
32 |
*/
|
20 |
33 |
function taxonomy_menu_form_alter(&$form, &$form_state, $form_id) {
|
21 |
34 |
if ($form_id == 'taxonomy_form_vocabulary') {
|
22 |
|
// do not alter on deletion
|
|
35 |
// Do not alter on deletion.
|
23 |
36 |
if (isset($form_state['confirm_delete']) && isset($form_state['values']['vid'])) {
|
24 |
37 |
return;
|
25 |
38 |
}
|
... | ... | |
61 |
74 |
'#title' => t('Menu location'),
|
62 |
75 |
'#default_value' => $default,
|
63 |
76 |
'#options' => $menu_items,
|
64 |
|
'#description' => t('The menu and parent under which to insert taxonomy menu items.'),
|
|
77 |
'#description' => t('Taxonomy menu items will be inserted below the item selected here.'),
|
65 |
78 |
'#attributes' => array('class' => array('menu-title-select')),
|
66 |
79 |
);
|
67 |
80 |
|
... | ... | |
73 |
86 |
'#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 |
87 |
);
|
75 |
88 |
|
76 |
|
//get taxonomy menu form options
|
|
89 |
// Get taxonomy menu form options.
|
77 |
90 |
if (isset($form['vid']) && $form['vid']['#value']) {
|
78 |
91 |
$vid = $form['vid']['#value'];
|
79 |
92 |
}
|
... | ... | |
82 |
95 |
}
|
83 |
96 |
$form['taxonomy_menu']['options'] = _taxonomy_menu_create_options($vid);
|
84 |
97 |
|
85 |
|
//rebuild the menu
|
|
98 |
// Rebuild the menu.
|
86 |
99 |
$form['taxonomy_menu']['options']['rebuild'] = array(
|
87 |
100 |
'#type' => 'checkbox',
|
88 |
|
'#title' => t('Select to rebuild the menu on submit.'),
|
|
101 |
'#title' => t('Rebuild the menu on submit'),
|
89 |
102 |
'#default_value' => 0,
|
90 |
103 |
'#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.'),
|
|
104 |
'#description' => t('<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 |
105 |
);
|
93 |
|
// move the buttons to the bottom of the form
|
|
106 |
// Move the buttons to the bottom of the form.
|
94 |
107 |
$form['submit']['#weight'] = 49;
|
95 |
108 |
$form['delete']['#weight'] = 50;
|
96 |
109 |
|
97 |
|
// add an extra submit handler to save these settings
|
|
110 |
// Add an extra submit handler to save these settings.
|
98 |
111 |
$form['#submit'][] = 'taxonomy_menu_vocab_submit';
|
99 |
112 |
|
100 |
113 |
}
|
101 |
114 |
elseif ($form_id == "taxonomy_overview_terms") {
|
102 |
|
// add an extra submit handler to sync the rearranged terms with menu
|
|
115 |
// Add an extra submit handler to sync the rearranged terms with menu.
|
103 |
116 |
// @ TODO: using hook_taxonomy_vocabulary_update is nicer then callback,
|
104 |
|
// but gives less info and does not always fire.
|
|
117 |
// But gives less info and does not always fire.
|
105 |
118 |
$form['#submit'][] = 'taxonomy_menu_overview_submit';
|
106 |
119 |
}
|
107 |
120 |
}
|
... | ... | |
117 |
130 |
$changed = FALSE;
|
118 |
131 |
|
119 |
132 |
if (is_numeric($form_state['values']['taxonomy_menu']['vocab_parent'])) {
|
120 |
|
// Menu location has been set to disabled, don't want to throw notices
|
|
133 |
// Menu location has been set to disabled, don't want to throw notices.
|
121 |
134 |
$form_state['values']['taxonomy_menu']['vocab_parent'] = '0:0';
|
122 |
135 |
}
|
123 |
136 |
|
124 |
137 |
// Split the menu location into menu name and menu item id.
|
125 |
138 |
list($vocab_parent['vocab_menu'], $vocab_parent['vocab_parent']) = explode(':', $form_state['values']['taxonomy_menu']['vocab_parent']);
|
126 |
139 |
|
127 |
|
// Init flag variables to avoid notices if changes haven't happened
|
|
140 |
// Init flag variables to avoid notices if changes haven't happened.
|
128 |
141 |
$changed_menu = FALSE;
|
129 |
142 |
$change_vocab_item = FALSE;
|
130 |
143 |
$changed_path = FALSE;
|
131 |
144 |
|
132 |
|
// Set the menu name and check for changes
|
|
145 |
// Set the menu name and check for changes.
|
133 |
146 |
$variable_name = _taxonomy_menu_build_variable('vocab_menu', $vid);
|
134 |
147 |
if (_taxonomy_menu_check_variable($variable_name, $vocab_parent['vocab_menu'])) {
|
135 |
148 |
$changed_menu = TRUE;
|
136 |
149 |
}
|
137 |
150 |
variable_set($variable_name, $vocab_parent['vocab_menu']);
|
138 |
151 |
|
139 |
|
// Set the menu parent item and check for changes
|
|
152 |
// Set the menu parent item and check for changes.
|
140 |
153 |
$variable_name = _taxonomy_menu_build_variable('vocab_parent', $vid);
|
141 |
154 |
if (_taxonomy_menu_check_variable($variable_name, $vocab_parent['vocab_parent'])) {
|
142 |
155 |
$changed_menu = TRUE;
|
143 |
156 |
}
|
144 |
157 |
variable_set($variable_name, $vocab_parent['vocab_parent']);
|
145 |
158 |
|
146 |
|
// Set the path and check for changes
|
|
159 |
// Set the path and check for changes.
|
147 |
160 |
$variable_name = _taxonomy_menu_build_variable('path', $vid);
|
148 |
161 |
if (_taxonomy_menu_check_variable($variable_name, $form_state['values']['taxonomy_menu']['path'])) {
|
149 |
162 |
$changed_path = TRUE;
|
... | ... | |
154 |
167 |
// Create the variable name
|
155 |
168 |
$variable_name = _taxonomy_menu_build_variable($key, $vid);
|
156 |
169 |
|
157 |
|
// Check to see if the vocab enable options has changed
|
|
170 |
// Check to see if the vocab enable options has changed.
|
158 |
171 |
if ($key == 'voc_item') {
|
159 |
172 |
if (_taxonomy_menu_check_variable($variable_name, $value)) {
|
160 |
173 |
$change_vocab_item = TRUE;
|
161 |
174 |
}
|
162 |
175 |
}
|
163 |
176 |
|
164 |
|
// If $changed is alreayd set to true, then don't bother checking any others.
|
|
177 |
// If $changed is already set to true, then don't bother checking any others.
|
165 |
178 |
if (!$changed) {
|
166 |
179 |
// Check to see if the variable has changed.
|
167 |
180 |
if (_taxonomy_menu_check_variable($variable_name, $value)) {
|
... | ... | |
190 |
203 |
// Only send a message if one has been created.
|
191 |
204 |
if (isset($message) && $message) {
|
192 |
205 |
// $message is sanitized coming out of its source function,
|
193 |
|
// no need to reclean it here
|
|
206 |
// No need to reclean it here.
|
194 |
207 |
drupal_set_message($message, 'status');
|
195 |
208 |
}
|
196 |
209 |
}
|
197 |
210 |
}
|
198 |
211 |
|
199 |
212 |
/**
|
200 |
|
* Submit handler, reacting on form ID: taxonomy_overview_terms
|
|
213 |
* Submit handler, reacting on form ID: taxonomy_overview_terms.
|
201 |
214 |
*/
|
202 |
215 |
function taxonomy_menu_overview_submit(&$form, &$form_state) {
|
203 |
216 |
// Only sync if taxonomy_menu is enabled for this vocab and the 'sync'
|
... | ... | |
215 |
228 |
$vid = $form['#vocabulary']->vid;
|
216 |
229 |
}
|
217 |
230 |
elseif ($form_state['rebuild'] == TRUE && isset($form['#vocabulary']->vid) ) {
|
218 |
|
// Try to catch the 'Reset to alphabetical' button
|
|
231 |
// Try to catch the 'Reset to alphabetical' button.
|
219 |
232 |
$vid = NULL;
|
220 |
233 |
}
|
221 |
234 |
elseif ($form_state['rebuild'] == FALSE && isset($form['vid']['#value']) ) {
|
... | ... | |
236 |
249 |
|
237 |
250 |
// Report status.
|
238 |
251 |
if (isset($message)) {
|
239 |
|
// message is sanitized coming out of _taxonomy_menu_update_link_items
|
240 |
|
// no need to reclean it here
|
|
252 |
// Message is sanitized coming out of _taxonomy_menu_update_link_items
|
|
253 |
// no need to reclean it here.
|
241 |
254 |
drupal_set_message($message, 'status');
|
242 |
255 |
}
|
243 |
256 |
|
... | ... | |
248 |
261 |
}
|
249 |
262 |
|
250 |
263 |
/**
|
251 |
|
* rebuilds a menu
|
|
264 |
* Rebuilds a menu.
|
252 |
265 |
*
|
253 |
|
* @param $vid
|
254 |
|
* @return $message
|
255 |
|
* message that is displayed
|
|
266 |
* @param int $vid
|
|
267 |
* Vocabulary ID.
|
|
268 |
*
|
|
269 |
* @return string
|
|
270 |
* Message that is displayed.
|
256 |
271 |
*/
|
257 |
272 |
function _taxonomy_menu_rebuild($vid) {
|
258 |
|
// Remove all of the menu items for this vocabulary
|
|
273 |
// Remove all of the menu items for this vocabulary.
|
259 |
274 |
_taxonomy_menu_delete_all($vid);
|
260 |
275 |
|
261 |
|
// Only insert the links if a menu is set
|
|
276 |
// Only insert the links if a menu is set.
|
262 |
277 |
if (variable_get(_taxonomy_menu_build_variable('vocab_menu', $vid), FALSE)) {
|
263 |
278 |
_taxonomy_menu_insert_link_items($vid);
|
264 |
279 |
menu_rebuild();
|
... | ... | |
272 |
287 |
/**
|
273 |
288 |
* Checks to see if the variable has changed.
|
274 |
289 |
*
|
275 |
|
* @param $variable
|
276 |
|
* name of variable
|
277 |
|
* @return Boolean
|
278 |
|
* TRUE if it has changed
|
|
290 |
* @param string $variable
|
|
291 |
* Name of variable.
|
|
292 |
*
|
|
293 |
* @return bool
|
|
294 |
* TRUE if it has changed
|
279 |
295 |
*/
|
280 |
296 |
function _taxonomy_menu_check_variable($variable, $new_value) {
|
281 |
297 |
if ($new_value != variable_get($variable, FALSE)) {
|
... | ... | |
285 |
301 |
}
|
286 |
302 |
|
287 |
303 |
/**
|
288 |
|
* Update the menu items
|
|
304 |
* Updates the menu items.
|
289 |
305 |
*
|
290 |
306 |
* @param $vid
|
291 |
|
* vocab id
|
|
307 |
* Vocabulary ID.
|
292 |
308 |
*/
|
293 |
309 |
function _taxonomy_menu_update_link_items($vid) {
|
294 |
310 |
$menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $vid), FALSE);
|
|
311 |
$depth = variable_get(_taxonomy_menu_build_variable('max_depth', $vid), 0);
|
295 |
312 |
|
296 |
|
// Get a list of the current tid - menu_link combinations
|
|
313 |
// Get a list of the current tid - menu_link combinations.
|
297 |
314 |
$menu_links = _taxonomy_menu_get_menu_items($vid);
|
298 |
315 |
|
299 |
|
// Cycle through the menu links
|
|
316 |
// Cycle through the menu links.
|
300 |
317 |
foreach ($menu_links as $tid => $mlid) {
|
|
318 |
if (!_taxonomy_menu_term_too_deep($tid, $depth))
|
301 |
319 |
// $args must be reset each time through.
|
302 |
320 |
$args = array(
|
303 |
321 |
'menu_name' => $menu_name,
|
... | ... | |
311 |
329 |
$args['term'] = taxonomy_term_load($tid);
|
312 |
330 |
}
|
313 |
331 |
|
314 |
|
//update the menu link
|
|
332 |
// Update the menu link.
|
315 |
333 |
taxonomy_menu_handler('update', $args);
|
316 |
334 |
}
|
317 |
335 |
|
... | ... | |
319 |
337 |
}
|
320 |
338 |
|
321 |
339 |
/**
|
322 |
|
* Creates new link items for the vocabulary
|
|
340 |
* Creates new link items for the vocabulary.
|
323 |
341 |
*
|
324 |
342 |
* @param $vid
|
|
343 |
* Vocabulary ID.
|
325 |
344 |
*/
|
326 |
345 |
function _taxonomy_menu_insert_link_items($vid) {
|
327 |
346 |
$menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $vid), FALSE);
|
328 |
|
// Check to see if we should had a vocab item
|
|
347 |
// Check to see if we should had a vocab item.
|
329 |
348 |
if (variable_get(_taxonomy_menu_build_variable('voc_item', $vid), FALSE)) {
|
330 |
349 |
$args = array(
|
331 |
350 |
'vid' => $vid,
|
... | ... | |
333 |
352 |
);
|
334 |
353 |
taxonomy_menu_handler('insert', $args);
|
335 |
354 |
}
|
336 |
|
// Let batch api take care of inserting the menu items
|
|
355 |
// Let batch api take care of inserting the menu items.
|
337 |
356 |
_taxonomy_menu_insert_link_items_batch($vid);
|
338 |
357 |
}
|
339 |
358 |
|
... | ... | |
341 |
360 |
* Implements hook_taxonomy_vocabulary_delete().
|
342 |
361 |
*/
|
343 |
362 |
function taxonomy_menu_taxonomy_vocabulary_delete($vocabulary) {
|
344 |
|
//delete the menu items
|
|
363 |
// Delete the menu items for this vocab.
|
345 |
364 |
_taxonomy_menu_delete_all($vocabulary->vid);
|
346 |
365 |
$menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $vocabulary->vid), 0);
|
347 |
366 |
menu_cache_clear($menu_name);
|
|
367 |
|
|
368 |
// Delete all the variables for this vocab.
|
|
369 |
$variable_prefixes = array(
|
|
370 |
'taxonomy_menu_vocab_menu_',
|
|
371 |
'taxonomy_menu_vocab_parent_',
|
|
372 |
'taxonomy_menu_voc_item_',
|
|
373 |
'taxonomy_menu_display_num_',
|
|
374 |
'taxonomy_menu_hide_empty_terms_',
|
|
375 |
'taxonomy_menu_expanded_',
|
|
376 |
'taxonomy_menu_rebuild_',
|
|
377 |
'taxonomy_menu_path_',
|
|
378 |
'taxonomy_menu_menu_end_all_',
|
|
379 |
'taxonomy_menu_display_descendants_',
|
|
380 |
'taxonomy_menu_voc_name_',
|
|
381 |
'taxonomy_menu_sync_',
|
|
382 |
);
|
|
383 |
foreach ($variable_prefixes as $prefix) {
|
|
384 |
variable_del($prefix . $vocabulary->name);
|
|
385 |
}
|
348 |
386 |
}
|
349 |
387 |
|
350 |
388 |
/**
|
351 |
389 |
* Implements hook_taxonomy_term_insert($term).
|
352 |
390 |
*/
|
353 |
|
|
354 |
391 |
function taxonomy_menu_taxonomy_term_insert($term) {
|
355 |
392 |
_taxonomy_menu_taxonomy_termapi_helper($term, 'insert');
|
356 |
393 |
}
|
... | ... | |
358 |
395 |
/**
|
359 |
396 |
* Implements hook_taxonomy_term_update().
|
360 |
397 |
*/
|
361 |
|
|
362 |
398 |
function taxonomy_menu_taxonomy_term_update($term) {
|
363 |
399 |
_taxonomy_menu_taxonomy_termapi_helper($term, 'update');
|
364 |
400 |
}
|
... | ... | |
366 |
402 |
/**
|
367 |
403 |
* Implements hook_taxonomy_term_delete().
|
368 |
404 |
*/
|
369 |
|
|
370 |
405 |
function taxonomy_menu_taxonomy_term_delete($term) {
|
371 |
406 |
_taxonomy_menu_taxonomy_termapi_helper($term, 'delete');
|
372 |
407 |
}
|
... | ... | |
377 |
412 |
function taxonomy_menu_node_insert($node) {
|
378 |
413 |
$terms_old = &drupal_static('taxonomy_menu_terms_old');
|
379 |
414 |
// We use this direct table pull to avoid the cache and because
|
380 |
|
// free tags are not formated in a matter where extrating the
|
|
415 |
// free tags are not formatted in a matter where extrating the
|
381 |
416 |
// tid's is easy.
|
382 |
417 |
$terms_new = _taxonomy_menu_get_node_terms($node);
|
383 |
418 |
|
... | ... | |
390 |
425 |
* Implements hook_node_update().
|
391 |
426 |
*/
|
392 |
427 |
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);
|
|
428 |
if (isset($node->original->status) and $node->original->status != $node->status) {
|
|
429 |
$terms_old = &drupal_static('taxonomy_menu_terms_old');
|
|
430 |
// We use this direct table pull to avoid the cache and because
|
|
431 |
// free tags are not formatted in a matter where extracting the
|
|
432 |
// tid's is easy.
|
|
433 |
$terms_new = _taxonomy_menu_get_node_terms($node);
|
|
434 |
|
|
435 |
// Merge current terms and previous terms to update both menu items.
|
|
436 |
$terms = array_unique(array_merge((array)$terms_new, (array)$terms_old));
|
|
437 |
_taxonomy_menu_nodeapi_helper('update', $terms, $node);
|
|
438 |
}
|
402 |
439 |
}
|
403 |
440 |
|
404 |
441 |
/**
|
... | ... | |
406 |
443 |
*/
|
407 |
444 |
function taxonomy_menu_node_presave($node) {
|
408 |
445 |
$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
|
|
446 |
// Get the terms from the database before the changes are made. These will be
|
|
447 |
// used to update the menu item's name if needed we go directly to the db to
|
|
448 |
// bypass any caches.
|
412 |
449 |
if (isset($node->nid)) {
|
413 |
450 |
$node_old = node_load($node->nid);
|
414 |
451 |
$terms_old = _taxonomy_menu_get_node_terms($node_old);
|
... | ... | |
422 |
459 |
* Implements hook_node_delete().
|
423 |
460 |
*/
|
424 |
461 |
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
|
|
462 |
// Since the delete operation is run after the data is deleted pull the terms
|
|
463 |
// from the node object.
|
427 |
464 |
$terms = _taxonomy_menu_get_node_terms($node);
|
428 |
465 |
_taxonomy_menu_nodeapi_helper('delete', $terms, $node);
|
429 |
466 |
}
|
... | ... | |
436 |
473 |
// option has been checked.
|
437 |
474 |
$menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $term->vid), 0);
|
438 |
475 |
$sync = variable_get(_taxonomy_menu_build_variable('sync', $term->vid), 0);
|
|
476 |
$depth = variable_get(_taxonomy_menu_build_variable('max_depth', $term->vid), 0);
|
439 |
477 |
|
440 |
|
if ($menu_name && $sync) {
|
|
478 |
if ($menu_name && $sync && !_taxonomy_menu_term_too_deep($term->tid, $depth)) {
|
441 |
479 |
$item = array(
|
442 |
480 |
'tid' => $term->tid,
|
443 |
481 |
'vid' => $term->vid,
|
... | ... | |
459 |
497 |
}
|
460 |
498 |
$message = t($text, array('%term' => $term->name, '%menu_name' => $menu_name));
|
461 |
499 |
|
462 |
|
// run function
|
|
500 |
// Run function.
|
463 |
501 |
taxonomy_menu_handler($operation, $item);
|
464 |
502 |
|
465 |
|
// report status
|
|
503 |
// Report status.
|
466 |
504 |
drupal_set_message($message, 'status');
|
467 |
505 |
|
468 |
|
// rebuild the menu
|
|
506 |
// Rebuild the menu.
|
469 |
507 |
menu_cache_clear($menu_name);
|
470 |
508 |
}
|
471 |
509 |
}
|
... | ... | |
473 |
511 |
/**
|
474 |
512 |
* Builds argument arrays calls taxonomy_menu_handler.
|
475 |
513 |
*
|
476 |
|
* @param $op
|
477 |
|
* A string of the operation to be performed [update|insert|delete]
|
478 |
|
* @param $terms
|
479 |
|
* An array of tids.
|
|
514 |
* @param string $op
|
|
515 |
* The operation to be performed [update|insert|delete]
|
|
516 |
* @param array $terms
|
|
517 |
* The taxonomy terms.
|
480 |
518 |
* @param $node
|
|
519 |
* The node object.
|
481 |
520 |
*/
|
482 |
521 |
function _taxonomy_menu_nodeapi_helper($op, $terms = array(), $node) {
|
483 |
522 |
foreach ($terms as $key => $tid) {
|
484 |
|
|
485 |
|
// taxonomy_term_load($tid) return FALSE if the term was not found
|
486 |
|
// if taxonomy $term is false, then go to the next $term
|
|
523 |
// If taxonomy $term is false, then go to the next $term.
|
|
524 |
// taxonomy_term_load($tid) returns FALSE if the term was not found.
|
487 |
525 |
if (!$term = taxonomy_term_load($tid)) {
|
488 |
526 |
continue;
|
489 |
527 |
}
|
490 |
528 |
|
491 |
|
// update the menu for each term if necessary
|
|
529 |
// Update the menu for each term if necessary.
|
492 |
530 |
$menu_name = variable_get(_taxonomy_menu_build_variable('vocab_menu', $term->vid), FALSE);
|
493 |
531 |
$vocb_sync = variable_get(_taxonomy_menu_build_variable('sync', $term->vid), TRUE);
|
494 |
532 |
$menu_num = variable_get(_taxonomy_menu_build_variable('display_num', $term->vid), TRUE);
|
|
533 |
$menu_num_inc_children = variable_get(_taxonomy_menu_build_variable('display_num_incl_children', $term->vid), TRUE);
|
495 |
534 |
$hide_empty = variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $term->vid), FALSE);
|
496 |
535 |
if ($menu_name && $vocb_sync && ($menu_num || $hide_empty)) {
|
497 |
|
//build argument array to save menu_item
|
|
536 |
// Build argument array to save menu_item.
|
498 |
537 |
$args = array(
|
499 |
538 |
'tid' => $term->tid,
|
500 |
539 |
'vid' => $term->vid,
|
... | ... | |
512 |
551 |
}
|
513 |
552 |
|
514 |
553 |
taxonomy_menu_handler($op, $args, $node);
|
515 |
|
if (variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $term->vid), FALSE)) {
|
|
554 |
if ($hide_empty || $menu_num && $menu_num_inc_children) {
|
516 |
555 |
_taxonomy_menu_update_all_parents($term, $menu_name);
|
517 |
556 |
}
|
518 |
557 |
}
|
... | ... | |
520 |
559 |
}
|
521 |
560 |
|
522 |
561 |
/**
|
523 |
|
* Update all parent items
|
524 |
|
*
|
525 |
|
* @param $op
|
526 |
|
* options are 'insert', 'update', 'delete' or path
|
|
562 |
* Updates all parent items.
|
527 |
563 |
*
|
|
564 |
* @param string $op
|
|
565 |
* options are 'insert', 'update', 'delete' or path.
|
528 |
566 |
* @param $node
|
529 |
|
* The node object.
|
|
567 |
* The node object.
|
530 |
568 |
*/
|
531 |
569 |
function _taxonomy_menu_update_all_parents($term, $menu_name) {
|
532 |
570 |
$parents = taxonomy_get_parents($term->tid);
|
... | ... | |
546 |
584 |
}
|
547 |
585 |
|
548 |
586 |
/**
|
549 |
|
* HANDLING
|
|
587 |
* Taxonomy Menu Handler: Creates a menu item for each taxonomy term.
|
550 |
588 |
*
|
551 |
589 |
* @param $op
|
552 |
|
* options are 'insert', 'update', 'delete' or path
|
553 |
|
*
|
|
590 |
* options are 'insert', 'update', 'delete' or path.
|
|
591 |
* @param $args
|
|
592 |
* if $op == 'insert' then args is an array with the following key/value pairs:
|
|
593 |
* 'term': taxonomy term object,
|
|
594 |
* 'menu_name' : menu that the item is set to apply to
|
|
595 |
* if $op == 'delete' then then args is an array with the following key/value pairs:
|
|
596 |
* 'tid': TermID
|
|
597 |
* 'mlid': Menu ID
|
|
598 |
* if $op == 'update' then then args is an array with the following key/value pairs:
|
|
599 |
* 'term': term object,
|
|
600 |
* 'menu_name': menu that the item is set to apply to
|
|
601 |
* 'mlid': Menu ID
|
554 |
602 |
* @param $node
|
555 |
|
* The node object.
|
|
603 |
* The node object.
|
|
604 |
* @param $item array
|
|
605 |
* Taxonomy menu item.
|
556 |
606 |
*
|
557 |
|
* @param $args
|
558 |
|
* if $op == 'insert' then
|
559 |
|
* array with the following key/value pairs:
|
560 |
|
* 'term' => term object,
|
561 |
|
* 'menu_name' => menu that the item is set to apply to
|
562 |
|
* if $op == 'delete' then
|
563 |
|
* array(
|
564 |
|
* 'tid' => TermID
|
565 |
|
* 'mlid => Menu ID
|
566 |
|
* )
|
567 |
|
* if $op == 'update' then
|
568 |
|
* 'term' => term object,
|
569 |
|
* 'menu_name' => menu that the item is set to apply to
|
570 |
|
* 'mlid' => Menu ID
|
|
607 |
* @return array
|
|
608 |
* Menu link ID for the taxonomy menu item.
|
571 |
609 |
*/
|
572 |
610 |
function taxonomy_menu_handler($op, $args = array(), $node = NULL, $item = array()) {
|
573 |
|
// Get the initial $item
|
|
611 |
// Get the initial $item.
|
574 |
612 |
if (empty($item)) {
|
575 |
613 |
$item = _taxonomy_menu_create_item($args, $node);
|
576 |
614 |
}
|
577 |
615 |
|
578 |
|
// Let other modules make edits
|
|
616 |
// Let other modules make edits.
|
579 |
617 |
$hook = 'taxonomy_menu_' . $op;
|
580 |
618 |
foreach (module_implements($hook) as $module) {
|
581 |
619 |
$function = $module . '_' . $hook;
|
582 |
620 |
$function($item);
|
583 |
621 |
}
|
584 |
622 |
|
585 |
|
// Update the menu and return the mlid if the remove element is not true
|
|
623 |
// Update the menu and return the mlid if the remove element is not true.
|
586 |
624 |
if ($op != 'delete') {
|
587 |
625 |
return _taxonomy_menu_save($item);
|
588 |
626 |
}
|
... | ... | |
609 |
647 |
* 'mlid' => if this is filled in then the mlid will be updated
|
610 |
648 |
*/
|
611 |
649 |
function _taxonomy_menu_save($item) {
|
|
650 |
if (empty($item)) return;
|
|
651 |
|
612 |
652 |
$insert = TRUE;
|
613 |
653 |
|
614 |
|
$flatten_menu = variable_get(_taxonomy_menu_build_variable('flat',
|
615 |
|
$item['vid']));
|
|
654 |
$flatten_menu = variable_get(_taxonomy_menu_build_variable('flat', $item['vid']));
|
616 |
655 |
// Child items should appear around the parent/root, so set their weight
|
617 |
|
// equal to the root term's
|
|
656 |
// equal to the root term's weight.
|
618 |
657 |
if ($flatten_menu) {
|
619 |
658 |
$item['weight'] = $item['root_term_weight'];
|
620 |
659 |
}
|
621 |
660 |
|
622 |
661 |
// create the path.
|
623 |
662 |
$path = taxonomy_menu_create_path($item['vid'], $item['tid']);
|
624 |
|
// get the parent mlid: this is either:
|
|
663 |
// Get the parent mlid: this is either:
|
625 |
664 |
// - the parent tid's mlid
|
626 |
665 |
// - the vocab menu item's mlid
|
627 |
666 |
// - the menu parent setting for this vocab
|
... | ... | |
630 |
669 |
$plid = variable_get(_taxonomy_menu_build_variable('vocab_parent', $item['vid']), NULL);
|
631 |
670 |
}
|
632 |
671 |
|
633 |
|
// Make sure the path has less then 256 characters
|
|
672 |
// Make sure the path has less then 256 characters.
|
634 |
673 |
if (drupal_strlen($path) > 256) {
|
635 |
674 |
preg_match('/(.{256}.*?)\b/', $path, $matches);
|
636 |
675 |
$path = rtrim($matches[1]);
|
... | ... | |
640 |
679 |
'link_title' => $item['name'],
|
641 |
680 |
'menu_name' => $item['menu_name'],
|
642 |
681 |
'plid' => $plid,
|
643 |
|
'options' => array('attributes' => array('title' => trim($item['description'])
|
644 |
|
? $item['description'] : $item['name'])),
|
645 |
682 |
'weight' => $item['weight'],
|
646 |
683 |
'module' => 'taxonomy_menu',
|
647 |
684 |
'expanded' => variable_get(_taxonomy_menu_build_variable('expanded', $item['vid']), TRUE),
|
648 |
685 |
'link_path' => $path,
|
649 |
686 |
);
|
650 |
687 |
|
651 |
|
// Add setup the query paramater in the URL correctly
|
|
688 |
// Be sure to load the original menu link to preserve non-standard properties.
|
|
689 |
if (isset($item['mlid']) && !empty($item['mlid']) && $original_link = menu_link_load($item['mlid'])) {
|
|
690 |
$link = array_merge($original_link, $link);
|
|
691 |
}
|
|
692 |
else {
|
|
693 |
$link['options'] = array(
|
|
694 |
'attributes' => array(
|
|
695 |
'title' => trim($item['description']) ? $item['description'] : $item['name'],
|
|
696 |
),
|
|
697 |
);
|
|
698 |
}
|
|
699 |
|
|
700 |
// Add setup the query paramater in the URL correctly.
|
652 |
701 |
if (strpos($path, '?') !== FALSE) {
|
653 |
702 |
$split = explode('?', $path);
|
654 |
703 |
if (strpos($split[1], '?') !== FALSE) {
|
... | ... | |
661 |
710 |
}
|
662 |
711 |
}
|
663 |
712 |
|
664 |
|
// If passed a mlid then add it
|
|
713 |
// If passed a mlid then add it.
|
665 |
714 |
if (isset($item['mlid']) && $item['mlid']) {
|
666 |
715 |
$link['mlid'] = $item['mlid'];
|
667 |
716 |
$insert = FALSE;
|
|
717 |
$menu_link = menu_link_load($item['mlid']);
|
|
718 |
$is_hidden = $menu_link['hidden'];
|
|
719 |
}
|
|
720 |
else {
|
|
721 |
$is_hidden = 0;
|
668 |
722 |
}
|
669 |
723 |
|
670 |
|
// FIXME: i18nmenu need to be cleaned up to allow translation from other menu module
|
|
724 |
// @todo i18nmenu needs to be cleaned up to allow translation from other menu modules.
|
671 |
725 |
if (module_exists('i18n_menu')) {
|
672 |
726 |
$link['options']['alter'] = TRUE;
|
673 |
727 |
$link['language'] = $item['language'];
|
674 |
728 |
$link['customized'] = 1;
|
675 |
729 |
}
|
676 |
730 |
|
677 |
|
// set the has_children property
|
678 |
|
// if tid=0 then adding a vocab item and had children
|
679 |
|
// if the term has any children then set it to true
|
|
731 |
// Set the has_children property.
|
|
732 |
// If tid=0 then adding a vocab item and had children.
|
|
733 |
// If the term has any children then set it to true.
|
680 |
734 |
if ($item['tid'] == 0) {
|
681 |
735 |
$link['has_children'] = 1;
|
682 |
736 |
}
|
... | ... | |
687 |
741 |
}
|
688 |
742 |
}
|
689 |
743 |
|
690 |
|
// If remove is true then set hidden to 1
|
691 |
|
$link['hidden'] = (isset($item['remove']) && $item['remove']) ? 1 : 0;
|
|
744 |
// If remove is true then set hidden to 1.
|
|
745 |
$link['hidden'] = (isset($item['remove']) && $item['remove']) ? 1 : $is_hidden;
|
692 |
746 |
|
693 |
|
// Save the menu item
|
|
747 |
// Save the menu item.
|
694 |
748 |
if ($mlid = menu_link_save($link)) {
|
695 |
|
// if inserting a new menu item then insert a record into the table
|
|
749 |
// if inserting a new menu item then insert a record into the table.
|
696 |
750 |
if ($insert) {
|
697 |
751 |
_taxonomy_menu_insert_menu_item($mlid, $item['tid'], $item['vid']);
|
698 |
752 |
}
|
... | ... | |
705 |
759 |
}
|
706 |
760 |
|
707 |
761 |
/**
|
708 |
|
* Create the path to use in the menu item
|
|
762 |
* Create the path to use in the menu item.
|
709 |
763 |
*
|
710 |
764 |
* @return array
|
711 |
|
* path selections
|
|
765 |
* Path selections.
|
712 |
766 |
*/
|
713 |
767 |
function _taxonomy_menu_get_paths() {
|
714 |
768 |
return module_invoke_all('taxonomy_menu_path');
|
... | ... | |
718 |
772 |
* Creates the path for the vid/tid combination.
|
719 |
773 |
*
|
720 |
774 |
* @param $vid
|
|
775 |
* Vocablary ID.
|
721 |
776 |
* @param $tid
|
|
777 |
* Taxonomy term ID.
|
|
778 |
*
|
722 |
779 |
* @return string
|
723 |
|
* path
|
|
780 |
* Path
|
724 |
781 |
*/
|
725 |
782 |
function taxonomy_menu_create_path($vid, $tid) {
|
726 |
|
// get the path function for this vocabulary
|
|
783 |
// Get the path function for this vocabulary.
|
727 |
784 |
$function = variable_get(_taxonomy_menu_build_variable('path', $vid), 'taxonomy_menu_path_default');
|
728 |
|
// run the function
|
|
785 |
// Run the function.
|
729 |
786 |
return $function($vid, $tid);
|
730 |
787 |
}
|
731 |
788 |
|
732 |
789 |
/**
|
733 |
790 |
* Implements hook_taxonomy_menu_path().
|
734 |
791 |
*
|
735 |
|
* Invoked from _taxonomy_menu_get_paths.
|
736 |
|
*
|
737 |
792 |
* @return array
|
738 |
|
* function name => Display Title
|
739 |
|
* a list of the path options.
|
|
793 |
* A list of the path options in the form: function_name => Display Title.
|
|
794 |
*
|
|
795 |
* @see _taxonomy_menu_get_paths().
|
740 |
796 |
*/
|
741 |
797 |
function taxonomy_menu_taxonomy_menu_path() {
|
742 |
798 |
$output = array(
|
... | ... | |
747 |
803 |
}
|
748 |
804 |
|
749 |
805 |
/**
|
750 |
|
* Callback for hook_taxonomy_menu_path
|
|
806 |
* Callback for hook_taxonomy_menu_path.
|
751 |
807 |
*/
|
752 |
808 |
function taxonomy_menu_path_default($vid, $tid) {
|
753 |
|
// if tid = 0 then we are creating the vocab menu item format will be taxonomy/term/$tid+$tid+$tid....
|
|
809 |
// If tid = 0 then we are creating the vocab menu item format will be taxonomy/term/$tid+$tid+$tid...
|
754 |
810 |
if ($tid == 0) {
|
755 |
|
// get all of the terms for the vocab
|
|
811 |
// Get all of the terms for the vocab.
|
756 |
812 |
$vtids = _taxonomy_menu_get_terms($vid);
|
757 |
|
$end = implode(' ', $vtids);
|
|
813 |
$end = implode('/', $vtids);
|
758 |
814 |
$path = "taxonomy/term/$end";
|
759 |
815 |
}
|
760 |
816 |
else {
|
761 |
817 |
$path = 'taxonomy/term/' . $tid;
|
762 |
818 |
if (variable_get(_taxonomy_menu_build_variable('display_descendants', $vid), FALSE)) {
|
763 |
|
// Use 'all' at the end of the path
|
|
819 |
// Use 'all' at the end of the path.
|
764 |
820 |
if (variable_get(_taxonomy_menu_build_variable('end_all', $vid), FALSE)) {
|
765 |
821 |
$path .= '/all';
|
766 |
822 |
}
|
767 |
823 |
else {
|
768 |
|
// we wait to run this instead of during the if above
|
769 |
|
// because we only wan to run it once.
|
|
824 |
// We wait to run this instead of during the if above because we only
|
|
825 |
// want to run it once.
|
770 |
826 |
$terms = taxonomy_get_tree($vid, $tid);
|
771 |
827 |
foreach ($terms as $term) {
|
772 |
828 |
$tids[] = $term->tid;
|
773 |
829 |
}
|
774 |
|
if ($tids) {
|
775 |
|
$end = implode(' ', $tids);
|
776 |
|
$path .= ' ' . $end;
|
|
830 |
if (isset($tids)) {
|
|
831 |
$end = implode('/', $tids);
|
|
832 |
$path .= '/ ' . $end;
|
777 |
833 |
}
|
778 |
834 |
}
|
779 |
835 |
}
|
... | ... | |
783 |
839 |
}
|
784 |
840 |
|
785 |
841 |
/**
|
786 |
|
* hook_taxonomy_menu_delete
|
787 |
|
*
|
788 |
|
* @param $args
|
789 |
|
* array(
|
790 |
|
* 'vid' => Vocab ID
|
791 |
|
* 'tid' => TermID
|
792 |
|
* 'mlid' => Menu ID
|
793 |
|
* )
|
|
842 |
* Implements hook_taxonomy_menu_delete().
|
794 |
843 |
*
|
|
844 |
* @param array $item
|
|
845 |
* Taxonomy menu item array, containing the following key/value pairs:
|
|
846 |
* 'vid': Vocabulary ID.
|
|
847 |
* 'tid': Taxonomy term ID.
|
|
848 |
* 'mlid': Menu link ID.
|
795 |
849 |
*/
|
796 |
850 |
function taxonomy_menu_taxonomy_menu_delete(&$item) {
|
797 |
851 |
menu_link_delete($item['mlid']);
|
798 |
852 |
_taxonomy_menu_delete_item($item['vid'], $item['tid']);
|
799 |
853 |
unset($item['mlid']);
|
800 |
|
|
801 |
854 |
}
|
802 |
855 |
|
803 |
856 |
/**
|
804 |
|
* Create the initial $item array
|
|
857 |
* Creates the initial $item array.
|
805 |
858 |
*
|
806 |
859 |
* @param $args
|
807 |
|
* array with the following key/value pairs:
|
808 |
|
* 'term' => term object, if updating a term
|
809 |
|
* 'menu_name' => menu that the item is set to apply to
|
810 |
|
* 'vid' => vocab id. if editing vocab item
|
811 |
|
* 'mlid' => menu id
|
|
860 |
* array with the following key/value pairs:
|
|
861 |
* 'term': Taxonomy term object, if updating a term.
|
|
862 |
* 'menu_name': menu that the item is set to apply to.
|
|
863 |
* 'vid': Vocabuary id, if editing vocab item.
|
|
864 |
* 'mlid': Menu link id.
|
812 |
865 |
*
|
813 |
866 |
* @param $node
|
814 |
|
* The node object.
|
|
867 |
* The node object.
|
815 |
868 |
*/
|
816 |
869 |
function _taxonomy_menu_create_item($args = array(), $node) {
|
817 |
|
|
818 |
|
// If tid = 0, then we are creating a vocab item
|
|
870 |
// If tid = 0, then we are creating a vocab item.
|
819 |
871 |
if (isset($args['tid']) && isset($args['vid']) && $args['tid'] == 0 && variable_get(_taxonomy_menu_build_variable('voc_item', $args['vid']), 0)) {
|
820 |
872 |
$vocab = taxonomy_vocabulary_load($args['vid']);
|
821 |
873 |
$item = array(
|
... | ... | |
838 |
890 |
if (empty($term->parents)) {
|
839 |
891 |
$term->parents = _taxonomy_menu_get_parents($term->tid);
|
840 |
892 |
if (empty($term->parents)) {
|
841 |
|
// even without parents, create one with $ptid = 0
|
|
893 |
// Even without parents, create one with $ptid = 0.
|
842 |
894 |
$term->parents = array(0 => '0');
|
843 |
895 |
}
|
844 |
896 |
}
|
... | ... | |
847 |
899 |
// a flat taxonomy menu.
|
848 |
900 |
if (is_object($term)) {
|
849 |
901 |
$term_parents = taxonomy_get_parents_all($term->tid);
|
850 |
|
$root_term_weight = $term_parents[count($term_parents) - 1]->weight;
|
|
902 |
$root_term_weight = ($term_parents) ? $term_parents[count($term_parents) - 1]->weight : 0;
|
851 |
903 |
}
|
852 |
904 |
else {
|
853 |
905 |
$root_term_weight = 0;
|
... | ... | |
855 |
907 |
|
856 |
908 |
foreach ($term->parents as $parent) {
|
857 |
909 |
$ptid = $parent;
|
858 |
|
// turn the term into the correct $item array form
|
|
910 |
// Turn the term into the correct $item array form.
|
859 |
911 |
$item = array(
|
|
912 |
'term' => $term,
|
860 |
913 |
'tid' => $term->tid,
|
861 |
914 |
'name' => $term->name,
|
862 |
915 |
'description' => variable_get(_taxonomy_menu_build_variable('term_item_description', $term->vid), 0) ? $term->description : '',
|
863 |
|
'weight' => $term->weight,
|
|
916 |
'weight' => !empty($term->weight) ? $term->weight : 0,
|
864 |
917 |
'vid' => $term->vid,
|
865 |
918 |
'ptid' => $ptid,
|
866 |
919 |
'root_term_weight' => $root_term_weight,
|
... | ... | |
870 |
923 |
if (isset($args['mlid'])) {
|
871 |
924 |
$item['mlid'] = $args['mlid'];
|
872 |
925 |
}
|
873 |
|
// Mutiple parents are not supported yet, hence this break.
|
874 |
|
// without the break, the item is inserted multiple under one parent, instead once under each parent.
|
|
926 |
// Mutiple parents are not supported yet. Without the break, the item is
|
|
927 |
// inserted multiple under one parent, instead of once under each parent.
|
875 |
928 |
break;
|
876 |
929 |
}
|
877 |
930 |
}
|
... | ... | |
880 |
933 |
}
|
881 |
934 |
|
882 |
935 |
/**
|
883 |
|
* Helper function to see if any of the children have any nodes
|
|
936 |
* Helper function: See if any of the children have any nodes.
|
884 |
937 |
*
|
885 |
938 |
* @param $tid
|
|
939 |
* Taxonomy term ID.
|
886 |
940 |
* @param $vid
|
|
941 |
* Vocabulary ID.
|
|
942 |
*
|
887 |
943 |
* @return boolean
|
888 |
944 |
*/
|
889 |
945 |
function _taxonomy_menu_children_has_nodes($tid, $vid, $return = FALSE) {
|
... | ... | |
900 |
956 |
}
|
901 |
957 |
|
902 |
958 |
/**
|
903 |
|
* Helper function for insert and update hooks
|
|
959 |
* Helper function: Inserts and updates menu along with taxonomy changes.
|
|
960 |
*
|
|
961 |
* @param array $item
|
|
962 |
* Taxonomy menu item.
|
904 |
963 |
*
|
905 |
|
* @param $item
|
906 |
964 |
* @return array
|
|
965 |
* Taxonomy menu item.
|
907 |
966 |
*/
|
908 |
967 |
function _taxonomy_menu_item($item) {
|
909 |
|
// if tid is 0 then do not change any settings
|
|
968 |
if (empty($item)) return;
|
|
969 |
|
|
970 |
// If tid is 0 then do not change any settings.
|
910 |
971 |
if ($item['tid'] > 0) {
|
911 |
|
// get the number of node attached to this term
|
|
972 |
// Get the number of node attached to this term.
|
912 |
973 |
$num = _taxonomy_menu_term_count($item['tid']);
|
913 |
974 |
|
914 |
|
// if hide menu is selected and the term count is 0 and the term has no children then do not create the menu item
|
|
975 |
// If hide menu is selected and the term count is 0 and the term has no
|
|
976 |
// children then do not create the menu item.
|
915 |
977 |
if ($num == 0 &&
|
916 |
|
variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $item['vid']), FALSE) &&
|
917 |
|
!_taxonomy_menu_children_has_nodes($item['tid'], $item['vid'])) {
|
918 |
|
|
|
978 |
variable_get(_taxonomy_menu_build_variable('hide_empty_terms', $item['vid']), FALSE) &&
|
|
979 |
!_taxonomy_menu_children_has_nodes($item['tid'], $item['vid'])) {
|
919 |
980 |
$item['remove'] = TRUE;
|
Weekly update of contrib modules