toolbar.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. $module_path . '/toolbar-print.css' => array(
  186. 'media' => 'print',
  187. ),
  188. ),
  189. 'library' => array(array('system', 'jquery.cookie')),
  190. ),
  191. );
  192. // Retrieve the admin menu from the database.
  193. $links = toolbar_menu_navigation_links(toolbar_get_menu_tree());
  194. $build['toolbar_menu'] = array(
  195. '#theme' => 'links__toolbar_menu',
  196. '#links' => $links,
  197. '#attributes' => array('id' => 'toolbar-menu'),
  198. '#heading' => array('text' => t('Administrative toolbar'), 'level' => 'h2', 'class' => 'element-invisible'),
  199. );
  200. // Add logout & user account links or login link.
  201. if ($user->uid) {
  202. $links = array(
  203. 'account' => array(
  204. 'title' => t('Hello <strong>@username</strong>', array('@username' => format_username($user))),
  205. 'href' => 'user',
  206. 'html' => TRUE,
  207. 'attributes' => array('title' => t('User account')),
  208. ),
  209. 'logout' => array(
  210. 'title' => t('Log out'),
  211. 'href' => 'user/logout',
  212. ),
  213. );
  214. }
  215. else {
  216. $links = array(
  217. 'login' => array(
  218. 'title' => t('Log in'),
  219. 'href' => 'user',
  220. ),
  221. );
  222. }
  223. $build['toolbar_user'] = array(
  224. '#theme' => 'links__toolbar_user',
  225. '#links' => $links,
  226. '#attributes' => array('id' => 'toolbar-user'),
  227. );
  228. // Add a "home" link.
  229. $link = array(
  230. 'home' => array(
  231. 'title' => '<span class="home-link">Home</span>',
  232. 'href' => '<front>',
  233. 'html' => TRUE,
  234. 'attributes' => array('title' => t('Home')),
  235. ),
  236. );
  237. $build['toolbar_home'] = array(
  238. '#theme' => 'links',
  239. '#links' => $link,
  240. '#attributes' => array('id' => 'toolbar-home'),
  241. );
  242. // Add an anchor to be able to toggle the visibility of the drawer.
  243. $build['toolbar_toggle'] = array(
  244. '#theme' => 'toolbar_toggle',
  245. '#collapsed' => _toolbar_is_collapsed(),
  246. '#attributes' => array('class' => array('toggle')),
  247. );
  248. // Prepare the drawer links CSS classes.
  249. $toolbar_drawer_classes = array(
  250. 'toolbar-drawer',
  251. 'clearfix',
  252. );
  253. if(_toolbar_is_collapsed()) {
  254. $toolbar_drawer_classes[] = 'collapsed';
  255. }
  256. $build['toolbar_drawer_classes'] = implode(' ', $toolbar_drawer_classes);
  257. return $build;
  258. }
  259. /**
  260. * Gets only the top level items below the 'admin' path.
  261. *
  262. * @return
  263. * An array containing a menu tree of top level items below the 'admin' path.
  264. */
  265. function toolbar_get_menu_tree() {
  266. $tree = array();
  267. $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();
  268. if ($admin_link) {
  269. $tree = menu_build_tree('management', array(
  270. 'expanded' => array($admin_link['mlid']),
  271. 'min_depth' => $admin_link['depth'] + 1,
  272. 'max_depth' => $admin_link['depth'] + 1,
  273. ));
  274. }
  275. return $tree;
  276. }
  277. /**
  278. * Generates a links array from a menu tree array.
  279. *
  280. * Based on menu_navigation_links(). Adds path based IDs and icon placeholders
  281. * to the links.
  282. *
  283. * @return
  284. * An array of links as defined above.
  285. */
  286. function toolbar_menu_navigation_links($tree) {
  287. $links = array();
  288. foreach ($tree as $item) {
  289. if (!$item['link']['hidden'] && $item['link']['access']) {
  290. // Make sure we have a path specific ID in place, so we can attach icons
  291. // and behaviors to the items.
  292. $id = str_replace(array('/', '<', '>'), array('-', '', ''), $item['link']['href']);
  293. $link = $item['link']['localized_options'];
  294. $link['href'] = $item['link']['href'];
  295. // Add icon placeholder.
  296. $link['title'] = '<span class="icon"></span>' . check_plain($item['link']['title']);
  297. // Add admin link ID.
  298. $link['attributes'] = array('id' => 'toolbar-link-' . $id);
  299. if (!empty($item['link']['description'])) {
  300. $link['title'] .= ' <span class="element-invisible">(' . $item['link']['description'] . ')</span>';
  301. $link['attributes']['title'] = $item['link']['description'];
  302. }
  303. $link['html'] = TRUE;
  304. $class = ' path-' . $id;
  305. if (toolbar_in_active_trail($item['link']['href'])) {
  306. $class .= ' active-trail';
  307. }
  308. $links['menu-' . $item['link']['mlid'] . $class] = $link;
  309. }
  310. }
  311. return $links;
  312. }
  313. /**
  314. * Checks whether an item is in the active trail.
  315. *
  316. * Useful when using a menu generated by menu_tree_all_data() which does
  317. * not set the 'in_active_trail' flag on items.
  318. *
  319. * @return
  320. * TRUE when path is in the active trail, FALSE if not.
  321. *
  322. * @todo
  323. * Look at migrating to a menu system level function.
  324. */
  325. function toolbar_in_active_trail($path) {
  326. $active_paths = &drupal_static(__FUNCTION__);
  327. // Gather active paths.
  328. if (!isset($active_paths)) {
  329. $active_paths = array();
  330. $trail = menu_get_active_trail();
  331. foreach ($trail as $item) {
  332. if (!empty($item['href'])) {
  333. $active_paths[] = $item['href'];
  334. }
  335. }
  336. }
  337. return in_array($path, $active_paths);
  338. }