-
Notifications
You must be signed in to change notification settings - Fork 3
/
taxonomy_menu.module
1021 lines (894 loc) · 31.9 KB
/
taxonomy_menu.module
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Adds links to taxonomy terms into a menu.
*/
// Include the database layer.
module_load_include('inc', 'taxonomy_menu', 'taxonomy_menu.database');
// Include the batch functions.
module_load_include('inc', 'taxonomy_menu', 'taxonomy_menu.batch');
/**
* Implements hook_config_info().
*/
function taxonomy_menu_config_info() {
// If there are a large number of configuration files prefixed with this
// string, provide a "name_key" that will be read from the configuration file
// and used when listing the configuration file.
$prefixes['taxonomy_menu.vocabulary'] = array(
'name_key' => 'name',
'label_key' => 'label',
'group' => t('Taxonomy Menus'),
);
return $prefixes;
}
/**
* Implements hook_form_alter().
*/
function taxonomy_menu_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'taxonomy_form_vocabulary') {
// Add our vertical tabs js.
$path = backdrop_get_path('module', 'taxonomy_menu') . '/js/taxonomy_menu.js';
$form['additional_settings']['#attached']['js'][] = $path;
// Do not alter on deletion
if (isset($form_state['confirm_delete'])) {
return;
}
// Choose a menu to add link items to.
$menus = menu_get_menus();
$form['taxonomy_menu'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#title' => t('Taxonomy menu'),
'#weight' => 10,
'#tree' => TRUE,
'#group' => 'additional_settings',
);
// This turns the vocab terms into menu items.
$item['mlid'] = 0;
$all_menus = menu_get_menus();
// If the main menu is an option, put it at the top of the list for UX.
if (array_key_exists('main-menu', $all_menus)) {
$main_menu = $all_menus['main-menu'];
unset($all_menus['main-menu']);
$new_menus = array('main-menu' => $main_menu) + $all_menus;
$all_menus = $new_menus;
}
$menu_items = menu_parent_options($all_menus, $item);
// Add disabled to the top of the list.
array_unshift($menu_items, t('- Disabled -'));
$vocabulary = 0;
$vocab_parent_default = 0;
// The vocab name isn't set when a new vocabulary is being created.
if (isset($form['machine_name']) && isset($form['machine_name']['#default_value'])) {
$vocabulary = $form['machine_name']['#default_value'];
$config = config('taxonomy_menu.vocabulary.' . $vocabulary);
$vocab_menu = $config->get('vocab_menu');
$vocab_parent = $config->get('vocab_parent');
$vocab_parent_default = $vocab_menu . ':' . $vocab_parent;
}
$form['taxonomy_menu']['vocab_parent'] = array(
'#type' => 'select',
'#title' => t('Menu location'),
'#default_value' => $vocab_parent_default,
'#options' => $menu_items,
'#description' => t('Taxonomy menu items will be inserted below the item selected here.'),
'#attributes' => array('class' => array('menu-title-select')),
);
// Build the options part of the vocab form.
$form['taxonomy_menu']['options'] = taxonomy_menu_get_options($vocabulary);
// Rebuild the menu.
$form['taxonomy_menu']['options']['rebuild'] = array(
'#type' => 'checkbox',
'#title' => t('Rebuild the menu on submit'),
'#default_value' => 0,
'#weight' => 20,
'#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.'),
);
// Move the buttons to the bottom of the form.
$form['submit']['#weight'] = 49;
$form['delete']['#weight'] = 50;
// Add an extra submit handler to save these settings.
$form['#submit'][] = 'taxonomy_menu_vocab_submit';
}
elseif ($form_id == "taxonomy_overview_terms") {
// Add an extra submit handler to sync the rearranged terms with menu.
$form['#submit'][] = 'taxonomy_menu_overview_submit';
// @ TODO: using hook_taxonomy_vocabulary_update() is nicer then callback,
// but gives less info and does not always fire.
}
}
/**
* Submit handler: Saves extra settings added to the taxonomy vocab form.
*/
function taxonomy_menu_vocab_submit($form, &$form_state) {
$vocabulary = $form_state['values']['machine_name'];
// Menu location has been set to disabled.
if (is_numeric($form_state['values']['taxonomy_menu']['vocab_parent'])) {
// Don't want to throw notices.
$form_state['values']['taxonomy_menu']['vocab_parent'] = '0:0';
// If the menu was not previously enabled, skip the rest.
if (config_get('taxonomy_menu.vocabulary.' . $vocabulary, 'vocab_menu') === NULL) {
return;
}
}
// Menu is enabled: create a new config object, or load existing.
$config = config('taxonomy_menu.vocabulary.' . $vocabulary);
$config->set('name', $vocabulary);
$config->set('label', $form_state['values']['name']);
// Split the menu location into menu name and menu item id.
list($vocab_parent['vocab_menu'], $vocab_parent['vocab_parent']) = explode(':', $form_state['values']['taxonomy_menu']['vocab_parent']);
// Init flag variables to avoid notices if changes haven't happened.
$changed_menu = FALSE;
$change_vocab_item = FALSE;
$changed_path = FALSE;
// Set the menu name and check for changes.
if ($config->get('vocab_menu') != $vocab_parent['vocab_menu']) {
$changed_menu = TRUE;
}
$config->set('vocab_menu', $vocab_parent['vocab_menu']);
// Set the menu parent item and check for changes.
if ($config->get('vocab_parent') != $vocab_parent['vocab_parent']) {
$changed_menu = TRUE;
}
$config->set('vocab_parent', $vocab_parent['vocab_parent']);
// Check each of the options for changes that might need a menu rebuild.
foreach ($form_state['values']['taxonomy_menu']['options'] as $key => $value) {
// Check to see if the vocab enable options has changed
if ($key == 'voc_item') {
if ($config->get($key) != $value) {
$change_vocab_item = TRUE;
}
}
if ($config->get($key) != $value) {
// Set checkboxes to boolean.
if ($form['taxonomy_menu']['options'][$key]['#type'] == 'checkbox') {
$value = (bool)$value;
}
$config->set($key, $value);
$changed = TRUE;
}
}
// Save new or updated config.
$config->save();
// These variables make the logic below easier to read.
$rebuild = $form_state['values']['taxonomy_menu']['options']['rebuild'];
$vocab_menu_enabled = $config->get('vocab_menu');
// If the menu hasn't changed and the menu is disabled then do not do anything else.
if ($rebuild || $changed_menu || (!$changed_menu && ($vocab_menu_enabled == 0))) {
// Rebuild if rebuild is selected, menu has changed or vocabulary option changed.
if ($rebuild || $changed_menu || $change_vocab_item) {
$message = _taxonomy_menu_rebuild($vocabulary);
}
// If setting has changed and a menu item is enabled, then update all of the menu items.
elseif ($changed && $vocab_menu_enabled) {
$message = _taxonomy_menu_update_link_items($vocabulary);
}
// Do a full menu rebuild in case we have removed the menu or moved it between menus.
state_set('menu_rebuild_needed', TRUE);
// Only send a message if one has been created.
if (isset($message) && $message) {
// Note: $message is already sanitized coming out of its source function.
drupal_set_message($message, 'status');
}
}
}
/**
* Submit handler: Reacts to form ID: taxonomy_overview_terms.
*/
function taxonomy_menu_overview_submit(&$form, &$form_state) {
// Only sync if taxonomy_menu is enabled for this vocab and the 'sync'
// option has been checked.
// This form has the following flow of buttons:
// 1. [Save] --> rebuild taxonomy_menu
// 2. [Reset to alphabetical] --> no rebuild yet
// 3. [Reset to alphabetical][Reset to alphabetical] --> rebuild
// 4. [Reset to alphabetical][Cancel] --> no rebuild
// The code below avoids rebuilding after situation 2.
if ($form_state['rebuild'] == FALSE && isset($form['#vocabulary']->machine_name) ) {
// Try to catch the 'Save' button.
$vocabulary = $form['#vocabulary']->machine_name;
}
elseif ($form_state['rebuild'] == TRUE && isset($form['#vocabulary']->machine_name) ) {
// Try to catch the 'Reset to alphabetical' button
$vocabulary = NULL;
}
elseif ($form_state['rebuild'] == FALSE && isset($form['machine_name']['#value']) ) {
// Try to catch the second (confirming) 'Reset to alphabetical' button.
$vocabulary = $form['machine_name']['#value'];
}
else {
// The button [Reset to alphabetical] [Cancel] does not call this page.
$vocabulary = NULL;
}
if (isset($vocabulary)) {
$config = config('taxonomy_menu.vocabulary.' . $vocabulary);
$menu_name = $config->get('vocab_menu');
$sync = $config->get('sync');
if ($menu_name && $sync) {
// Update all menu items (do not rebuild the menu).
$message = _taxonomy_menu_update_link_items($vocabulary);
// Report status.
if (isset($message)) {
// message is sanitized coming out of _taxonomy_menu_update_link_items
// no need to reclean it here
backdrop_set_message($message, 'status');
}
// Rebuild the menu.
menu_cache_clear($menu_name);
}
}
}
/**
* Helper function: Rebuilds a taxonomy menu.
*
* @param sting $vocabulary
* Vocabulary machine name.
*
* @return string
* Message that is displayed.
*/
function _taxonomy_menu_rebuild($vocabulary) {
// Remove all of the menu items for this vocabulary
_taxonomy_menu_delete_all($vocabulary);
// Only insert the links if a menu is set
if (config_get('taxonomy_menu.vocabulary.' . $vocabulary, 'vocab_menu')) {
taxonomy_menu_insert_link_items($vocabulary);
menu_rebuild();
return t('The Taxonomy Menu has been rebuilt.');
}
menu_rebuild();
return t('The Taxonomy Menu has been removed.');
}
/**
* Helper function: Updates the menu items.
*
* @param string $vocabulary
* Vocabulary machine name.
*/
function _taxonomy_menu_update_link_items($vocabulary) {
$config = config('taxonomy_menu.vocabulary.' . $vocabulary);
$menu_name = $config->get('vocab_menu');
$depth = $config->get('max_depth');
// Get a list of the current tid and menu_link combinations.
$menu_links = _taxonomy_menu_get_menu_items($vocabulary);
// Cycle through the menu links.
foreach ($menu_links as $tid => $mlid) {
if (!_taxonomy_menu_term_too_deep($tid, $depth))
// The $args must be reset each time through.
$args = array(
'menu_name' => $menu_name,
'mlid' => $mlid,
);
if ($tid == 0) {
$args['vocabulary'] = $vocabulary;
}
else {
$args['term'] = taxonomy_term_load($tid);
$args['vocabulary'] = $args['term']->vocabulary;
}
// Update the menu link.
taxonomy_menu_handler('update', $args);
}
return t('The Taxonomy Menu %menu_name has been updated.', array('%menu_name' => $menu_name));
}
/**
* Creates new link items for the vocabulary.
*
* @param $vocabulary
* Vocabulary machine name.
*/
function taxonomy_menu_insert_link_items($vocabulary) {
taxonomy_menu_insert_link_items_batch($vocabulary);
}
/**
* Implements hook_taxonomy_vocabulary_delete().
*/
function taxonomy_menu_taxonomy_vocabulary_delete($vocabulary) {
// Delete the menu items for this vocabulary.
_taxonomy_menu_delete_all($vocabulary->name);
$config = config('taxonomy_menu.vocabulary.' . $vocabulary->name);
$menu_name = $config->get('vocab_menu');
// Clear the menu cache.
menu_cache_clear($menu_name);
// Delete the config file.
$config->delete();
}
/**
* Implements hook_taxonomy_term_insert($term).
*/
function taxonomy_menu_taxonomy_term_insert($term) {
_taxonomy_menu_taxonomy_termapi_helper($term, 'insert');
}
/**
* Implements hook_taxonomy_term_update().
*/
function taxonomy_menu_taxonomy_term_update($term) {
_taxonomy_menu_taxonomy_termapi_helper($term, 'update');
}
/**
* Implements hook_taxonomy_term_delete().
*/
function taxonomy_menu_taxonomy_term_delete($term) {
_taxonomy_menu_taxonomy_termapi_helper($term, 'delete');
}
/**
* Implements hook_node_insert().
*/
function taxonomy_menu_node_insert($node) {
$terms_old = &backdrop_static('taxonomy_menu_terms_old');
// We use this direct table pull to avoid the cache and because
// free tags are not formatted in a matter where extrating the
// tid's is easy.
$terms_new = _taxonomy_menu_get_node_terms($node);
// Merge current terms and previous terms to update both menu items.
$terms = array_unique(array_merge((array)$terms_new, (array)$terms_old));
_taxonomy_menu_nodeapi_helper('insert', $terms, $node);
}
/**
* Implements hook_node_update().
*/
function taxonomy_menu_node_update($node) {
if (isset($node->original->status) and $node->original->status != $node->status) {
$terms_old = &backdrop_static('taxonomy_menu_terms_old');
//we use this direct table pull to avoid the cache and because
//free tags are not formatted in a matter where extrating the
//tid's is easy
$terms_new = _taxonomy_menu_get_node_terms($node);
//merge current terms and previous terms to update both menu items.
$terms = array_unique(array_merge((array)$terms_new, (array)$terms_old));
_taxonomy_menu_nodeapi_helper('update', $terms, $node);
}
}
/**
* Implements hook_node_presave().
*/
function taxonomy_menu_node_presave($node) {
$terms_old = &backdrop_static('taxonomy_menu_terms_old');
// Get the terms from the database before the changes are made. These will be
// used to update the menu item's name if needed we go directly to the db to
// bypass any caches.
if (isset($node->nid)) {
$node_old = node_load($node->nid);
$terms_old = _taxonomy_menu_get_node_terms($node_old);
}
else {
$terms_old = array();
}
}
/**
* Implements hook_node_delete().
*/
function taxonomy_menu_node_delete($node) {
// Since the delete operation is run after the data is deleted pull the terms
// from the node object.
$terms = _taxonomy_menu_get_node_terms($node);
_taxonomy_menu_nodeapi_helper('delete', $terms, $node);
}
/**
* Helper function: Abstraction of hook_taxonomy_term_<operation>()
*/
function _taxonomy_menu_taxonomy_termapi_helper($term, $operation) {
// Only sync if taxonomy_menu is enabled for this vocab and the 'sync'
// option has been checked.
$config = config('taxonomy_menu.vocabulary.' . $term->vocabulary);
$menu_name = $config->get('vocab_menu');
$sync = $config->get('sync');
$depth = $config->get('max_depth');
if ($menu_name && $sync && !_taxonomy_menu_term_too_deep($term->tid, $depth)) {
$item = array(
'tid' => $term->tid,
'vocabulary' => $term->vocabulary,
'term' => $term,
'menu_name' => $menu_name,
'mlid' => _taxonomy_menu_get_mlid($term->tid, $term->vocabulary),
);
// Run function.
taxonomy_menu_handler($operation, $item);
// Rebuild the menu.
menu_cache_clear($menu_name);
}
}
/**
* Helper function: Builds arguments for taxonomy_menu_handler().
*
* @param string $op
* The operation to be performed [update|insert|delete]
* @param array $terms
* The taxonomy terms.
* @param Node $node
* The node object.
*/
function _taxonomy_menu_nodeapi_helper($op, $terms, $node) {
foreach ($terms as $key => $tid) {
// If taxonomy $term is false, then go to the next $term.
// taxonomy_term_load($tid) returns FALSE if the term was not found
if (!$term = taxonomy_term_load($tid)) {
continue;
}
// Update the menu for each term if necessary.
$config = config('taxonomy_menu.vocabulary.' . $term->vocabulary);
$menu_name = $config->get('vocab_menu');
$vocb_sync = $config->get('sync');
$menu_num = $config->get('display_num');
$hide_empty = $config->get('hide_empty_terms');
if ($menu_name && $vocb_sync && ($menu_num || $hide_empty)) {
// Build argument array to save menu_item.
$args = array(
'tid' => $term->tid,
'vocabulary' => $term->vocabulary,
'term' => $term,
'menu_name' => $menu_name,
'mlid' => _taxonomy_menu_get_mlid($term->tid, $term->vocabulary),
);
if ($op == 'delete') {
/* Turn the op to 'update' here since we really do want to update the item
* and not delete/recreate it, since the latter will break hierarchy and
* customizations.
*/
$op = 'update';
}
taxonomy_menu_handler($op, $args, $node);
if ($hide_empty) {
_taxonomy_menu_update_all_parents($term, $menu_name);
}
}
}
}
/**
* Helper function: Updates all parent items.
*
* @param string $term
* Taxonomy term.
* @param $menu_name
* Machine name of the menu.
*/
function _taxonomy_menu_update_all_parents($term, $menu_name) {
$parents = taxonomy_term_load_parents($term->tid);
if ($parents) {
foreach ($parents as $parent) {
$parent->parents = array_keys(taxonomy_term_load_parents($parent->tid));
$item = array(
'term' => $parent,
'menu_name' => $menu_name,
'mlid' => _taxonomy_menu_get_mlid($parent->tid, $parent->vocabulary),
'remove' => FALSE,
);
taxonomy_menu_handler('update', $item);
_taxonomy_menu_update_all_parents($parent, $menu_name);
}
}
}
/**
* Taxonomy Menu Handler: Creates a menu item for each taxonomy term.
*
* @param $op (string)
* options are 'insert', 'update', or 'delete'.
* @param $args (array)
* if $op == 'insert' then args is an array with the following key/value pairs:
* 'term': Taxonomy term object.
* 'menu_name' : menu that the item is set to apply to
* if $op == 'update' then then args is an array with the following key/value pairs:
* 'term': Taxonomy term object.
* 'menu_name': Machine name of the menu that the item is set to apply to.
* 'mlid': Menu link ID.
* if $op == 'delete' then then args is an array with the following key/value pairs:
* 'tid': Taxonomy term ID.
* 'mlid': Menu link ID.
* @param $node
* The node object, or NULL.
* @param $item (array)
* Taxonomy menu item.
*
* @return array
* Menu link ID for the taxonomy menu item.
*/
function taxonomy_menu_handler($op = '', $args = array(), $node = NULL, $item = array()) {
if ($op == '' || empty($args)) {
$message = 'Missing argument values when calling taxonomy_menu_handler().';
watchdog('taxonomy_menu', $message, array(), WATCHDOG_ERROR);
return;
}
// Get the initial $item.
if (empty($item)) {
if (is_null($node)) {
$message = 'Missing Node value when calling taxonomy_menu_handler().';
watchdog('taxonomy_menu', $message, array(), WATCHDOG_ERROR);
return;
}
$item = _taxonomy_menu_create_item($args, $node);
}
// Let other modules make edits.
switch ($op) {
case 'insert':
$item = module_invoke_all('taxonomy_menu_insert', $item);
break;
case 'update':
$item = module_invoke_all('taxonomy_menu_update', $item);
break;
case 'delete':
$item = module_invoke_all('taxonomy_menu_delete', $item);
break;
}
// Update the menu and return the mlid if the remove element is not true.
if ($op != 'delete') {
return _taxonomy_menu_save($item);
}
}
/**
* Helper function: Adds or updates a taxonomy menu item.
*
* We use a custom data array $item as a parameter, instead of using a
* standard taxonomy $term object. This is because this function is also
* called from hook_taxonomy(), which doesn't have a $term object. This
* provides one consistent way of passing the data.
*
* @param $item
* array with the following key/value pairs:
* 'tid' => the term id (if 0 then adding the vocab as an item)
* 'name' => the term's name
* 'description' => term description, used as to build the title attribute
* 'weight' => term weight
* (This will be overriden by the order created from taxonomy_get_tree which respects the correct wight)
* 'vocabulary' => The machine name for the vocabulary.
* 'ptid' => the term's parent's term id
* 'menu_name' => the menu that the link item will be inserted into
* 'mlid' => if this is filled in then the mlid will be updated
*/
function _taxonomy_menu_save($item) {
if (empty($item)) {
return;
}
$insert = TRUE;
$config = config('taxonomy_menu.vocabulary.' . $item['vocabulary']);
$flatten_menu = $config->get('flat');
// Child items should appear around the parent/root, so set their weight
// equal to the root term's weight.
if ($flatten_menu) {
$item['weight'] = $item['root_term_weight'];
}
// Get the parent mlid: this is either:
// - the parent tid's mlid
// - the menu parent setting for this vocab
$plid = _taxonomy_menu_get_mlid($item['ptid'], $item['vocabulary']);
if (!$plid || $flatten_menu) {
$plid = $config->get('vocab_parent');
}
$link = array(
'link_title' => $item['name'],
'menu_name' => $item['menu_name'],
'plid' => $plid,
'weight' => $item['weight'],
'module' => 'taxonomy_menu',
'expanded' => $config->get('expanded'),
'link_path' => 'taxonomy/term/' . $item['tid'],
);
if (isset($item['mlid']) && !empty($item['mlid'])) {
// Be sure to load the original menu link to preserve non-standard properties.
$original_link = menu_link_load($item['mlid']);
$link = array_merge($original_link, $link);
$is_hidden = $original_link['hidden'];
$insert = FALSE;
}
else {
$is_hidden = 0;
$link['options'] = array(
'attributes' => array(
'title' => trim($item['description']) ? $item['description'] : $item['name'],
),
);
}
// @todo i18nmenu needs to be cleaned up to allow translation from other menu
// modules.
if (module_exists('i18n_menu')) {
$link['options']['alter'] = TRUE;
$link['language'] = $item['language'];
$link['customized'] = 1;
}
// Set the has_children property.
// If tid=0 then adding a vocab item and had children.
// If the term has any children then set it to true.
if ($item['tid'] == 0) {
$link['has_children'] = 1;
}
else {
$children = taxonomy_term_load_children($item['tid']);
if (!empty($children)) {
$link['has_children'] = 1;
}
}
// Expanded must be an integer.
$link['expanded'] = isset($link['expanded']) ? (int)$link['expanded'] : 0;
// If remove is true then set hidden to 1.
$link['hidden'] = (isset($item['remove']) && $item['remove']) ? 1 : $is_hidden;
// Save the menu item.
if ($mlid = menu_link_save($link)) {
// If inserting a new menu item then insert a record into the table.
if ($insert) {
_taxonomy_menu_insert_menu_item($mlid, $item['tid'], $item['vocabulary']);
}
return $mlid;
}
else {
backdrop_set_message(t('Could not save the menu link for the taxonomy menu.'), 'error');
return FALSE;
}
}
/**
* Helper function: Creates the initial $item array.
*
* @param array $args
* array with the following key/value pairs:
* 'term': Taxonomy term object, if updating a term.
* 'menu_name': menu that the item is set to apply to.
* 'vocabulary': Vocabuary machine name, if editing vocab item.
* 'mlid': Menu link id.
*
* @param Node $node
* The node object.
*/
function _taxonomy_menu_create_item($args, $node) {
// If tid <> 0 then we are creating a term item.
$term = $args['term'];
// Sometimes $term->parents is not set so we find it.
if (empty($term->parents)) {
$term->parents = _taxonomy_menu_get_parents($term->tid);
if (empty($term->parents)) {
// even without parents, create one with $ptid = 0
$term->parents = array(0 => '0');
}
}
// Find the weight of the root taxonomy term; we'll need it in case we want
// a flat taxonomy menu.
if (is_object($term)) {
$term_parents = taxonomy_term_load_parents_all($term->tid);
$root_term_weight = ($term_parents) ? $term_parents[count($term_parents) - 1]->weight : 0;
}
else {
$root_term_weight = 0;
}
foreach ($term->parents as $parent) {
$config = config('taxonomy_menu.vocabulary.' . $term->vocabulary);
$ptid = $parent;
// turn the term into the correct $item array form
$item = array(
'term' => $term,
'tid' => $term->tid,
'name' => $term->name,
'description' => $config->get('term_item_description') ? check_plain(strip_tags($term->description)) : '',
'weight' => !empty($term->weight) ? $term->weight : 0,
'vocabulary' => $term->vocabulary,
'ptid' => $ptid,
'root_term_weight' => $root_term_weight,
'menu_name' => $args['menu_name'],
'language' => isset($term->language) ? $term->langcode : ($node ? $node->langcode : $GLOBALS['language']->langcode),
);
if (isset($args['mlid'])) {
$item['mlid'] = $args['mlid'];
}
// Mutiple parents are not supported yet. Without the break, the item is
// inserted multiple under one parent, instead of once under each parent.
break;
}
return $item;
}
/**
* Helper function: See if any of the children have any nodes.
*
* @param $tid
* Taxonomy term ID.
* @param $vocabulary
* Vocabulary machine name.
*
* @return boolean
*/
function _taxonomy_menu_children_has_nodes($tid, $vocabulary, $return = FALSE) {
$children = taxonomy_term_load_children($tid, $vocabulary);
foreach ($children as $tid => $term) {
if (_taxonomy_menu_term_count($tid) > 0) {
$return = TRUE;
}
else {
$return = _taxonomy_menu_children_has_nodes($tid, $vocabulary, $return);
}
}
return $return;
}
/**
* Helper function: Inserts and updates menu along with taxonomy changes.
*
* @param array $item
* Taxonomy menu item, which has the following keys:
* 'tid': Taxonomy term id.
* 'vocabulary': Vocabulary machine name.
* 'mlid': Menu link iD.
*
* @return array
* Taxonomy meny item.
*/
function _taxonomy_menu_item($item) {
if (empty($item)) return;
// If tid is 0 then do not change any settings.
if ($item['tid'] > 0) {
// Get the number of node attached to this term.
$num = _taxonomy_menu_term_count($item['tid']);
$config = config('taxonomy_menu.vocabulary.' . $item['vocabulary']);
// If hide menu is selected and the term count is 0 and the term has no
// children then do not create the menu item.
if ($num == 0 && $config->get('hide_empty_terms') &&
!_taxonomy_menu_children_has_nodes($item['tid'], $item['vocabulary'])) {
$item['remove'] = TRUE;
return $item;
}
// If display number is selected and $num > 0 then change the title.
if ($config->get('display_num')) {
$item['name'] .= " ($num)";
}
}
return $item;
}
/**
* Implements hook_taxonomy_menu_insert().
*/
function taxonomy_menu_taxonomy_menu_insert($item) {
$item = _taxonomy_menu_item($item);
return $item;
}
/**
* Implements hook_taxonomy_menu_update().
*/
function taxonomy_menu_taxonomy_menu_update($item) {
$item = _taxonomy_menu_item($item);
return $item;
}
/**
* Implements hook_taxonomy_menu_delete().
*/
function taxonomy_menu_taxonomy_menu_delete($item) {
menu_link_delete($item['mlid']);
_taxonomy_menu_delete_item($item['vocabulary'], $item['tid']);
unset($item['mlid']);
return $item;
}
/**
* Helper function: Creates a form array of taxonomy menu options.
*
* @param $vocabulary
* Vocabulary machine name.
*
* @return array
* Form array.
*
* @see hook_taxonomy_menu_options().
*/
function taxonomy_menu_get_options($vocabulary) {
$options = module_invoke_all('taxonomy_menu_options');
// Cycle through fields.
foreach ($options as $field_name => $field_elements) {
// Set the type to checkbox if it is empty.
if (empty($options[$field_name]['#type'])) {
$options[$field_name]['#type'] = 'checkbox';
}
// Overwrite the default value from config, if set.
$saved_default = config_get('taxonomy_menu.vocabulary.' . $vocabulary, $field_name);
if (isset($saved_default)) {
$options[$field_name]['#default_value'] = $saved_default;
}
}
// Set the options container.
$options['#type'] = 'container';
return $options;
}
/**
* Implements hook_taxonomy_menu_options().
*/
function taxonomy_menu_taxonomy_menu_options() {
$options['sync'] = array(
'#type' => 'checkbox',
'#title' => t('Synchronise changes to this vocabulary'),
'#description' => t('Every time a term is added/deleted/modified, the corresponding menu link will be altered too.'),
'#default_value' => TRUE,
);
$options['max_depth'] = array(
'#type' => 'select',
'#title' => t('Maximim depth'),
'#description' => t('Limit how many levels of the taxonomy tree to process. Useful if you have a very large tree of taxonomy terms, and only want to provide a menu for the first several levels.'),
'#options' => array(0 => t('All'), 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9),
'#default_value' => 0,
);
$options['display_num'] = array(
'#type' => 'checkbox',
'#title' => t('Display the number of items in each taxonomy term.'),
'#default_value' => FALSE,
);
$options['hide_empty_terms'] = array(
'#type' => 'checkbox',
'#title' => t('Do not add a menu link for taxonomy terms with no items.'),
'#default_value' => FALSE,
);
$options['term_item_description'] = array(
'#type' => 'checkbox',
'#title' => t('Add the taxonomy term description as the title attribute on the menu link.'),
'#default_value' => FALSE,
);
$options['expanded'] = array(
'#type' => 'checkbox',
'#title' => t('Automatically show all menu items as expanded.'),
'#default_value' => TRUE,
);
$options['flat'] = array(
'#type' => 'checkbox',
'#title' => t('Add all menu items to the same level rather than retaining term hierarchy.'),
'#default_value' => FALSE,
);
return $options;
}
/**
* Implements hook_translated_menu_link_alter().
*/
function taxonomy_menu_translated_menu_link_alter(&$item, $map) {
if (module_exists('i18n_taxonomy')) {
// In case of localized terms, use term translation for menu title.
if ($item['module'] == 'taxonomy_menu') {
$record = _taxonomy_menu_get_item($item['mlid']);
// Only translate when term exist (may occur with stray menu item).
if ($record) {
// Only translate when translation mode is set to localize.
if (i18n_taxonomy_vocabulary_mode($record->vocabulary, I18N_MODE_LOCALIZE)) {
// This is a term.
if ($record->tid > 0) {
$term = taxonomy_term_load($record->tid);
$display_num = '';
$num = _taxonomy_menu_term_count($record->tid);
$config = config('taxonomy_menu.vocabulary.' . $record->vocabulary);
// If hide menu is selected and the term count is 0 and the term
// has no children then do not create the menu item.
if ($num == 0 && $config->get('hide_empty_terms') && !_taxonomy_menu_children_has_nodes($record->tid, $record->vocabulary)) {
$display_num = '';
}
// If display number is selected and $num > 0 then change the title.
elseif ($config->get('display_num')) {
$display_num = " ($num)";
}
$term = i18n_taxonomy_localize_terms($term);
$item['title'] = $item['link_title'] = $term->name . $display_num;
if ($config->get('term_item_description')) {
$item['options']['attributes']['title'] = check_plain(strip_tags($term->description));
}
}
}
}
// No term, add a watchdog entry to help.
else {
watchdog('taxonomy_menu', 'Error with menu entry "%me" in menu "%mt"', array('%me' => $item['title'], '%mt' => $item['menu_name']));
}
}
}
}
/**
* Helper function: Gets term depth from a tid.
*
* @param $tid
* Taxonomy term ID.
* @param $max_depth
* Maximum depth.