Projet

Général

Profil

Paste
Télécharger (4,88 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / dhtml_menu / dhtml_menu.theme.inc @ bad4e148

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
  if (empty($l)) {
30
    return;
31
  }
32

    
33
  // Determine if the menu is blacklisted or not whitelisted.
34
  // First check menu blocks.
35
  if (!empty($variables['element']['#bid']['module']) &&
36
      !empty($variables['element']['#bid']['delta']) &&
37
      $variables['element']['#bid']['module'] == 'menu_block') {
38
    $checked = !empty($settings['filter']['menu_block']['menu_block_' . $variables['element']['#bid']['delta']]);
39
    if ($checked == ($settings['filter']['type'] == 'blacklist')) {
40
      // This menu block is either blacklisted, or not whitelisted, hence it is disabled.
41
      return;
42
    }
43
  }
44
  // Then check menus.
45
  else {
46
    $disabled = ($settings['filter']['type'] == 'blacklist') == !empty($settings['filter']['list'][$l['menu_name']]);
47
    if (empty($l['menu_name']) || empty($l['mlid']) || $disabled) {
48
      return;
49
    }
50
  }
51

    
52
  // Add the ID and class attributes.
53

    
54
  $variables['element']['#attributes']['id'] = 'dhtml_menu-' . _dhtml_menu_unique_id($l['mlid']);
55
  $variables['element']['#attributes']['class'][] = 'dhtml-menu';
56

    
57
  // If there are children, but they were not loaded, load them.
58
  if ($l['has_children'] && !$variables['element']['#below']) {
59
    $variables['element']['#below'] = _dhtml_menu_subtree($l['menu_name'], $l['mlid']);
60
  }
61

    
62
  // If the current item can expand, and is neither saved as open nor in the active trail, close it.
63
  if ($l['has_children'] && !$l['in_active_trail'] && !in_array($variables['element']['#attributes']['id'], $cookie)) {
64
    if (!in_array('collapsed', $variables['element']['#attributes']['class'])) {
65
      $variables['element']['#attributes']['class'][] = 'collapsed';
66
    }
67
    if (!in_array('start-collapsed', $variables['element']['#attributes']['class'])) {
68
      $variables['element']['#attributes']['class'][] = 'start-collapsed';
69
    }
70
  }
71
}
72

    
73
/**
74
 * Traverses the menu tree and returns the sub-tree of the item
75
 * indicated by the parameter.
76
 *
77
 * @param $menu_name
78
 *   The internal name of the menu.
79
 * @param $mlid
80
 *   The menu link ID.
81
 *
82
 * @return
83
 *   The tree below the menu item, as a renderable array, or an empty array.
84
 */
85
function _dhtml_menu_subtree($menu_name, $mlid) {
86
  static $index = array();
87
  static $indexed = array();
88

    
89
  // This looks expensive, but menu_tree_all_data uses static caching.
90
  $tree = menu_tree_all_data($menu_name);
91

    
92
  // Index the menu tree to find ancestor paths for each item.
93
  if (!isset($indexed[$menu_name])) {
94
    $index += _dhtml_menu_index($tree);
95
    $indexed[$menu_name] = TRUE;
96
  }
97

    
98
  // If the menu tree does not contain this item, stop.
99
  if (!isset($index[$mlid])) {
100
    return array();
101
  }
102

    
103
  // Traverse the tree using the ancestor path.
104
  foreach ($index[$mlid]['parents'] as $id) {
105
    $key = $index[$id]['key'];
106
    if (isset($tree[$key])) {
107
      $tree = $tree[$key]['below'];
108
    }
109
    else {
110
      return array();
111
    }
112
  }
113

    
114
  // Go one level further to go below the current item.
115
  $key = $index[$mlid]['key'];
116
  return isset($tree[$key]) ? menu_tree_output($tree[$key]['below']) : array();
117
}
118

    
119
/**
120
 * Indexes the menu tree by mlid. This is needed to identify the items
121
 * without relying on titles or stacks. This function is recursive.
122
 *
123
 * @param $tree
124
 *   A tree of menu items such as the return value of menu_tree_all_data().
125
 * @param $ancestors
126
 *   Optional, used only by internal recursion.
127
 * @param $parent
128
 *   Optional, used only by internal recursion.
129
 *
130
 * @return
131
 *   An array associating mlid values with the internal keys of the menu tree,
132
 *   and all the mlids of the item's ancestors.
133
 */
134
function _dhtml_menu_index($tree, $ancestors = array(), $parent = NULL) {
135
  $index = array();
136
  if ($parent) {
137
    $ancestors[] = $parent;
138
  }
139

    
140
  foreach ($tree as $key => $item) {
141
    $index[$item['link']['mlid']] = array(
142
      'key' => $key,
143
      'parents' => $ancestors,
144
    );
145
    if (!empty($item['below'])) {
146
      $index += _dhtml_menu_index($item['below'], $ancestors, $item['link']['mlid']);
147
    }
148
  }
149
  return $index;
150
}
151

    
152
/**
153
 * Keeps track of ID attributes and adds a suffix to make it unique-when necessary.
154
 */
155
function _dhtml_menu_unique_id($id) {
156
  static $ids = array();
157
  if (!isset($ids[$id])) {
158
    $ids[$id] = 1;
159
    return $id;
160
  }
161
  else {
162
    return $id . '-' . $ids[$id]++;
163
  }
164
}
165