adminimal_admin_menu.module 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * @file
  4. * Themes Administration menu like Adminimal theme.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function adminimal_admin_menu_menu() {
  10. $items = array();
  11. $items['admin/config/administration/adminimal_menu'] = array(
  12. 'title' => 'Adminimal menu',
  13. 'description' => 'Adjust adminimal menu settings.',
  14. 'page callback' => 'drupal_get_form',
  15. 'page arguments' => array('adminimal_admin_menu_settings'),
  16. 'access arguments' => array('administer site configuration'),
  17. 'file' => 'adminimal_admin_menu.admin.inc',
  18. );
  19. return $items;
  20. }
  21. /**
  22. * Implements hook_module_implements_alter().
  23. */
  24. function adminimal_admin_menu_module_implements_alter(&$implementations, $hook) {
  25. // Put Adminimal Admin Menu's implementation of hook_admin_menu_output_alter()
  26. // to the end of the list of implementors, assuring that it will be executed
  27. // at the very end. Other modules may manipulate the menu items in the
  28. // administration menu (i.e. the excellent Administration Menu Source) and we
  29. // need to make sure those changes are preserved and respected by the output
  30. // of Adminimal Admin Menu.
  31. if ($hook == 'admin_menu_output_alter') {
  32. $group = $implementations['adminimal_admin_menu'];
  33. unset($implementations['adminimal_admin_menu']);
  34. $implementations['adminimal_admin_menu'] = $group;
  35. }
  36. }
  37. /**
  38. * Implements hook_page_build().
  39. */
  40. function adminimal_admin_menu_page_build(&$page) {
  41. if (!_adminimal_admin_menu_access()) {
  42. return;
  43. }
  44. $path = drupal_get_path('module', 'adminimal_admin_menu');
  45. $load_slicknav = variable_get('adminimal_admin_menu_slicknav', TRUE);
  46. $load_jquery = variable_get('adminimal_admin_menu_jquery', TRUE);
  47. // Attach the CSS and JavaScript assets.
  48. drupal_add_css($path . '/adminimal_admin_menu.css');
  49. _adminimal_admin_menu_support_environment_indicator();
  50. // Check if both slicknav and custom jQuery must be loaded.
  51. if ($load_slicknav and $load_jquery) {
  52. drupal_add_js($path . '/js/jquery.min.js', array(
  53. 'type' => 'file',
  54. 'scope' => 'header',
  55. 'weight' => 888,
  56. ));
  57. drupal_add_js($path . '/js/slicknav/jquery-no-conflict.slicknav.js', array(
  58. 'type' => 'file',
  59. 'scope' => 'header',
  60. 'weight' => 888,
  61. ));
  62. drupal_add_css($path . '/js/slicknav/slicknav.css');
  63. }
  64. elseif ($load_slicknav and !$load_jquery) {
  65. drupal_add_js($path . '/js/slicknav/jquery.slicknav.js', array(
  66. 'type' => 'file',
  67. 'scope' => 'header',
  68. 'weight' => 888,
  69. ));
  70. drupal_add_css($path . '/js/slicknav/slicknav.css');
  71. }
  72. drupal_add_js($path . '/js/adminimal_admin_menu.js', array(
  73. 'type' => 'file',
  74. 'scope' => 'header',
  75. 'weight' => 888,
  76. ));
  77. if (!isset($page['page_bottom']['admin_menu'])) {
  78. return;
  79. }
  80. $attached = &$page['page_bottom']['admin_menu']['#attached'];
  81. $options = array('every_page' => TRUE);
  82. // @todo Stop-gap fix until cached rendering is resolved (http://drupal.org/node/1567622).
  83. if (module_exists('shortcut')) {
  84. $attached['css'][drupal_get_path('module', 'shortcut') . '/shortcut.css'] = $options;
  85. }
  86. $settings = array();
  87. // Add current path to support menu item highlighting.
  88. // @todo Compile real active trail here?
  89. $args = explode('/', $_GET['q']);
  90. if ($args[0] == 'admin' && !empty($args[1])) {
  91. $settings['activeTrail'] = url($args[0] . '/' . $args[1]);
  92. }
  93. elseif (drupal_is_front_page()) {
  94. $settings['activeTrail'] = url('<front>');
  95. }
  96. $attached['js'][] = array(
  97. 'data' => array('admin_menu' => array('toolbar' => $settings)),
  98. 'type' => 'setting',
  99. );
  100. }
  101. /**
  102. * Add CSS for module environment_indicator.
  103. */
  104. function _adminimal_admin_menu_support_environment_indicator() {
  105. if (!module_exists('environment_indicator')) {
  106. return;
  107. }
  108. $environment_info = environment_indicator_get_active();
  109. if (!environment_indicator_check_access($environment_info)) {
  110. return;
  111. }
  112. drupal_add_css('
  113. div.slicknav_menu a.slicknav_btn:after {
  114. content: "' . $environment_info['name'] . '";
  115. margin-left: 1em;
  116. color: ' . $environment_info['text_color'] . ';
  117. font-size: 12px;
  118. background-color: ' . $environment_info['color'] . ';
  119. padding: 2px 5px;
  120. }
  121. #admin-menu-wrapper {background: #222 !important;}
  122. ',
  123. array(
  124. 'group' => CSS_DEFAULT,
  125. 'type' => 'inline',
  126. 'preprocess' => FALSE,
  127. 'weight' => '100',
  128. ));
  129. }
  130. /**
  131. * Implements hook_admin_menu_output_build().
  132. */
  133. function adminimal_admin_menu_admin_menu_output_build(&$content) {
  134. // Shortcut menu.
  135. if (module_exists('shortcut') && variable_get('adminimal_admin_menu_render', 'collapsed') != 'hidden') {
  136. // Add shortcuts bar.
  137. $content['shortcut'] = array(
  138. '#access' => module_exists('shortcut'),
  139. '#weight' => 200,
  140. '#prefix' => '<div class="shortcut-toolbar">',
  141. '#suffix' => '</div>',
  142. );
  143. $content['shortcut']['shortcuts'] = array(
  144. // Shortcut module's CSS relies on Toolbar module's markup.
  145. // @see http://drupal.org/node/1217038
  146. '#prefix' => '<div id="toolbar">',
  147. '#suffix' => '</div>',
  148. // @todo Links may contain .active-trail classes.
  149. '#pre_render' => array('shortcut_toolbar_pre_render'),
  150. );
  151. }
  152. }
  153. /**
  154. * Implements hook_admin_menu_output_alter().
  155. */
  156. function adminimal_admin_menu_admin_menu_output_alter(&$content) {
  157. // Add a class to top-level items for styling.
  158. foreach (element_children($content['menu']) as $link) {
  159. $content['menu'][$link]['#attributes']['class'][] = 'admin-menu-toolbar-category';
  160. }
  161. // Alter icon.
  162. unset($content['icon']['icon']['#theme']);
  163. $content['icon']['icon']['#title'] = '<span class="admin-menu-home-icon">&nbsp;</span>';
  164. $content['icon']['icon']['#attributes']['class'][] = 'admin-menu-toolbar-home-menu';
  165. // @todo Fix this CS-issue.
  166. $page['#attributes']['class'][] = 'adminimal-menu';
  167. // Hide the menu.
  168. if (module_exists('shortcut') && variable_get('adminimal_admin_menu_render', 'collapsed') == 'exclusive') {
  169. unset($content['icon']['icon']);
  170. unset($content['search']);
  171. foreach ($content['menu'] as $key => $link) {
  172. // Move local tasks on 'admin' into icon menu.
  173. unset($content['menu'][$key]);
  174. }
  175. }
  176. // Create the responsive menu.
  177. if (variable_get('adminimal_admin_menu_slicknav', TRUE)) {
  178. // Join the Icon menu with the administration menu.
  179. $responsivemenu = array_merge($content['icon'], $content['menu']);
  180. // Give it ID to target it later with js and css.
  181. $responsivemenu['#wrapper_attributes']['id'] = 'admin-menu-menu-responsive';
  182. // Move the icon menu to the top.
  183. $responsivemenu['icon']['#weight'] = '-100';
  184. // Change the link title for 'admin/index' to Administration if it exists.
  185. if (!empty($responsivemenu['admin/index'])) {
  186. $responsivemenu['admin/index']['#title'] = t('Administration');
  187. }
  188. // Add the account menu, and push account and logout links toward bottom.
  189. $responsivemenu = array_merge($content['account'], $responsivemenu);
  190. $responsivemenu['account']['#weight'] = 99;
  191. $responsivemenu['logout']['#weight'] = 100;
  192. // Bind the responsive menu the the content varable so it can be rendered.
  193. $content['responsive-menu'] = $responsivemenu;
  194. // Create the responsive shortucts.
  195. if (module_exists('shortcut')) {
  196. $content['responsive']['shortcuts'] = array(
  197. '#prefix' => '<div id="admin-menu-shortcuts-responsive">',
  198. '#suffix' => '</div>',
  199. '#pre_render' => array('shortcut_toolbar_pre_render'),
  200. );
  201. }
  202. }
  203. }
  204. /**
  205. * Implements hook_preprocess_html().
  206. */
  207. function adminimal_admin_menu_preprocess_html(&$vars) {
  208. if (!_adminimal_admin_menu_access()) {
  209. return;
  210. }
  211. // Add the "adminimal" class to the body for better css selection.
  212. $vars['classes_array'][] = 'adminimal-menu';
  213. // Add frontend and backend classes to the body.
  214. if (path_is_admin(current_path())) {
  215. $vars['classes_array'][] = 'adminimal-backend';
  216. }
  217. else {
  218. $vars['classes_array'][] = 'adminimal-frontend';
  219. }
  220. // Add the shortcut render mode class.
  221. if (module_exists('shortcut')) {
  222. $vars['classes_array'][] = 'menu-render-' . variable_get('adminimal_admin_menu_render', 'collapsed');
  223. }
  224. _adminimal_admin_menu_viewport();
  225. }
  226. /**
  227. * Add viewport for scaling on devices.
  228. */
  229. function _adminimal_admin_menu_viewport() {
  230. $use_slicknav = variable_get('adminimal_admin_menu_slicknav', TRUE);
  231. $use_viewport = variable_get('adminimal_admin_menu_viewport', TRUE);
  232. // Only needed for slicknav.
  233. if ($use_slicknav && $use_viewport) {
  234. $viewport = array(
  235. '#tag' => 'meta',
  236. '#attributes' => array(
  237. 'name' => 'viewport',
  238. 'content' => 'width=device-width, initial-scale=1',
  239. ),
  240. );
  241. drupal_add_html_head($viewport, 'viewport');
  242. }
  243. }
  244. /**
  245. * Check if the user has access to use the admin menu.
  246. *
  247. * @return bool
  248. * Result of access checks.
  249. */
  250. function _adminimal_admin_menu_access() {
  251. if (!user_access('access administration menu') || admin_menu_suppress(FALSE)) {
  252. return FALSE;
  253. }
  254. // Performance: Skip this entirely for AJAX requests.
  255. if (strpos($_GET['q'], 'js/') === 0) {
  256. return FALSE;
  257. }
  258. return TRUE;
  259. }