drupal/modules/toolbar/toolbar.module

187 lines
5.0 KiB
Plaintext

<?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.'),
),
);
}
/**
* Implementation of hook_theme().
*/
function toolbar_theme($existing, $type, $theme, $path) {
$items['toolbar'] = array(
'arguments' => array('toolbar' => array()),
'template' => 'toolbar',
'path' => drupal_get_path('module', 'toolbar'),
);
return $items;
}
/**
* Implementation of hook_page_alter().
*
* Add admin toolbar to the page_top region automatically.
*/
function toolbar_page_alter(&$page) {
if (user_access('access toolbar')) {
$page['page_top']['toolbar'] = toolbar_build();
}
}
/**
* Implementation of hook_preprocess_page().
*
* Add some page classes, so global page theming can adjust to the toolbar.
*/
function toolbar_preprocess_page(&$vars) {
if (user_access('access toolbar')) {
$vars['classes_array'][] = 'toolbar toolbar-shortcuts';
}
}
/**
* 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_js' => array(
$module_path . '/toolbar.js',
array('data' => 'misc/jquery.cookie.js', 'weight' => JS_LIBRARY + 2),
),
'#attached_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
$build['toolbar_user'] = array(
'#theme' => 'links',
'#links' => array(
'account' => array(
'title' => t('Hello <strong>@username</strong>', array('@username' => $user->name)),
'href' => 'user',
'html' => TRUE,
),
'logout' => array(
'title' => t('Logout'),
'href' => 'user/logout',
),
),
'#attributes' => array('id' => 'toolbar-user'),
);
// Add convenience shortcut links.
$shortcuts = menu_tree_all_data('admin_shortcuts');
$shortcuts = toolbar_menu_navigation_links($shortcuts);
$build['toolbar_shortcuts'] = array(
'#theme' => 'links',
'#links' => $shortcuts,
'#attributes' => array('id' => 'toolbar-shortcuts'),
);
return $build;
}
/**
* Get only the top level items below the 'admin' path.
*/
function toolbar_get_menu_tree() {
$tree = menu_tree_all_data('management');
foreach ($tree as $item) {
if ($item['link']['link_path'] == 'admin' && !empty($item['below'])) {
// Only take items right below the 'admin' path. All other management
// items are discarded.
$tree = $item['below'];
break;
}
}
foreach ($tree as $key => $item) {
// Get rid of subitems to have a leaner data structure.
unset($tree[$key]['below']);
}
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' => '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);
}