summaryrefslogtreecommitdiff
path: root/modules/toolbar/toolbar.module
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2009-07-04 05:37:30 +0000
committerDries Buytaert <dries@buytaert.net>2009-07-04 05:37:30 +0000
commit7f1606632dd96aff2aea73c95faf50cd35e454a4 (patch)
treefec81a59bbaa36870bb160156e65f45623cf3848 /modules/toolbar/toolbar.module
parentb9f4e59c5a4655595e3254560febbdb01d599fd1 (diff)
downloadbrdo-7f1606632dd96aff2aea73c95faf50cd35e454a4.tar.gz
brdo-7f1606632dd96aff2aea73c95faf50cd35e454a4.tar.bz2
- Patch #484820 by Gábor Hojtsy, yhahn, markboulton, leisareichelt, et al: initial version of the new administration toolbar.
Diffstat (limited to 'modules/toolbar/toolbar.module')
-rw-r--r--modules/toolbar/toolbar.module186
1 files changed, 186 insertions, 0 deletions
diff --git a/modules/toolbar/toolbar.module b/modules/toolbar/toolbar.module
new file mode 100644
index 000000000..cf38e738b
--- /dev/null
+++ b/modules/toolbar/toolbar.module
@@ -0,0 +1,186 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Administration toolbar for quick access to top level administration items.
+ */
+
+/**
+ * Implementation of hook_perm().
+ */
+function toolbar_perm() {
+ 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';
+ }
+}
+
+/**
+ * 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);
+}