1
|
<?php
|
2
|
|
3
|
|
4
|
/**
|
5
|
* @file dhtml_menu.theme.inc
|
6
|
* All functions related to generating the menu markup.
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Preprocessor for menu_link.
|
11
|
* Adds the required HTML attributes and loads subtrees if necessary.
|
12
|
*/
|
13
|
function dhtml_menu_preprocess_menu_link(&$variables) {
|
14
|
$cookie = &drupal_static(__FUNCTION__);
|
15
|
$settings = variable_get('dhtml_menu_settings');
|
16
|
|
17
|
// Parse the cookie, if it is enabled.
|
18
|
if (!isset($cookie)) {
|
19
|
if ($settings['effects']['remember'] && $settings['nav'] != 'open' && $settings['effects']['siblings'] != 'close-all') {
|
20
|
$cookie = explode(',', @$_COOKIE['dhtml_menu']);
|
21
|
}
|
22
|
else {
|
23
|
$cookie = array();
|
24
|
}
|
25
|
}
|
26
|
|
27
|
// Saves a lot of code.
|
28
|
$l = &$variables['element']['#original_link'];
|
29
|
|
30
|
// Determine if the menu is blacklisted or not whitelisted.
|
31
|
// First check menu blocks.
|
32
|
if (!empty($variables['element']['#bid']['module']) &&
|
33
|
!empty($variables['element']['#bid']['delta']) &&
|
34
|
$variables['element']['#bid']['module'] == 'menu_block') {
|
35
|
$checked = !empty($settings['filter']['menu_block']['menu_block_' . $variables['element']['#bid']['delta']]);
|
36
|
if ($checked == ($settings['filter']['type'] == 'blacklist')) {
|
37
|
// This menu block is either blacklisted, or not whitelisted, hence it is disabled.
|
38
|
return;
|
39
|
}
|
40
|
}
|
41
|
// Then check menus.
|
42
|
else {
|
43
|
$disabled = ($settings['filter']['type'] == 'blacklist') == !empty($settings['filter']['list'][$l['menu_name']]);
|
44
|
if (empty($l['menu_name']) || empty($l['mlid']) || $disabled) {
|
45
|
return;
|
46
|
}
|
47
|
}
|
48
|
|
49
|
// Add the ID and class attributes.
|
50
|
|
51
|
$variables['element']['#attributes']['id'] = 'dhtml_menu-' . _dhtml_menu_unique_id($l['mlid']);
|
52
|
$variables['element']['#attributes']['class'][] = 'dhtml-menu';
|
53
|
|
54
|
// If there are children, but they were not loaded, load them.
|
55
|
if ($l['has_children'] && !$variables['element']['#below']) {
|
56
|
$variables['element']['#below'] = _dhtml_menu_subtree($l['menu_name'], $l['mlid']);
|
57
|
}
|
58
|
|
59
|
// If the current item can expand, and is neither saved as open nor in the active trail, close it.
|
60
|
if ($l['has_children'] && !$l['in_active_trail'] && !in_array($variables['element']['#attributes']['id'], $cookie)) {
|
61
|
if (!in_array('collapsed', $variables['element']['#attributes']['class'])) {
|
62
|
$variables['element']['#attributes']['class'][] = 'collapsed';
|
63
|
}
|
64
|
if (!in_array('start-collapsed', $variables['element']['#attributes']['class'])) {
|
65
|
$variables['element']['#attributes']['class'][] = 'start-collapsed';
|
66
|
}
|
67
|
}
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Traverses the menu tree and returns the sub-tree of the item
|
72
|
* indicated by the parameter.
|
73
|
*
|
74
|
* @param $menu_name
|
75
|
* The internal name of the menu.
|
76
|
* @param $mlid
|
77
|
* The menu link ID.
|
78
|
*
|
79
|
* @return
|
80
|
* The tree below the menu item, as a renderable array, or an empty array.
|
81
|
*/
|
82
|
function _dhtml_menu_subtree($menu_name, $mlid) {
|
83
|
static $index = array();
|
84
|
static $indexed = array();
|
85
|
|
86
|
// This looks expensive, but menu_tree_all_data uses static caching.
|
87
|
$tree = menu_tree_all_data($menu_name);
|
88
|
|
89
|
// Index the menu tree to find ancestor paths for each item.
|
90
|
if (!isset($indexed[$menu_name])) {
|
91
|
$index += _dhtml_menu_index($tree);
|
92
|
$indexed[$menu_name] = TRUE;
|
93
|
}
|
94
|
|
95
|
// If the menu tree does not contain this item, stop.
|
96
|
if (!isset($index[$mlid])) {
|
97
|
return array();
|
98
|
}
|
99
|
|
100
|
// Traverse the tree using the ancestor path.
|
101
|
foreach ($index[$mlid]['parents'] as $id) {
|
102
|
$key = $index[$id]['key'];
|
103
|
if (isset($tree[$key])) {
|
104
|
$tree = $tree[$key]['below'];
|
105
|
}
|
106
|
else {
|
107
|
return array();
|
108
|
}
|
109
|
}
|
110
|
|
111
|
// Go one level further to go below the current item.
|
112
|
$key = $index[$mlid]['key'];
|
113
|
return isset($tree[$key]) ? menu_tree_output($tree[$key]['below']) : array();
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Indexes the menu tree by mlid. This is needed to identify the items
|
118
|
* without relying on titles or stacks. This function is recursive.
|
119
|
*
|
120
|
* @param $tree
|
121
|
* A tree of menu items such as the return value of menu_tree_all_data().
|
122
|
* @param $ancestors
|
123
|
* Optional, used only by internal recursion.
|
124
|
* @param $parent
|
125
|
* Optional, used only by internal recursion.
|
126
|
*
|
127
|
* @return
|
128
|
* An array associating mlid values with the internal keys of the menu tree,
|
129
|
* and all the mlids of the item's ancestors.
|
130
|
*/
|
131
|
function _dhtml_menu_index($tree, $ancestors = array(), $parent = NULL) {
|
132
|
$index = array();
|
133
|
if ($parent) {
|
134
|
$ancestors[] = $parent;
|
135
|
}
|
136
|
|
137
|
foreach ($tree as $key => $item) {
|
138
|
$index[$item['link']['mlid']] = array(
|
139
|
'key' => $key,
|
140
|
'parents' => $ancestors,
|
141
|
);
|
142
|
if (!empty($item['below'])) {
|
143
|
$index += _dhtml_menu_index($item['below'], $ancestors, $item['link']['mlid']);
|
144
|
}
|
145
|
}
|
146
|
return $index;
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* Keeps track of ID attributes and adds a suffix to make it unique-when necessary.
|
151
|
*/
|
152
|
function _dhtml_menu_unique_id($id) {
|
153
|
static $ids = array();
|
154
|
if (!isset($ids[$id])) {
|
155
|
$ids[$id] = 1;
|
156
|
return $id;
|
157
|
}
|
158
|
else {
|
159
|
return $id . '-' . $ids[$id]++;
|
160
|
}
|
161
|
}
|
162
|
|