| 1 |
<?php
|
| 2 |
// $Id: toolbar.module,v 1.17 2009/10/30 21:43:41 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Administration toolbar for quick access to top level administration items.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_permission().
|
| 11 |
*/
|
| 12 |
function toolbar_permission() {
|
| 13 |
return array(
|
| 14 |
'access toolbar' => array(
|
| 15 |
'title' => t('Access administration toolbar'),
|
| 16 |
'description' => t('Access the persistent administration toolbar displayed on all pages.'),
|
| 17 |
),
|
| 18 |
);
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implement hook_theme().
|
| 23 |
*/
|
| 24 |
function toolbar_theme($existing, $type, $theme, $path) {
|
| 25 |
$items['toolbar'] = array(
|
| 26 |
'render element' => 'toolbar',
|
| 27 |
'template' => 'toolbar',
|
| 28 |
'path' => drupal_get_path('module', 'toolbar'),
|
| 29 |
);
|
| 30 |
return $items;
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implement hook_page_build().
|
| 35 |
*
|
| 36 |
* Add admin toolbar to the page_top region automatically.
|
| 37 |
*/
|
| 38 |
function toolbar_page_build(&$page) {
|
| 39 |
$page['page_top']['toolbar'] = array(
|
| 40 |
'#pre_render' => array('toolbar_pre_render'),
|
| 41 |
'#access' => user_access('access toolbar'),
|
| 42 |
'toolbar_drawer' => isset($page['toolbar_drawer']) ? $page['toolbar_drawer'] : array(),
|
| 43 |
);
|
| 44 |
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Prerender function for the toolbar.
|
| 48 |
*
|
| 49 |
* Since building the toolbar takes some time, it is done just prior to
|
| 50 |
* rendering to ensure that it is built only if it will be displayed.
|
| 51 |
*/
|
| 52 |
function toolbar_pre_render($toolbar) {
|
| 53 |
$toolbar = array_merge($toolbar, toolbar_build());
|
| 54 |
return $toolbar;
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Implement hook_preprocess_html().
|
| 59 |
*
|
| 60 |
* Add some page classes, so global page theming can adjust to the toolbar.
|
| 61 |
*/
|
| 62 |
function toolbar_preprocess_html(&$vars) {
|
| 63 |
if (user_access('access toolbar')) {
|
| 64 |
$vars['classes_array'][] = 'toolbar toolbar-drawer';
|
| 65 |
}
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Build the admin menu as a structured array ready for drupal_render().
|
| 70 |
*/
|
| 71 |
function toolbar_build() {
|
| 72 |
global $user;
|
| 73 |
|
| 74 |
$module_path = drupal_get_path('module', 'toolbar');
|
| 75 |
$build = array(
|
| 76 |
'#theme' => 'toolbar',
|
| 77 |
'#attached'=> array(
|
| 78 |
'js' => array(
|
| 79 |
$module_path . '/toolbar.js',
|
| 80 |
array('data' => 'misc/jquery.cookie.js', 'weight' => JS_LIBRARY + 2),
|
| 81 |
array(
|
| 82 |
'data' => array('tableHeaderOffset' => 'Drupal.admin.toolbar.height'),
|
| 83 |
'type' => 'setting'
|
| 84 |
),
|
| 85 |
),
|
| 86 |
'css' => array(
|
| 87 |
$module_path . '/toolbar.css',
|
| 88 |
),
|
| 89 |
),
|
| 90 |
);
|
| 91 |
|
| 92 |
// Retrieve the admin menu from the database.
|
| 93 |
$links = toolbar_menu_navigation_links(toolbar_get_menu_tree());
|
| 94 |
$build['toolbar_menu'] = array(
|
| 95 |
'#theme' => 'links',
|
| 96 |
'#links' => $links,
|
| 97 |
'#attributes' => array('id' => 'toolbar-menu'),
|
| 98 |
);
|
| 99 |
|
| 100 |
// Add logout & user account links or login link
|
| 101 |
if ($user->uid) {
|
| 102 |
$links = array(
|
| 103 |
'account' => array(
|
| 104 |
'title' => t('Hello <strong>@username</strong>', array('@username' => format_username($user))),
|
| 105 |
'href' => 'user',
|
| 106 |
'html' => TRUE,
|
| 107 |
),
|
| 108 |
'logout' => array(
|
| 109 |
'title' => t('Log out'),
|
| 110 |
'href' => 'user/logout',
|
| 111 |
),
|
| 112 |
);
|
| 113 |
}
|
| 114 |
else {
|
| 115 |
$links = array(
|
| 116 |
'login' => array(
|
| 117 |
'title' => t('Log in'),
|
| 118 |
'href' => 'user',
|
| 119 |
),
|
| 120 |
);
|
| 121 |
}
|
| 122 |
$build['toolbar_user'] = array(
|
| 123 |
'#theme' => 'links',
|
| 124 |
'#links' => $links,
|
| 125 |
'#attributes' => array('id' => 'toolbar-user'),
|
| 126 |
);
|
| 127 |
return $build;
|
| 128 |
}
|
| 129 |
|
| 130 |
/**
|
| 131 |
* Get only the top level items below the 'admin' path.
|
| 132 |
*/
|
| 133 |
function toolbar_get_menu_tree() {
|
| 134 |
$tree = array();
|
| 135 |
$admin_link = db_query("SELECT * FROM {menu_links} WHERE menu_name = 'management' AND module = 'system' AND link_path = 'admin'")->fetchAssoc();
|
| 136 |
if ($admin_link) {
|
| 137 |
// @todo Use a function like book_menu_subtree_data().
|
| 138 |
$tree = menu_tree_all_data('management', $admin_link, $admin_link['depth'] + 1);
|
| 139 |
// The tree will be a sub-tree with the admin link as a single root item.
|
| 140 |
// @todo It is wrong to assume it's the last.
|
| 141 |
$admin_link = array_pop($tree);
|
| 142 |
$tree = $admin_link['below'] ? $admin_link['below'] : array();
|
| 143 |
}
|
| 144 |
return $tree;
|
| 145 |
}
|
| 146 |
|
| 147 |
/**
|
| 148 |
* Generate a links array from a menu tree array.
|
| 149 |
*
|
| 150 |
* Based on menu_navigation_links(). Adds in path based IDs, icon placeholders
|
| 151 |
* and overlay classes for the links.
|
| 152 |
*/
|
| 153 |
function toolbar_menu_navigation_links($tree) {
|
| 154 |
$links = array();
|
| 155 |
foreach ($tree as $item) {
|
| 156 |
if (!$item['link']['hidden'] && $item['link']['access']) {
|
| 157 |
$class = '';
|
| 158 |
// Make sure we have a path specific ID in place, so we can attach icons
|
| 159 |
// and behaviors to the items.
|
| 160 |
$id = str_replace(array('/', '<', '>'), array('-', '', ''), $item['link']['href']);
|
| 161 |
|
| 162 |
$link = $item['link']['localized_options'];
|
| 163 |
$link['href'] = $item['link']['href'];
|
| 164 |
// Add icon placeholder.
|
| 165 |
$link['title'] = '<span class="icon"></span>' . $item['link']['title'];
|
| 166 |
// Add admin link ID and to-overlay class for the overlay.
|
| 167 |
$link['attributes'] = array('id' => 'toolbar-link-' . $id, 'class' => array('to-overlay'));
|
| 168 |
$link['html'] = TRUE;
|
| 169 |
|
| 170 |
$class = ' path-' . $id;
|
| 171 |
if (toolbar_in_active_trail($item['link']['href'])) {
|
| 172 |
$class .= ' active-trail';
|
| 173 |
}
|
| 174 |
$links['menu-' . $item['link']['mlid'] . $class] = $link;
|
| 175 |
}
|
| 176 |
}
|
| 177 |
return $links;
|
| 178 |
}
|
| 179 |
|
| 180 |
/**
|
| 181 |
* Checks whether an item is in the active trail.
|
| 182 |
*
|
| 183 |
* Useful when using a menu generated by menu_tree_all_data() which does
|
| 184 |
* not set the 'in_active_trail' flag on items.
|
| 185 |
*
|
| 186 |
* @todo
|
| 187 |
* Look at migrating to a menu system level function.
|
| 188 |
*/
|
| 189 |
function toolbar_in_active_trail($path) {
|
| 190 |
$active_paths = &drupal_static(__FUNCTION__);
|
| 191 |
|
| 192 |
// Gather active paths.
|
| 193 |
if (!isset($active_paths)) {
|
| 194 |
$active_paths = array();
|
| 195 |
$trail = menu_get_active_trail();
|
| 196 |
foreach ($trail as $item) {
|
| 197 |
if (!empty($item['href'])) {
|
| 198 |
$active_paths[] = $item['href'];
|
| 199 |
}
|
| 200 |
}
|
| 201 |
}
|
| 202 |
return in_array($path, $active_paths);
|
| 203 |
}
|