toolbar.module 10.4 KB
Newer Older
1
2
3
4
5
6
7
<?php

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

8
9
10
11
12
13
14
15
16
17
18
/**
 * Implements hook_help().
 */
function toolbar_help($path, $arg) {
  switch ($path) {
    case 'admin/help#toolbar':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Toolbar module displays links to top-level administration menu items and links from other modules at the top of the screen. For more information, see the online handbook entry for <a href="@toolbar">Toolbar module</a>.', array('@toolbar' => 'http://drupal.org/handbook/modules/toolbar/')) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Displaying administrative links') . '</dt>';
19
      $output .= '<dd>' . t('The Toolbar module displays a bar containing top-level administrative links across the top of the screen. Below that, the Toolbar module has a <em>drawer</em> section where it displays links provided by other modules, such as the core <a href="@shortcuts-help">Shortcut module</a>. The drawer can be hidden/shown by using the show/hide shortcuts link at the end of the toolbar.', array('@shortcuts-help' => url('admin/help/shortcut'))) . '</dd>';
20
21
22
23
24
      $output .= '</dl>';
      return $output;
  }
}

25
/**
26
 * Implements hook_permission().
27
 */
28
function toolbar_permission() {
29
30
  return array(
    'access toolbar' => array(
31
      'title' => t('Use the administration toolbar'),
32
33
34
35
36
    ),
  );
}

/**
37
 * Implements hook_theme().
38
39
40
 */
function toolbar_theme($existing, $type, $theme, $path) {
  $items['toolbar'] = array(
41
    'render element' => 'toolbar',
42
43
44
    'template' => 'toolbar',
    'path' => drupal_get_path('module', 'toolbar'),
  );
45
46
47
48
49
50
51
52
53
54
  $items['toolbar_toggle'] = array(
    'variables' => array(
      'collapsed' => NULL,
      'attributes' => array(),
    ),
  );
  return $items;
}

/**
55
 * Implements hook_menu().
56
57
58
59
60
61
62
63
 */
function toolbar_menu() {
  $items['toolbar/toggle'] = array(
    'title' => 'Toggle drawer visibility',
    'type' => MENU_CALLBACK,
    'page callback' => 'toolbar_toggle_page',
    'access arguments' => array('access toolbar'),
  );
64
65
66
  return $items;
}

67
68
69
70
71
72
/**
 * Menu callback; toggles the visibility of the toolbar drawer.
 */
function toolbar_toggle_page() {
  global $base_path;
  // Toggle the value in the cookie.
73
  setcookie('Drupal.toolbar.collapsed', !_toolbar_is_collapsed(), NULL, $base_path);
74
75
76
77
78
79
  // Redirect the user from where he used the toggle element.
  drupal_goto();
}

/**
 * Formats an element used to toggle the toolbar drawer's visibility.
80
 *
81
82
83
84
85
86
87
88
89
90
91
 * @param $variables
 *   An associative array containing:
 *   - collapsed: A boolean value representing the toolbar drawer's visibility.
 *   - attributes: An associative array of HTML attributes.
 * @return
 *   An HTML string representing the element for toggling.
 *
 * @ingroup themable
 */
function theme_toolbar_toggle($variables) {
  if ($variables['collapsed']) {
92
    $toggle_text = t('Show shortcuts');
93
94
  }
  else {
95
    $toggle_text = t('Hide shortcuts');
96
97
    $variables['attributes']['class'][] = 'toggle-active';
  }
98
  return l($toggle_text, 'toolbar/toggle', array('query' => drupal_get_destination(), 'attributes' => array('title' => $toggle_text) + $variables['attributes']));
99
100
101
102
}

/**
 * Determines the current state of the toolbar drawer's visibility.
103
 *
104
105
106
107
108
109
 * @return
 *   TRUE when drawer is collapsed, FALSE when it is expanded.
 */
function _toolbar_is_collapsed() {
  // PHP converts dots into underscores in cookie names to avoid problems with
  // its parser, so we use a converted cookie name.
110
  return isset($_COOKIE['Drupal_toolbar_collapsed']) ? $_COOKIE['Drupal_toolbar_collapsed'] : 0;
111
112
}

113
/**
114
 * Implements hook_page_build().
115
 *
116
117
 * Add admin toolbar to the page_top region automatically.
 */
118
function toolbar_page_build(&$page) {
119
120
121
  $page['page_top']['toolbar'] = array(
    '#pre_render' => array('toolbar_pre_render'),
    '#access' => user_access('access toolbar'),
122
    'toolbar_drawer' => array(),
123
124
125
126
127
128
129
130
131
132
  );
}

/**
 * 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) {
133
  $toolbar = array_merge($toolbar, toolbar_view());
134
  return $toolbar;
135
136
137
}

/**
138
 * Implements hook_preprocess_html().
139
140
141
 *
 * Add some page classes, so global page theming can adjust to the toolbar.
 */
142
function toolbar_preprocess_html(&$vars) {
143
  if (isset($vars['page']['page_top']['toolbar']) && user_access('access toolbar')) {
144
145
146
147
    $vars['classes_array'][] = 'toolbar';
    if (!_toolbar_is_collapsed()) {
      $vars['classes_array'][] = 'toolbar-drawer';
    }
148
149
150
  }
}

151
152
153
154
155
156
157
158
159
160
/**
 * Implements hook_preprocess_toolbar().
 *
 * Adding the 'overlay-displace-top' class to the toolbar pushes the overlay
 * down, so it appears below the toolbar.
 */
function toolbar_preprocess_toolbar(&$variables) {
  $variables['classes_array'][] = "overlay-displace-top";
}

161
162
163
/**
 * Implements hook_system_info_alter().
 *
164
165
166
167
168
169
 * Indicate that the 'page_top' region (in which the toolbar will be displayed)
 * is an overlay supplemental region that should be refreshed whenever its
 * content is updated.
 *
 * This information is provided for any module that might need to use it, not
 * just the core Overlay module.
170
171
 */
function toolbar_system_info_alter(&$info, $file, $type) {
172
  if ($type == 'theme') {
173
174
175
176
    $info['overlay_supplemental_regions'][] = 'page_top';
  }
}

177
178
179
/**
 * Build the admin menu as a structured array ready for drupal_render().
 */
180
function toolbar_view() {
181
182
183
184
185
  global $user;

  $module_path = drupal_get_path('module', 'toolbar');
  $build = array(
    '#theme' => 'toolbar',
186
187
    '#attached'=> array(
      'js' => array(
188
        $module_path . '/toolbar.js',
Dries's avatar
Dries committed
189
190
191
192
        array(
          'data' => array('tableHeaderOffset' => 'Drupal.toolbar.height'),
          'type' => 'setting'
        ),
193
194
195
196
      ),
      'css' => array(
        $module_path . '/toolbar.css',
      ),
197
      'library' => array(array('system', 'jquery.cookie')),
198
199
200
201
202
203
    ),
  );

  // Retrieve the admin menu from the database.
  $links = toolbar_menu_navigation_links(toolbar_get_menu_tree());
  $build['toolbar_menu'] = array(
204
    '#theme' => 'links__toolbar_menu',
205
206
    '#links' => $links,
    '#attributes' => array('id' => 'toolbar-menu'),
207
    '#heading' => array('text' => t('Administrative toolbar'), 'level' => 'h2', 'class' => 'element-invisible'),
208
209
  );

210
  // Add logout & user account links or login link.
211
212
  if ($user->uid) {
    $links = array(
213
      'account' => array(
214
        'title' => t('Hello <strong>@username</strong>', array('@username' => format_username($user))),
215
216
        'href' => 'user',
        'html' => TRUE,
217
        'attributes' => array('title' => t('User account')),
218
219
      ),
      'logout' => array(
220
        'title' => t('Log out'),
221
222
        'href' => 'user/logout',
      ),
223
224
225
226
227
228
229
230
231
232
233
    );
  }
  else {
     $links = array(
      'login' => array(
        'title' => t('Log in'),
        'href' => 'user',
      ),
    );
  }
  $build['toolbar_user'] = array(
234
    '#theme' => 'links__toolbar_user',
235
    '#links' => $links,
236
237
    '#attributes' => array('id' => 'toolbar-user'),
  );
238

239
240
241
242
243
244
  // Add a "home" link.
  $link = array(
    'home' => array(
      'title' => '<span class="home-link">Home</span>',
      'href' => '<front>',
      'html' => TRUE,
245
      'attributes' => array('title' => t('Home')),
246
247
248
249
250
251
252
    ),
  );
  $build['toolbar_home'] = array(
    '#theme' => 'links',
    '#links' => $link,
    '#attributes' => array('id' => 'toolbar-home'),
  );
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

  // Add an anchor to be able to toggle the visibility of the drawer.
  $build['toolbar_toggle'] = array(
    '#theme' => 'toolbar_toggle',
    '#collapsed' => _toolbar_is_collapsed(),
    '#attributes' => array('class' => array('toggle')),
  );

  // Prepare the drawer links CSS classes.
  $toolbar_drawer_classes = array(
    'toolbar-drawer',
    'clearfix',
  );
  if(_toolbar_is_collapsed()) {
    $toolbar_drawer_classes[] = 'collapsed';
  }
  $build['toolbar_drawer_classes'] = implode(' ', $toolbar_drawer_classes);

271
272
273
274
275
276
277
  return $build;
}

/**
 * Get only the top level items below the 'admin' path.
 */
function toolbar_get_menu_tree() {
278
  $tree = array();
279
  $admin_link = db_query('SELECT * FROM {menu_links} WHERE menu_name = :menu_name AND module = :module AND link_path = :path', array(':menu_name' => 'management', ':module' => 'system', ':path' => 'admin'))->fetchAssoc();
280
  if ($admin_link) {
281
282
283
284
285
    $tree = menu_build_tree('management', array(
      'expanded' => array($admin_link['mlid']),
      'min_depth' => $admin_link['depth'] + 1,
      'max_depth' => $admin_link['depth'] + 1,
    ));
286
  }
287

288
289
290
291
292
293
  return $tree;
}

/**
 * Generate a links array from a menu tree array.
 *
294
295
 * Based on menu_navigation_links(). Adds path based IDs and icon placeholders
 * to the links.
296
297
298
299
300
301
302
303
304
305
306
307
 */
function toolbar_menu_navigation_links($tree) {
  $links = array();
  foreach ($tree as $item) {
    if (!$item['link']['hidden'] && $item['link']['access']) {
      // 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.
308
      $link['title'] = '<span class="icon"></span>' . check_plain($item['link']['title']);
309
310
      // Add admin link ID.
      $link['attributes'] = array('id' => 'toolbar-link-' . $id);
311
312
313
314
      if (!empty($item['link']['description'])) {
        $link['title'] .= ' <span class="element-invisible">(' . $item['link']['description'] . ')</span>';
        $link['attributes']['title'] = $item['link']['description'];
      }
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
      $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.
 *
330
 * Useful when using a menu generated by menu_tree_all_data() which does
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
 * 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);
}