toolbar.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /**
  3. * @file
  4. * Administration toolbar for quick access to top level administration items.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function toolbar_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#toolbar':
  12. $output = '<h3>' . t('About') . '</h3>';
  13. $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>';
  14. $output .= '<h3>' . t('Uses') . '</h3>';
  15. $output .= '<dl>';
  16. $output .= '<dt>' . t('Displaying administrative links') . '</dt>';
  17. $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>';
  18. $output .= '</dl>';
  19. return $output;
  20. }
  21. }
  22. /**
  23. * Implements hook_permission().
  24. */
  25. function toolbar_permission() {
  26. return array(
  27. 'access toolbar' => array(
  28. 'title' => t('Use the administration toolbar'),
  29. ),
  30. );
  31. }
  32. /**
  33. * Implements hook_theme().
  34. */
  35. function toolbar_theme($existing, $type, $theme, $path) {
  36. $items['toolbar'] = array(
  37. 'render element' => 'toolbar',
  38. 'template' => 'toolbar',
  39. 'path' => drupal_get_path('module', 'toolbar'),
  40. );
  41. $items['toolbar_toggle'] = array(
  42. 'variables' => array(
  43. 'collapsed' => NULL,
  44. 'attributes' => array(),
  45. ),
  46. );
  47. return $items;
  48. }
  49. /**
  50. * Implements hook_menu().
  51. */
  52. function toolbar_menu() {
  53. $items['toolbar/toggle'] = array(
  54. 'title' => 'Toggle drawer visibility',
  55. 'type' => MENU_CALLBACK,
  56. 'page callback' => 'toolbar_toggle_page',
  57. 'access arguments' => array('access toolbar'),
  58. );
  59. return $items;
  60. }
  61. /**
  62. * Menu callback; toggles the visibility of the toolbar drawer.
  63. */
  64. function toolbar_toggle_page() {
  65. global $base_path;
  66. // Toggle the value in the cookie.
  67. setcookie('Drupal.toolbar.collapsed', !_toolbar_is_collapsed(), NULL, $base_path);
  68. // Redirect the user from where he used the toggle element.
  69. drupal_goto();
  70. }
  71. /**
  72. * Formats an element used to toggle the toolbar drawer's visibility.
  73. *
  74. * @param $variables
  75. * An associative array containing:
  76. * - collapsed: A boolean value representing the toolbar drawer's visibility.
  77. * - attributes: An associative array of HTML attributes.
  78. *
  79. * @return
  80. * An HTML string representing the element for toggling.
  81. *
  82. * @ingroup themable
  83. */
  84. function theme_toolbar_toggle($variables) {
  85. if ($variables['collapsed']) {
  86. $toggle_text = t('Show shortcuts');
  87. }
  88. else {
  89. $toggle_text = t('Hide shortcuts');
  90. $variables['attributes']['class'][] = 'toggle-active';
  91. }
  92. return l($toggle_text, 'toolbar/toggle', array('query' => drupal_get_destination(), 'attributes' => array('title' => $toggle_text) + $variables['attributes']));
  93. }
  94. /**
  95. * Determines the current state of the toolbar drawer's visibility.
  96. *
  97. * @return
  98. * TRUE when drawer is collapsed, FALSE when it is expanded.
  99. */
  100. function _toolbar_is_collapsed() {
  101. // PHP converts dots into underscores in cookie names to avoid problems with
  102. // its parser, so we use a converted cookie name.
  103. return isset($_COOKIE['Drupal_toolbar_collapsed']) ? $_COOKIE['Drupal_toolbar_collapsed'] : 0;
  104. }
  105. /**
  106. * Implements hook_page_build().
  107. *
  108. * Add admin toolbar to the page_top region automatically.
  109. */
  110. function toolbar_page_build(&$page) {
  111. $page['page_top']['toolbar'] = array(
  112. '#pre_render' => array('toolbar_pre_render'),
  113. '#access' => user_access('access toolbar'),
  114. 'toolbar_drawer' => array(),
  115. );
  116. }
  117. /**
  118. * Prerender function for the toolbar.
  119. *
  120. * Since building the toolbar takes some time, it is done just prior to
  121. * rendering to ensure that it is built only if it will be displayed.
  122. */
  123. function toolbar_pre_render($toolbar) {
  124. $toolbar = array_merge($toolbar, toolbar_view());
  125. return $toolbar;
  126. }
  127. /**
  128. * Implements hook_preprocess_html().
  129. *
  130. * Add some page classes, so global page theming can adjust to the toolbar.
  131. */
  132. function toolbar_preprocess_html(&$vars) {
  133. if (isset($vars['page']['page_top']['toolbar']) && user_access('access toolbar')) {
  134. $vars['classes_array'][] = 'toolbar';
  135. if (!_toolbar_is_collapsed()) {
  136. $vars['classes_array'][] = 'toolbar-drawer';
  137. }
  138. }
  139. }
  140. /**
  141. * Implements hook_preprocess_toolbar().
  142. *
  143. * Adding the 'overlay-displace-top' class to the toolbar pushes the overlay
  144. * down, so it appears below the toolbar.
  145. */
  146. function toolbar_preprocess_toolbar(&$variables) {
  147. $variables['classes_array'][] = "overlay-displace-top";
  148. }
  149. /**
  150. * Implements hook_system_info_alter().
  151. *
  152. * Indicate that the 'page_top' region (in which the toolbar will be displayed)
  153. * is an overlay supplemental region that should be refreshed whenever its
  154. * content is updated.
  155. *
  156. * This information is provided for any module that might need to use it, not
  157. * just the core Overlay module.
  158. */
  159. function toolbar_system_info_alter(&$info, $file, $type) {
  160. if ($type == 'theme') {
  161. $info['overlay_supplemental_regions'][] = 'page_top';
  162. }
  163. }
  164. /**
  165. * Builds the admin menu as a structured array ready for drupal_render().
  166. *
  167. * @return
  168. * Array of links and settings relating to the admin menu.
  169. */
  170. function toolbar_view() {
  171. global $user;
  172. $module_path = drupal_get_path('module', 'toolbar');
  173. $build = array(
  174. '#theme' => 'toolbar',
  175. '#attached'=> array(
  176. 'js' => array(
  177. $module_path . '/toolbar.js',
  178. array(
  179. 'data' => array('tableHeaderOffset' => 'Drupal.toolbar.height'),
  180. 'type' => 'setting'
  181. ),
  182. ),
  183. 'css' => array(
  184. $module_path . '/toolbar.css',
  185. ),
  186. 'library' => array(array('system', 'jquery.cookie')),
  187. ),
  188. );
  189. // Retrieve the admin menu from the database.
  190. $links = toolbar_menu_navigation_links(toolbar_get_menu_tree());
  191. $build['toolbar_menu'] = array(
  192. '#theme' => 'links__toolbar_menu',
  193. '#links' => $links,
  194. '#attributes' => array('id' => 'toolbar-menu'),
  195. '#heading' => array('text' => t('Administrative toolbar'), 'level' => 'h2', 'class' => 'element-invisible'),
  196. );
  197. // Add logout & user account links or login link.
  198. if ($user->uid) {
  199. $links = array(
  200. 'account' => array(
  201. 'title' => t('Hello <strong>@username</strong>', array('@username' => format_username($user))),
  202. 'href' => 'user',
  203. 'html' => TRUE,
  204. 'attributes' => array('title' => t('User account')),
  205. ),
  206. 'logout' => array(
  207. 'title' => t('Log out'),
  208. 'href' => 'user/logout',
  209. ),
  210. );
  211. }
  212. else {
  213. $links = array(
  214. 'login' => array(
  215. 'title' => t('Log in'),
  216. 'href' => 'user',
  217. ),
  218. );
  219. }
  220. $build['toolbar_user'] = array(
  221. '#theme' => 'links__toolbar_user',
  222. '#links' => $links,
  223. '#attributes' => array('id' => 'toolbar-user'),
  224. );
  225. // Add a "home" link.
  226. $link = array(
  227. 'home' => array(
  228. 'title' => '<span class="home-link">Home</span>',
  229. 'href' => '<front>',
  230. 'html' => TRUE,
  231. 'attributes' => array('title' => t('Home')),
  232. ),
  233. );
  234. $build['toolbar_home'] = array(
  235. '#theme' => 'links',
  236. '#links' => $link,
  237. '#attributes' => array('id' => 'toolbar-home'),
  238. );
  239. // Add an anchor to be able to toggle the visibility of the drawer.
  240. $build['toolbar_toggle'] = array(
  241. '#theme' => 'toolbar_toggle',
  242. '#collapsed' => _toolbar_is_collapsed(),
  243. '#attributes' => array('class' => array('toggle')),
  244. );
  245. // Prepare the drawer links CSS classes.
  246. $toolbar_drawer_classes = array(
  247. 'toolbar-drawer',
  248. 'clearfix',
  249. );
  250. if(_toolbar_is_collapsed()) {
  251. $toolbar_drawer_classes[] = 'collapsed';
  252. }
  253. $build['toolbar_drawer_classes'] = implode(' ', $toolbar_drawer_classes);
  254. return $build;
  255. }
  256. /**
  257. * Gets only the top level items below the 'admin' path.
  258. *
  259. * @return
  260. * An array containing a menu tree of top level items below the 'admin' path.
  261. */
  262. function toolbar_get_menu_tree() {
  263. $tree = array();
  264. $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();
  265. if ($admin_link) {
  266. $tree = menu_build_tree('management', array(
  267. 'expanded' => array($admin_link['mlid']),
  268. 'min_depth' => $admin_link['depth'] + 1,
  269. 'max_depth' => $admin_link['depth'] + 1,
  270. ));
  271. }
  272. return $tree;
  273. }
  274. /**
  275. * Generates a links array from a menu tree array.
  276. *
  277. * Based on menu_navigation_links(). Adds path based IDs and icon placeholders
  278. * to the links.
  279. *
  280. * @return
  281. * An array of links as defined above.
  282. */
  283. function toolbar_menu_navigation_links($tree) {
  284. $links = array();
  285. foreach ($tree as $item) {
  286. if (!$item['link']['hidden'] && $item['link']['access']) {
  287. // Make sure we have a path specific ID in place, so we can attach icons
  288. // and behaviors to the items.
  289. $id = str_replace(array('/', '<', '>'), array('-', '', ''), $item['link']['href']);
  290. $link = $item['link']['localized_options'];
  291. $link['href'] = $item['link']['href'];
  292. // Add icon placeholder.
  293. $link['title'] = '<span class="icon"></span>' . check_plain($item['link']['title']);
  294. // Add admin link ID.
  295. $link['attributes'] = array('id' => 'toolbar-link-' . $id);
  296. if (!empty($item['link']['description'])) {
  297. $link['title'] .= ' <span class="element-invisible">(' . $item['link']['description'] . ')</span>';
  298. $link['attributes']['title'] = $item['link']['description'];
  299. }
  300. $link['html'] = TRUE;
  301. $class = ' path-' . $id;
  302. if (toolbar_in_active_trail($item['link']['href'])) {
  303. $class .= ' active-trail';
  304. }
  305. $links['menu-' . $item['link']['mlid'] . $class] = $link;
  306. }
  307. }
  308. return $links;
  309. }
  310. /**
  311. * Checks whether an item is in the active trail.
  312. *
  313. * Useful when using a menu generated by menu_tree_all_data() which does
  314. * not set the 'in_active_trail' flag on items.
  315. *
  316. * @return
  317. * TRUE when path is in the active trail, FALSE if not.
  318. *
  319. * @todo
  320. * Look at migrating to a menu system level function.
  321. */
  322. function toolbar_in_active_trail($path) {
  323. $active_paths = &drupal_static(__FUNCTION__);
  324. // Gather active paths.
  325. if (!isset($active_paths)) {
  326. $active_paths = array();
  327. $trail = menu_get_active_trail();
  328. foreach ($trail as $item) {
  329. if (!empty($item['href'])) {
  330. $active_paths[] = $item['href'];
  331. }
  332. }
  333. }
  334. return in_array($path, $active_paths);
  335. }