Projet

Général

Profil

Paste
Télécharger (3,28 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / includes / menu.inc @ c2ac6d1d

1
<?php
2

    
3
/**
4
 * @file
5
 * General menu helper functions.
6
 */
7

    
8
/**
9
 * Dynamically add a tab to the current path.
10
 *
11
 * This function is a simplified interface for adding tabs to the current path.
12
 * Some considerations when doing this:
13
 *
14
 * - First, if there is only 1 tab, Drupal will not show it. Therefore, if
15
 *   you are only adding one tab, you should find a way to make sure there is
16
 *   already tab, or instead add 2.
17
 * - Second, the caller is responsible for providing access control to these
18
 *   links.
19
 *
20
 * @param $link
21
 *   An array describing this link. It must contain:
22
 *   - 'title': The printed title of the link.
23
 *   - 'href': The path of the link. This is an argument to l() so it has all
24
 *     of those features and limitations.
25
 *   - 'options': Any options that go to l, including query, fragment and html
26
 *     options necessary.
27
 *   - 'weight': The weight to use in ordering the tabs.
28
 *   - 'type': Optional. If set to MENU_DEFAULT_LOCAL_TASK this can be used to
29
 *     add a fake 'default' local task, which is useful if you have to add
30
 *     tabs to a page that has none.
31
 */
32
function ctools_menu_add_tab($link = NULL) {
33
  $links = &drupal_static(__FUNCTION__, array());
34
  if (isset($link)) {
35
    $links[$link['href']] = $link;
36
  }
37

    
38
  return $links;
39
}
40

    
41
/**
42
 * Re-sort menu items after we have modified them.
43
 */
44
function ctools_menu_sort($a, $b) {
45
  $a_weight = (is_array($a) && isset($a['#link']['weight'])) ? $a['#link']['weight'] : 0;
46
  $b_weight = (is_array($b) && isset($b['#link']['weight'])) ? $b['#link']['weight'] : 0;
47
  if ($a_weight == $b_weight) {
48
    $a_title = (is_array($a) && isset($a['#link']['title'])) ? $a['#link']['title'] : 0;
49
    $b_title = (is_array($b) && isset($b['#link']['title'])) ? $b['#link']['title'] : 0;
50
    if ($a_title == $b_title) {
51
      return 0;
52
    }
53

    
54
    return ($a_title < $b_title) ? -1 : 1;
55
  }
56

    
57
  return ($a_weight < $b_weight) ? -1 : 1;
58
}
59

    
60
function _ctools_menu_add_dynamic_items(&$data, &$router_item, &$root_path) {
61
  if ($additions = ctools_menu_add_tab()) {
62
    // If none of the static local tasks are active allow one of the dynamic
63
    // active tasks to be marked as such.
64
    $has_active = FALSE;
65
    if (!empty($data['tabs'][0]['output'])) {
66
      foreach ($data['tabs'][0]['output'] as $element) {
67
        if (!empty($element['#link']['#active'])) {
68
          $has_active = TRUE;
69
        }
70
      }
71
    }
72
    foreach ($additions as $addition) {
73
      $addition['localized_options'] = isset($addition['options']) ? $addition['options'] : array();
74
      if (isset($addition['type']) && $addition['type'] == MENU_LOCAL_ACTION) {
75
        $data['actions']['output'][] = array(
76
          '#theme' => 'menu_local_action',
77
          '#link' => $addition,
78
        );
79
      }
80
      else {
81
        $data['tabs'][0]['output'][] = array(
82
          '#theme' => 'menu_local_task',
83
          '#link' => $addition,
84
          '#active' => (!$has_active && $root_path === $addition['href']),
85
        );
86
      }
87
    }
88
    if (!empty($data['tabs'][0]['output'])) {
89
      uasort($data['tabs'][0]['output'], 'ctools_menu_sort');
90
      $data['tabs'][0]['count'] = count($data['tabs'][0]['output']);
91
    }
92

    
93
    if (!empty($data['actions']['output'])) {
94
      uasort($data['actions']['output'], 'ctools_menu_sort');
95
      $data['actions']['count'] = count($data['actions']['output']);
96
    }
97
  }
98
}