summaryrefslogtreecommitdiff
path: root/modules/toolbar/toolbar.module
blob: 0c050c8a1099fa738ad847253ab6a2d467869335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
// $Id$

/**
 * @file
 * Administration toolbar for quick access to top level administration items.
 */

/**
 * Implementation of hook_permission().
 */
function toolbar_permission() {
  return array(
    'access toolbar' => array(
      'title' => t('Access administration toolbar'),
      'description' => t('Access the persistent administration toolbar displayed on all pages.'),
    ),
  );
}

/**
 * Implement hook_theme().
 */
function toolbar_theme($existing, $type, $theme, $path) {
  $items['toolbar'] = array(
    'render element' => 'toolbar',
    'template' => 'toolbar',
    'path' => drupal_get_path('module', 'toolbar'),
  );
  return $items;
}

/**
 * Implement hook_page_build().
 *
 * Add admin toolbar to the page_top region automatically.
 */
function toolbar_page_build(&$page) {
  $page['page_top']['toolbar'] = array(
    '#pre_render' => array('toolbar_pre_render'),
    '#access' => user_access('access toolbar'),
    'toolbar_drawer' => isset($page['toolbar_drawer']) ? $page['toolbar_drawer'] : array(),
  );
}

/**
 * Prerender function for the toolbar.
 *
 * Since building the toolbar takes some time, it is done just prior to
 * rendering to ensure that it is built only if it will be displayed.
 */
function toolbar_pre_render($toolbar) {
  $toolbar = array_merge($toolbar, toolbar_build());
  return $toolbar;
}

/**
 * Implement hook_preprocess_html().
 *
 * Add some page classes, so global page theming can adjust to the toolbar.
 */
function toolbar_preprocess_html(&$vars) {
  if (user_access('access toolbar')) {
    $vars['classes_array'][] = 'toolbar toolbar-drawer';
  }
}

/**
 * Build the admin menu as a structured array ready for drupal_render().
 */
function toolbar_build() {
  global $user;

  $module_path = drupal_get_path('module', 'toolbar');
  $build = array(
    '#theme' => 'toolbar',
    '#attached'=> array(
      'js' => array(
        $module_path . '/toolbar.js',
        array('data' => 'misc/jquery.cookie.js', 'weight' => JS_LIBRARY + 2),
        array(
          'data' => array('tableHeaderOffset' => 'Drupal.admin.toolbar.height'),
          'type' => 'setting'
        ),
      ),
      'css' => array(
        $module_path . '/toolbar.css',
      ),
    ),
  );

  // Retrieve the admin menu from the database.
  $links = toolbar_menu_navigation_links(toolbar_get_menu_tree());
  $build['toolbar_menu'] = array(
    '#theme' => 'links',
    '#links' => $links,
    '#attributes' => array('id' => 'toolbar-menu'),
  );

  // Add logout & user account links or login link
  if ($user->uid) {
    $links = array(
      'account' => array(
        'title' => t('Hello <strong>@username</strong>', array('@username' => format_username($user))),
        'href' => 'user',
        'html' => TRUE,
      ),
      'logout' => array(
        'title' => t('Log out'),
        'href' => 'user/logout',
      ),
    );
  }
  else {
     $links = array(
      'login' => array(
        'title' => t('Log in'),
        'href' => 'user',
      ),
    );
  }
  $build['toolbar_user'] = array(
    '#theme' => 'links',
    '#links' => $links,
    '#attributes' => array('id' => 'toolbar-user'),
  );
  return $build;
}

/**
 * Get only the top level items below the 'admin' path.
 */
function toolbar_get_menu_tree() {
  $tree = array();
  $admin_link = db_query("SELECT * FROM {menu_links} WHERE menu_name = 'management' AND module = 'system' AND link_path = 'admin'")->fetchAssoc();
  if ($admin_link) {
    // @todo Use a function like book_menu_subtree_data().
    $tree = menu_tree_all_data('management', $admin_link, $admin_link['depth'] + 1);
    // The tree will be a sub-tree with the admin link as a single root item.
    // @todo It is wrong to assume it's the last.
    $admin_link = array_pop($tree);
    $tree = $admin_link['below'] ? $admin_link['below'] : array();
  }
  return $tree;
}

/**
 * Generate a links array from a menu tree array.
 *
 * Based on menu_navigation_links(). Adds in path based IDs, icon placeholders
 * and overlay classes for the links.
 */
function toolbar_menu_navigation_links($tree) {
  $links = array();
  foreach ($tree as $item) {
    if (!$item['link']['hidden'] && $item['link']['access']) {
      $class = '';
      // Make sure we have a path specific ID in place, so we can attach icons
      // and behaviors to the items.
      $id = str_replace(array('/', '<', '>'), array('-', '', ''), $item['link']['href']);

      $link = $item['link']['localized_options'];
      $link['href'] = $item['link']['href'];
      // Add icon placeholder.
      $link['title'] = '<span class="icon"></span>' . $item['link']['title'];
      // Add admin link ID and to-overlay class for the overlay.
      $link['attributes'] = array('id' => 'toolbar-link-' . $id, 'class' => array('to-overlay'));
      $link['html'] = TRUE;

      $class = ' path-' . $id;
      if (toolbar_in_active_trail($item['link']['href'])) {
        $class .= ' active-trail';
      }
      $links['menu-' . $item['link']['mlid'] . $class] = $link;
    }
  }
  return $links;
}

/**
 * Checks whether an item is in the active trail.
 *
 * Useful when using a menu generated by menu_tree_all_data() which does
 * not set the 'in_active_trail' flag on items.
 *
 * @todo
 *   Look at migrating to a menu system level function.
 */
function toolbar_in_active_trail($path) {
  $active_paths = &drupal_static(__FUNCTION__);

  // Gather active paths.
  if (!isset($active_paths)) {
    $active_paths = array();
    $trail = menu_get_active_trail();
    foreach ($trail as $item) {
      if (!empty($item['href'])) {
        $active_paths[] = $item['href'];
      }
    }
  }
  return in_array($path, $active_paths);
}