danielebarchiesi@0
|
1 <?php
|
danielebarchiesi@0
|
2
|
danielebarchiesi@0
|
3 /**
|
danielebarchiesi@0
|
4 * @file
|
danielebarchiesi@0
|
5 * General menu helper functions.
|
danielebarchiesi@0
|
6 */
|
danielebarchiesi@0
|
7
|
danielebarchiesi@0
|
8 /**
|
danielebarchiesi@0
|
9 * Dynamically add a tab to the current path.
|
danielebarchiesi@0
|
10 *
|
danielebarchiesi@0
|
11 * This function is a simplified interface for adding tabs to the current path.
|
danielebarchiesi@0
|
12 * Some considerations when doing this:
|
danielebarchiesi@0
|
13 *
|
danielebarchiesi@0
|
14 * - First, if there is only 1 tab, Drupal will not show it. Therefore, if
|
danielebarchiesi@0
|
15 * you are only adding one tab, you should find a way to make sure there is
|
danielebarchiesi@0
|
16 * already tab, or instead add 2.
|
danielebarchiesi@0
|
17 * - Second, the caller is responsible for providing access control to these
|
danielebarchiesi@0
|
18 * links.
|
danielebarchiesi@0
|
19 *
|
danielebarchiesi@0
|
20 * @param $link
|
danielebarchiesi@0
|
21 * An array describing this link. It must contain:
|
danielebarchiesi@0
|
22 * - 'title': The printed title of the link.
|
danielebarchiesi@0
|
23 * - 'href': The path of the link. This is an argument to l() so it has all
|
danielebarchiesi@0
|
24 * of those features and limitations.
|
danielebarchiesi@0
|
25 * - 'options': Any options that go to l, including query, fragment and html
|
danielebarchiesi@0
|
26 * options necessary.
|
danielebarchiesi@0
|
27 * - 'weight': The weight to use in ordering the tabs.
|
danielebarchiesi@0
|
28 * - 'type': Optional. If set to MENU_DEFAULT_LOCAL_TASK this can be used to
|
danielebarchiesi@0
|
29 * add a fake 'default' local task, which is useful if you have to add
|
danielebarchiesi@0
|
30 * tabs to a page that has none.
|
danielebarchiesi@0
|
31 */
|
danielebarchiesi@0
|
32 function ctools_menu_add_tab($link = NULL) {
|
danielebarchiesi@0
|
33 $links = &drupal_static(__FUNCTION__, array());
|
danielebarchiesi@0
|
34 if (isset($link)) {
|
danielebarchiesi@0
|
35 $links[$link['href']] = $link;
|
danielebarchiesi@0
|
36 }
|
danielebarchiesi@0
|
37
|
danielebarchiesi@0
|
38 return $links;
|
danielebarchiesi@0
|
39 }
|
danielebarchiesi@0
|
40
|
danielebarchiesi@0
|
41 /**
|
danielebarchiesi@0
|
42 * Re-sort menu items after we have modified them.
|
danielebarchiesi@0
|
43 */
|
danielebarchiesi@0
|
44 function ctools_menu_sort($a, $b) {
|
danielebarchiesi@0
|
45 $a_weight = (is_array($a) && isset($a['#link']['weight'])) ? $a['#link']['weight'] : 0;
|
danielebarchiesi@0
|
46 $b_weight = (is_array($b) && isset($b['#link']['weight'])) ? $b['#link']['weight'] : 0;
|
danielebarchiesi@0
|
47 if ($a_weight == $b_weight) {
|
danielebarchiesi@0
|
48 $a_title = (is_array($a) && isset($a['#link']['title'])) ? $a['#link']['title'] : 0;
|
danielebarchiesi@0
|
49 $b_title = (is_array($b) && isset($b['#link']['title'])) ? $b['#link']['title'] : 0;
|
danielebarchiesi@0
|
50 if ($a_title == $b_title) {
|
danielebarchiesi@0
|
51 return 0;
|
danielebarchiesi@0
|
52 }
|
danielebarchiesi@0
|
53
|
danielebarchiesi@0
|
54 return ($a_title < $b_title) ? -1 : 1;
|
danielebarchiesi@0
|
55 }
|
danielebarchiesi@0
|
56
|
danielebarchiesi@0
|
57 return ($a_weight < $b_weight) ? -1 : 1;
|
danielebarchiesi@0
|
58 }
|
danielebarchiesi@0
|
59
|
danielebarchiesi@0
|
60 function _ctools_menu_add_dynamic_items(&$data, &$router_item, &$root_path) {
|
danielebarchiesi@0
|
61 if ($additions = ctools_menu_add_tab()) {
|
danielebarchiesi@0
|
62 // If none of the static local tasks are active allow one of the dynamic
|
danielebarchiesi@0
|
63 // active tasks to be marked as such.
|
danielebarchiesi@0
|
64 $has_active = FALSE;
|
danielebarchiesi@0
|
65 if (!empty($data['tabs'][0]['output'])) {
|
danielebarchiesi@0
|
66 foreach ($data['tabs'][0]['output'] as $element) {
|
danielebarchiesi@0
|
67 if (!empty($element['#link']['#active'])) {
|
danielebarchiesi@0
|
68 $has_active = TRUE;
|
danielebarchiesi@0
|
69 }
|
danielebarchiesi@0
|
70 }
|
danielebarchiesi@0
|
71 }
|
danielebarchiesi@0
|
72 foreach ($additions as $addition) {
|
danielebarchiesi@0
|
73 $addition['localized_options'] = isset($addition['options']) ? $addition['options'] : array();
|
danielebarchiesi@0
|
74 if (isset($addition['type']) && $addition['type'] == MENU_LOCAL_ACTION) {
|
danielebarchiesi@0
|
75 $data['actions']['output'][] = array(
|
danielebarchiesi@0
|
76 '#theme' => 'menu_local_action',
|
danielebarchiesi@0
|
77 '#link' => $addition,
|
danielebarchiesi@0
|
78 );
|
danielebarchiesi@0
|
79 }
|
danielebarchiesi@0
|
80 else {
|
danielebarchiesi@0
|
81 $data['tabs'][0]['output'][] = array(
|
danielebarchiesi@0
|
82 '#theme' => 'menu_local_task',
|
danielebarchiesi@0
|
83 '#link' => $addition,
|
danielebarchiesi@0
|
84 '#active' => (!$has_active && $root_path === $addition['href']),
|
danielebarchiesi@0
|
85 );
|
danielebarchiesi@0
|
86 }
|
danielebarchiesi@0
|
87 }
|
danielebarchiesi@0
|
88 if (!empty($data['tabs'][0]['output'])) {
|
danielebarchiesi@0
|
89 uasort($data['tabs'][0]['output'], 'ctools_menu_sort');
|
danielebarchiesi@0
|
90 $data['tabs'][0]['count'] = count($data['tabs'][0]['output']);
|
danielebarchiesi@0
|
91 }
|
danielebarchiesi@0
|
92
|
danielebarchiesi@0
|
93 if (!empty($data['actions']['output'])) {
|
danielebarchiesi@0
|
94 uasort($data['actions']['output'], 'ctools_menu_sort');
|
danielebarchiesi@0
|
95 $data['actions']['count'] = count($data['actions']['output']);
|
danielebarchiesi@0
|
96 }
|
danielebarchiesi@0
|
97 }
|
danielebarchiesi@0
|
98 }
|