2009-07-04 05:37:30 +00:00
<?php
/**
* @file
* Administration toolbar for quick access to top level administration items.
*/
2009-12-04 16:02:42 +00:00
/**
* 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>';
2010-12-01 07:05:26 +00:00
$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>';
2009-12-04 16:02:42 +00:00
$output .= '</dl>';
return $output;
}
}
2009-07-04 05:37:30 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_permission().
2009-07-04 05:37:30 +00:00
*/
2009-07-05 18:00:11 +00:00
function toolbar_permission() {
2009-07-04 05:37:30 +00:00
return array(
'access toolbar' => array(
2009-12-01 13:14:43 +00:00
'title' => t('Use the administration toolbar'),
2009-07-04 05:37:30 +00:00
),
);
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_theme().
2009-07-04 05:37:30 +00:00
*/
function toolbar_theme($existing, $type, $theme, $path) {
$items['toolbar'] = array(
2009-10-23 22:24:19 +00:00
'render element' => 'toolbar',
2009-07-04 05:37:30 +00:00
'template' => 'toolbar',
'path' => drupal_get_path('module', 'toolbar'),
);
2009-11-15 21:13:26 +00:00
$items['toolbar_toggle'] = array(
'variables' => array(
'collapsed' => NULL,
'attributes' => array(),
),
);
return $items;
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_menu().
2009-11-15 21:13:26 +00:00
*/
function toolbar_menu() {
$items['toolbar/toggle'] = array(
'title' => 'Toggle drawer visibility',
'type' => MENU_CALLBACK,
'page callback' => 'toolbar_toggle_page',
'access arguments' => array('access toolbar'),
);
2009-07-04 05:37:30 +00:00
return $items;
}
2009-11-15 21:13:26 +00:00
/**
2011-12-19 10:59:44 +00:00
* Page callback: Toggles the visibility of the toolbar drawer.
*
* Path: toolbar/toggle
*
* @see toolbar_menu().
2009-11-15 21:13:26 +00:00
*/
function toolbar_toggle_page() {
global $base_path;
// Toggle the value in the cookie.
2010-01-29 14:26:26 +00:00
setcookie('Drupal.toolbar.collapsed', !_toolbar_is_collapsed(), NULL, $base_path);
2009-11-15 21:13:26 +00:00
// Redirect the user from where he used the toggle element.
drupal_goto();
}
/**
* Formats an element used to toggle the toolbar drawer's visibility.
2010-01-30 07:59:26 +00:00
*
2009-11-15 21:13:26 +00:00
* @param $variables
* An associative array containing:
* - collapsed: A boolean value representing the toolbar drawer's visibility.
* - attributes: An associative array of HTML attributes.
2011-12-19 10:59:44 +00:00
*
2009-11-15 21:13:26 +00:00
* @return
* An HTML string representing the element for toggling.
*
* @ingroup themable
*/
function theme_toolbar_toggle($variables) {
if ($variables['collapsed']) {
2010-12-01 07:05:26 +00:00
$toggle_text = t('Show shortcuts');
2009-11-15 21:13:26 +00:00
}
else {
2010-12-01 07:05:26 +00:00
$toggle_text = t('Hide shortcuts');
2009-11-15 21:13:26 +00:00
$variables['attributes']['class'][] = 'toggle-active';
}
2011-04-12 20:30:26 +00:00
return l($toggle_text, 'toolbar/toggle', array('query' => drupal_get_destination(), 'attributes' => array('title' => $toggle_text) + $variables['attributes']));
2009-11-15 21:13:26 +00:00
}
/**
* Determines the current state of the toolbar drawer's visibility.
2010-01-30 07:59:26 +00:00
*
2009-11-15 21:13:26 +00:00
* @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.
2010-04-04 17:11:20 +00:00
return isset($_COOKIE['Drupal_toolbar_collapsed']) ? $_COOKIE['Drupal_toolbar_collapsed'] : 0;
2009-11-15 21:13:26 +00:00
}
2009-07-04 05:37:30 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_page_build().
2009-11-10 17:27:54 +00:00
*
2009-07-04 05:37:30 +00:00
* Add admin toolbar to the page_top region automatically.
*/
2009-08-31 16:46:32 +00:00
function toolbar_page_build(&$page) {
2009-10-26 04:45:10 +00:00
$page['page_top']['toolbar'] = array(
'#pre_render' => array('toolbar_pre_render'),
'#access' => user_access('access toolbar'),
2009-11-19 03:57:15 +00:00
'toolbar_drawer' => array(),
2009-10-26 04:45:10 +00:00
);
}
/**
2011-12-19 10:59:44 +00:00
* Provides pre_render function for the toolbar.
2009-10-26 04:45:10 +00:00
*
* 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.
2011-12-19 10:59:44 +00:00
*
* @see toolbar_page_build().
2009-10-26 04:45:10 +00:00
*/
function toolbar_pre_render($toolbar) {
2009-12-21 13:47:32 +00:00
$toolbar = array_merge($toolbar, toolbar_view());
2009-10-26 04:45:10 +00:00
return $toolbar;
2009-07-04 05:37:30 +00:00
}
/**
2009-12-04 16:49:48 +00:00
* Implements hook_preprocess_html().
2009-07-04 05:37:30 +00:00
*
* Add some page classes, so global page theming can adjust to the toolbar.
*/
2009-09-15 17:10:39 +00:00
function toolbar_preprocess_html(&$vars) {
2010-04-04 17:11:20 +00:00
if (isset($vars['page']['page_top']['toolbar']) && user_access('access toolbar')) {
2009-11-15 21:13:26 +00:00
$vars['classes_array'][] = 'toolbar';
if (!_toolbar_is_collapsed()) {
$vars['classes_array'][] = 'toolbar-drawer';
}
2009-07-04 05:37:30 +00:00
}
}
2010-06-08 05:16:29 +00:00
/**
* 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";
}
#610234 by Gábor Hojtsy, ksenzee, cwgordon7, David_Rothstein, seutje, marcvangend, sun, JoshuaRogers, markus_petrux, Bojhan, Rob Loach, Everett Zufelt, drifter, markboulton, leisareichelt, et al: Added Overlay module to core, which shows administrative pages in a JS overlay, retaining context on the front-end site.
2009-12-02 07:28:22 +00:00
/**
* Implements hook_system_info_alter().
*
2010-04-29 05:19:50 +00:00
* 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.
#610234 by Gábor Hojtsy, ksenzee, cwgordon7, David_Rothstein, seutje, marcvangend, sun, JoshuaRogers, markus_petrux, Bojhan, Rob Loach, Everett Zufelt, drifter, markboulton, leisareichelt, et al: Added Overlay module to core, which shows administrative pages in a JS overlay, retaining context on the front-end site.
2009-12-02 07:28:22 +00:00
*/
function toolbar_system_info_alter(&$info, $file, $type) {
2010-04-29 05:19:50 +00:00
if ($type == 'theme') {
#610234 by Gábor Hojtsy, ksenzee, cwgordon7, David_Rothstein, seutje, marcvangend, sun, JoshuaRogers, markus_petrux, Bojhan, Rob Loach, Everett Zufelt, drifter, markboulton, leisareichelt, et al: Added Overlay module to core, which shows administrative pages in a JS overlay, retaining context on the front-end site.
2009-12-02 07:28:22 +00:00
$info['overlay_supplemental_regions'][] = 'page_top';
}
}
2009-07-04 05:37:30 +00:00
/**
2011-12-19 10:59:44 +00:00
* Builds the admin menu as a structured array ready for drupal_render().
*
* @return
* Array of links and settings relating to the admin menu.
2009-07-04 05:37:30 +00:00
*/
2009-12-21 13:47:32 +00:00
function toolbar_view() {
2009-07-04 05:37:30 +00:00
global $user;
$module_path = drupal_get_path('module', 'toolbar');
$build = array(
'#theme' => 'toolbar',
2009-09-05 15:05:05 +00:00
'#attached'=> array(
'js' => array(
2010-05-14 07:45:54 +00:00
$module_path . '/toolbar.js',
2010-05-23 18:23:32 +00:00
array(
'data' => array('tableHeaderOffset' => 'Drupal.toolbar.height'),
'type' => 'setting'
),
2009-09-05 15:05:05 +00:00
),
'css' => array(
$module_path . '/toolbar.css',
),
2010-11-30 17:16:37 +00:00
'library' => array(array('system', 'jquery.cookie')),
2009-07-04 05:37:30 +00:00
),
);
// Retrieve the admin menu from the database.
$links = toolbar_menu_navigation_links(toolbar_get_menu_tree());
$build['toolbar_menu'] = array(
2010-01-08 07:36:53 +00:00
'#theme' => 'links__toolbar_menu',
2009-07-04 05:37:30 +00:00
'#links' => $links,
'#attributes' => array('id' => 'toolbar-menu'),
2010-09-16 20:53:34 +00:00
'#heading' => array('text' => t('Administrative toolbar'), 'level' => 'h2', 'class' => 'element-invisible'),
2009-07-04 05:37:30 +00:00
);
2009-12-16 19:41:22 +00:00
// Add logout & user account links or login link.
2009-10-26 04:45:10 +00:00
if ($user->uid) {
$links = array(
2009-07-04 05:37:30 +00:00
'account' => array(
2009-11-01 21:26:44 +00:00
'title' => t('Hello <strong>@username</strong>', array('@username' => format_username($user))),
2009-07-04 05:37:30 +00:00
'href' => 'user',
'html' => TRUE,
2010-01-12 23:04:48 +00:00
'attributes' => array('title' => t('User account')),
2009-07-04 05:37:30 +00:00
),
'logout' => array(
2009-09-14 07:55:24 +00:00
'title' => t('Log out'),
2009-07-04 05:37:30 +00:00
'href' => 'user/logout',
),
2009-10-26 04:45:10 +00:00
);
}
else {
$links = array(
'login' => array(
'title' => t('Log in'),
'href' => 'user',
),
);
}
$build['toolbar_user'] = array(
2010-01-08 07:36:53 +00:00
'#theme' => 'links__toolbar_user',
2009-10-26 04:45:10 +00:00
'#links' => $links,
2009-07-04 05:37:30 +00:00
'#attributes' => array('id' => 'toolbar-user'),
);
2010-01-30 07:59:26 +00:00
2009-12-16 19:41:22 +00:00
// Add a "home" link.
$link = array(
'home' => array(
'title' => '<span class="home-link">Home</span>',
'href' => '<front>',
'html' => TRUE,
2010-01-12 23:04:48 +00:00
'attributes' => array('title' => t('Home')),
2009-12-16 19:41:22 +00:00
),
);
$build['toolbar_home'] = array(
'#theme' => 'links',
'#links' => $link,
'#attributes' => array('id' => 'toolbar-home'),
);
2009-11-15 21:13:26 +00:00
// 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',
);
2011-09-21 09:50:30 +00:00
if (_toolbar_is_collapsed()) {
2009-11-15 21:13:26 +00:00
$toolbar_drawer_classes[] = 'collapsed';
}
2011-09-21 09:50:30 +00:00
$build['toolbar_drawer']['#type'] = 'container';
$build['toolbar_drawer']['#attributes']['class'] = $toolbar_drawer_classes;
2009-11-15 21:13:26 +00:00
2009-07-04 05:37:30 +00:00
return $build;
}
/**
2011-12-19 10:59:44 +00:00
* Gets only the top level items below the 'admin' path.
*
* @return
* An array containing a menu tree of top level items below the 'admin' path.
2009-07-04 05:37:30 +00:00
*/
function toolbar_get_menu_tree() {
2009-08-14 15:30:59 +00:00
$tree = array();
2010-07-02 02:22:26 +00:00
$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();
2009-08-14 15:30:59 +00:00
if ($admin_link) {
2010-07-02 02:22:26 +00:00
$tree = menu_build_tree('management', array(
'expanded' => array($admin_link['mlid']),
'min_depth' => $admin_link['depth'] + 1,
'max_depth' => $admin_link['depth'] + 1,
));
2009-07-04 05:37:30 +00:00
}
2010-01-30 07:59:26 +00:00
2009-07-04 05:37:30 +00:00
return $tree;
}
/**
2011-12-19 10:59:44 +00:00
* Generates an array of links from a menu tree array.
2009-07-04 05:37:30 +00:00
*
2010-05-14 16:52:27 +00:00
* Based on menu_navigation_links(). Adds path based IDs and icon placeholders
* to the links.
2011-12-19 10:59:44 +00:00
*
* @return
* An array of links as defined above.
2009-07-04 05:37:30 +00:00
*/
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.
2010-06-13 05:47:47 +00:00
$link['title'] = '<span class="icon"></span>' . check_plain($item['link']['title']);
2010-05-14 16:52:27 +00:00
// Add admin link ID.
$link['attributes'] = array('id' => 'toolbar-link-' . $id);
2009-12-12 23:42:59 +00:00
if (!empty($item['link']['description'])) {
$link['title'] .= ' <span class="element-invisible">(' . $item['link']['description'] . ')</span>';
$link['attributes']['title'] = $item['link']['description'];
}
2009-07-04 05:37:30 +00:00
$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.
*
2009-11-10 17:27:54 +00:00
* Useful when using a menu generated by menu_tree_all_data() which does
2009-07-04 05:37:30 +00:00
* not set the 'in_active_trail' flag on items.
*
2011-12-19 10:59:44 +00:00
* @return
* TRUE when path is in the active trail, FALSE if not.
*
2009-07-04 05:37:30 +00:00
* @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);
}