Projet

Général

Profil

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

root / drupal7 / sites / all / modules / dhtml_menu / dhtml_menu.theme.inc @ 87dbc3bf

1
<?php
2
// $Id: dhtml_menu.theme.inc,v 1.14 2009/11/12 21:47:59 arancaytar Exp $
3

    
4

    
5
/**
6
 * @file dhtml_menu.theme.inc
7
 * All functions related to generating the menu markup.
8
 */
9

    
10
/**
11
 * Preprocessor for menu_link.
12
 * Adds the required HTML attributes and loads subtrees if necessary.
13
 */
14
function dhtml_menu_preprocess_menu_link(&$variables) {
15
  $cookie = &drupal_static(__FUNCTION__);
16
  $settings = variable_get('dhtml_menu_settings');
17

    
18
  // Parse the cookie, if it is enabled.
19
  if (!isset($cookie)) {
20
    if ($settings['effects']['remember'] && $settings['nav'] != 'open' && $settings['effects']['siblings'] != 'close-all') {
21
      $cookie = explode(',', @$_COOKIE['dhtml_menu']);
22
    }
23
    else {
24
      $cookie = array();
25
    }
26
  }
27

    
28
  // Saves a lot of code.
29
  $l = &$variables['element']['#original_link'];
30

    
31
  // Determine if the menu is blacklisted or not whitelisted.
32
  $disabled = ($settings['filter']['type'] == 'blacklist') == !empty($settings['filter']['list'][$l['menu_name']]);
33
  if (empty($l['menu_name']) || empty($l['mlid']) || $disabled) {
34
    return;
35
  }
36

    
37
  // Add the ID and class attributes.
38

    
39
  $variables['element']['#attributes']['id'] = 'dhtml_menu-' . _dhtml_menu_unique_id($l['mlid']);
40
  $variables['element']['#attributes']['class'][] = 'dhtml-menu';
41

    
42
  // If there are children, but they were not loaded, load them.
43
  if ($l['has_children'] && !$variables['element']['#below']) {
44
    $variables['element']['#below'] = _dhtml_menu_subtree($l['menu_name'], $l['mlid']);
45
  }
46

    
47
  // If the current item can expand, and is neither saved as open nor in the active trail, close it.
48
  if ($l['has_children'] && !$l['in_active_trail'] && !in_array($variables['element']['#attributes']['id'], $cookie)) {
49
    $variables['element']['#attributes']['class'][] = 'collapsed';
50
    $variables['element']['#attributes']['class'][] = 'start-collapsed';
51
  }
52
}
53

    
54
/**
55
 * Traverses the menu tree and returns the sub-tree of the item
56
 * indicated by the parameter.
57
 *
58
 * @param $menu_name
59
 *   The internal name of the menu.
60
 * @param $mlid
61
 *   The menu link ID.
62
 *
63
 * @return
64
 *   The tree below the menu item, as a renderable array, or an empty array.
65
 */
66
function _dhtml_menu_subtree($menu_name, $mlid) {
67
  static $index = array();
68
  static $indexed = array();
69

    
70
  // This looks expensive, but menu_tree_all_data uses static caching.
71
  $tree = menu_tree_all_data($menu_name);
72

    
73
  // Index the menu tree to find ancestor paths for each item.
74
  if (!isset($indexed[$menu_name])) {
75
    $index += _dhtml_menu_index($tree);
76
    $indexed[$menu_name] = TRUE;
77
  }
78

    
79
  // If the menu tree does not contain this item, stop.
80
  if (!isset($index[$mlid])) {
81
    return array();
82
  }
83

    
84
  // Traverse the tree using the ancestor path.
85
  foreach ($index[$mlid]['parents'] as $id) {
86
    $key = $index[$id]['key'];
87
    if (isset($tree[$key])) {
88
      $tree = $tree[$key]['below'];
89
    }
90
    else {
91
      return array();
92
    }
93
  }
94

    
95
  // Go one level further to go below the current item.
96
  $key = $index[$mlid]['key'];
97
  return isset($tree[$key]) ? menu_tree_output($tree[$key]['below']) : array();
98
}
99

    
100
/**
101
 * Indexes the menu tree by mlid. This is needed to identify the items
102
 * without relying on titles or stacks. This function is recursive.
103
 *
104
 * @param $tree
105
 *   A tree of menu items such as the return value of menu_tree_all_data().
106
 * @param $ancestors
107
 *   Optional, used only by internal recursion.
108
 * @param $parent
109
 *   Optional, used only by internal recursion.
110
 *
111
 * @return
112
 *   An array associating mlid values with the internal keys of the menu tree,
113
 *   and all the mlids of the item's ancestors.
114
 */
115
function _dhtml_menu_index($tree, $ancestors = array(), $parent = NULL) {
116
  $index = array();
117
  if ($parent) {
118
    $ancestors[] = $parent;
119
  }
120

    
121
  foreach ($tree as $key => $item) {
122
    $index[$item['link']['mlid']] = array(
123
      'key' => $key,
124
      'parents' => $ancestors,
125
    );
126
    if (!empty($item['below'])) {
127
      $index += _dhtml_menu_index($item['below'], $ancestors, $item['link']['mlid']);
128
    }
129
  }
130
  return $index;
131
}
132

    
133
/**
134
 * Keeps track of ID attributes and adds a suffix to make it unique-when necessary.
135
 */
136
function _dhtml_menu_unique_id($id) {
137
  static $ids = array();
138
  if (!isset($ids[$id])) {
139
    $ids[$id] = 1;
140
    return $id;
141
  }
142
  else {
143
    return $id . '-' . $ids[$id]++;
144
  }
145
}
146