toolbar.module 11 KB

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