Projet

Général

Profil

Paste
Télécharger (10,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / toolbar / toolbar.module @ c7768a53

1
<?php
2

    
3
/**
4
 * @file
5
 * Administration toolbar for quick access to top level administration items.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function toolbar_help($path, $arg) {
12
  switch ($path) {
13
    case 'admin/help#toolbar':
14
      $output = '<h3>' . t('About') . '</h3>';
15
      $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/documentation/modules/toolbar/')) . '</p>';
16
      $output .= '<h3>' . t('Uses') . '</h3>';
17
      $output .= '<dl>';
18
      $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
      $output .= '</dl>';
21
      return $output;
22
  }
23
}
24

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

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

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

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

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

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

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

    
127
/**
128
 * Prerender function for the toolbar.
129
 *
130
 * Since building the toolbar takes some time, it is done just prior to
131
 * rendering to ensure that it is built only if it will be displayed.
132
 */
133
function toolbar_pre_render($toolbar) {
134
  $toolbar = array_merge($toolbar, toolbar_view());
135
  return $toolbar;
136
}
137

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

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

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

    
178
/**
179
 * Builds the admin menu as a structured array ready for drupal_render().
180
 *
181
 * @return
182
 *   Array of links and settings relating to the admin menu.
183
 */
184
function toolbar_view() {
185
  global $user;
186

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

    
208
  // Retrieve the admin menu from the database.
209
  $links = toolbar_menu_navigation_links(toolbar_get_menu_tree());
210
  $build['toolbar_menu'] = array(
211
    '#theme' => 'links__toolbar_menu',
212
    '#links' => $links,
213
    '#attributes' => array('id' => 'toolbar-menu'),
214
    '#heading' => array('text' => t('Administrative toolbar'), 'level' => 'h2', 'class' => 'element-invisible'),
215
  );
216

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

    
246
  // Add a "home" link.
247
  $link = array(
248
    'home' => array(
249
      'title' => '<span class="home-link">Home</span>',
250
      'href' => '<front>',
251
      'html' => TRUE,
252
      'attributes' => array('title' => t('Home')),
253
    ),
254
  );
255
  $build['toolbar_home'] = array(
256
    '#theme' => 'links',
257
    '#links' => $link,
258
    '#attributes' => array('id' => 'toolbar-home'),
259
  );
260

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

    
268
  // Prepare the drawer links CSS classes.
269
  $toolbar_drawer_classes = array(
270
    'toolbar-drawer',
271
    'clearfix',
272
  );
273
  if(_toolbar_is_collapsed()) {
274
    $toolbar_drawer_classes[] = 'collapsed';
275
  }
276
  $build['toolbar_drawer_classes'] = implode(' ', $toolbar_drawer_classes);
277

    
278
  return $build;
279
}
280

    
281
/**
282
 * Gets only the top level items below the 'admin' path.
283
 *
284
 * @return
285
 *   An array containing a menu tree of top level items below the 'admin' path.
286
 */
287
function toolbar_get_menu_tree() {
288
  $tree = array();
289
  $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();
290
  if ($admin_link) {
291
    $tree = menu_build_tree('management', array(
292
      'expanded' => array($admin_link['mlid']),
293
      'min_depth' => $admin_link['depth'] + 1,
294
      'max_depth' => $admin_link['depth'] + 1,
295
    ));
296
  }
297

    
298
  return $tree;
299
}
300

    
301
/**
302
 * Generates a links array from a menu tree array.
303
 *
304
 * Based on menu_navigation_links(). Adds path based IDs and icon placeholders
305
 * to the links.
306
 *
307
 * @return
308
 *   An array of links as defined above.
309
 */
310
function toolbar_menu_navigation_links($tree) {
311
  $links = array();
312
  foreach ($tree as $item) {
313
    if (!$item['link']['hidden'] && $item['link']['access']) {
314
      // Make sure we have a path specific ID in place, so we can attach icons
315
      // and behaviors to the items.
316
      $id = str_replace(array('/', '<', '>'), array('-', '', ''), $item['link']['href']);
317

    
318
      $link = $item['link']['localized_options'];
319
      $link['href'] = $item['link']['href'];
320
      // Add icon placeholder.
321
      $link['title'] = '<span class="icon"></span>' . check_plain($item['link']['title']);
322
      // Add admin link ID.
323
      $link['attributes'] = array('id' => 'toolbar-link-' . $id);
324
      if (!empty($item['link']['description'])) {
325
        $link['title'] .= ' <span class="element-invisible">(' . $item['link']['description'] . ')</span>';
326
        $link['attributes']['title'] = $item['link']['description'];
327
      }
328
      $link['html'] = TRUE;
329

    
330
      $class = ' path-' . $id;
331
      if (toolbar_in_active_trail($item['link']['href'])) {
332
        $class .= ' active-trail';
333
      }
334
      $links['menu-' . $item['link']['mlid'] . $class] = $link;
335
    }
336
  }
337
  return $links;
338
}
339

    
340
/**
341
 * Checks whether an item is in the active trail.
342
 *
343
 * Useful when using a menu generated by menu_tree_all_data() which does
344
 * not set the 'in_active_trail' flag on items.
345
 *
346
 * @return
347
 *   TRUE when path is in the active trail, FALSE if not.
348
 *
349
 * @todo
350
 *   Look at migrating to a menu system level function.
351
 */
352
function toolbar_in_active_trail($path) {
353
  $active_paths = &drupal_static(__FUNCTION__);
354

    
355
  // Gather active paths.
356
  if (!isset($active_paths)) {
357
    $active_paths = array();
358
    $trail = menu_get_active_trail();
359
    foreach ($trail as $item) {
360
      if (!empty($item['href'])) {
361
        $active_paths[] = $item['href'];
362
      }
363
    }
364
  }
365
  return in_array($path, $active_paths);
366
}