adminimal_admin_menu.module 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_menu_settings.inc',
  18. );
  19. return $items;
  20. }
  21. /**
  22. * Implements hook_page_build().
  23. */
  24. function adminimal_admin_menu_page_build(&$page) {
  25. if (!_adminimal_admin_menu_access()) {
  26. return;
  27. }
  28. $path = drupal_get_path('module', 'adminimal_admin_menu');
  29. $load_slicknav = variable_get('adminimal_admin_menu_slicknav', TRUE );
  30. $load_jQuery = variable_get('adminimal_admin_menu_jquery', TRUE );
  31. // Attach the CSS and JavaScript assets.
  32. drupal_add_css($path . '/adminimal_admin_menu.css');
  33. // Support for the Environment Indicator module.
  34. if (module_exists('environment_indicator')) {
  35. $environment_info = environment_indicator_get_active();
  36. drupal_add_css('
  37. div.slicknav_menu a.slicknav_btn:after {
  38. content: "' . $environment_info['name']. '";
  39. margin-left: 1em;
  40. color: ' . $environment_info['text_color'] . ';
  41. font-size: 12px;
  42. background-color: ' . $environment_info['color'] . ';
  43. padding: 2px 5px;
  44. }
  45. #admin-menu-wrapper {background: #222 !important;}
  46. ',
  47. array(
  48. 'group' => CSS_DEFAULT,
  49. 'type' => 'inline',
  50. 'preprocess' => FALSE,
  51. 'weight' => '100',
  52. ));
  53. }
  54. // Check if both slicknav and custom jQuery must be loaded.
  55. if ($load_slicknav and $load_jQuery) {
  56. drupal_add_js($path . '/js/jquery.min.js', array( 'type' => 'file', 'scope' => 'header', 'weight' => 888 ));
  57. drupal_add_js($path . '/js/slicknav/jquery-no-conflict.slicknav.js', array( 'type' => 'file', 'scope' => 'header', 'weight' => 888 ));
  58. drupal_add_css($path . '/js/slicknav/slicknav.css');
  59. }
  60. elseif ($load_slicknav and !$load_jQuery) {
  61. drupal_add_js($path . '/js/slicknav/jquery.slicknav.js', array( 'type' => 'file', 'scope' => 'header', 'weight' => 888 ));
  62. drupal_add_css($path . '/js/slicknav/slicknav.css');
  63. }
  64. drupal_add_js($path . '/js/adminimal_admin_menu.js', array( 'type' => 'file', 'scope' => 'header', 'weight' => 888 ));
  65. if (!isset($page['page_bottom']['admin_menu'])) {
  66. return;
  67. }
  68. $attached = &$page['page_bottom']['admin_menu']['#attached'];
  69. $options = array('every_page' => TRUE);
  70. // @todo Stop-gap fix until cached rendering is resolved.
  71. // @see http://drupal.org/node/1567622
  72. if (module_exists('shortcut')) {
  73. $attached['css'][drupal_get_path('module', 'shortcut') . '/shortcut.css'] = $options;
  74. }
  75. $settings = array();
  76. // Add current path to support menu item highlighting.
  77. // @todo Compile real active trail here?
  78. $args = explode('/', $_GET['q']);
  79. if ($args[0] == 'admin' && !empty($args[1])) {
  80. $settings['activeTrail'] = url($args[0] . '/' . $args[1]);
  81. }
  82. elseif (drupal_is_front_page()) {
  83. $settings['activeTrail'] = url('<front>');
  84. }
  85. $attached['js'][] = array(
  86. 'data' => array('admin_menu' => array('toolbar' => $settings)),
  87. 'type' => 'setting',
  88. );
  89. }
  90. /**
  91. * Implements hook_admin_menu_output_build().
  92. */
  93. function adminimal_admin_menu_admin_menu_output_build(&$content) {
  94. if (variable_get('adminimal_admin_menu_render', 'collapsed') != 'hidden') {
  95. // Add shortcuts bar.
  96. $content['shortcut'] = array(
  97. '#access' => module_exists('shortcut'),
  98. '#weight' => 200,
  99. '#prefix' => '<div class="shortcut-toolbar">',
  100. '#suffix' => '</div>',
  101. );
  102. $content['shortcut']['shortcuts'] = array(
  103. // Shortcut module's CSS relies on Toolbar module's markup.
  104. // @see http://drupal.org/node/1217038
  105. '#prefix' => '<div id="toolbar">',
  106. '#suffix' => '</div>',
  107. // @todo Links may contain .active-trail classes.
  108. '#pre_render' => array('shortcut_toolbar_pre_render'),
  109. );
  110. }
  111. }
  112. /**
  113. * Implements hook_admin_menu_output_alter().
  114. */
  115. function adminimal_admin_menu_admin_menu_output_alter(&$content) {
  116. // Add a class to top-level items for styling.
  117. foreach (element_children($content['menu']) as $link) {
  118. $content['menu'][$link]['#attributes']['class'][] = 'admin-menu-toolbar-category';
  119. }
  120. // Alter icon.
  121. unset($content['icon']['icon']['#theme']);
  122. $content['icon']['icon']['#title'] = '<span class="admin-menu-home-icon">&nbsp;</span>';
  123. $content['icon']['icon']['#attributes']['class'][] = 'admin-menu-toolbar-home-menu';
  124. $page['#attributes']['class'][] = 'adminimal-menu';
  125. // Hide the menu.
  126. if (variable_get('adminimal_admin_menu_render', 'collapsed') == 'exclusive') {
  127. unset($content['icon']['icon']);
  128. unset($content['search']);
  129. foreach ($content['menu'] as $key => $link) {
  130. // Move local tasks on 'admin' into icon menu.
  131. unset($content['menu'][$key]);
  132. }
  133. }
  134. // Create the responsive menu.
  135. if (variable_get('adminimal_admin_menu_slicknav', 'TRUE')) {
  136. // Prepare the responsive menu and Join the Icon menu with the administration menu.
  137. $responsivemenu = array_merge($content['icon'], $content['menu']);
  138. // Give it ID to target it later with js and css.
  139. $responsivemenu['#wrapper_attributes']['id'] = 'admin-menu-menu-responsive';
  140. // Move the icon menu to the top.
  141. $responsivemenu['icon']['#weight'] = '-100';
  142. // Change the link title to Administration.
  143. $responsivemenu['admin/index']['#title'] = t('Administration');
  144. // Bind the responsive menu the the content varable so it can be rendered.
  145. $content['responsive-menu'] = $responsivemenu;
  146. // Create the responsive shortucts.
  147. $content['responsive']['shortcuts'] = array(
  148. '#prefix' => '<div id="admin-menu-shortcuts-responsive">',
  149. '#suffix' => '</div>',
  150. '#pre_render' => array('shortcut_toolbar_pre_render'),
  151. );
  152. }
  153. }
  154. /**
  155. * Implements hook_preprocess_html().
  156. */
  157. function adminimal_admin_menu_preprocess_html(&$vars) {
  158. if (!_adminimal_admin_menu_access()) {
  159. return;
  160. }
  161. // Add the "adminimal" class to the body for better css selection.
  162. $vars['classes_array'][] = 'adminimal-menu';
  163. // Add frontend and backend classes to the body.
  164. if (path_is_admin(current_path())) {
  165. $vars['classes_array'][] = 'adminimal-backend';
  166. }
  167. else {
  168. $vars['classes_array'][] = 'adminimal-frontend';
  169. }
  170. // Add the shortcut render mode class.
  171. $vars['classes_array'][] = 'menu-render-' . variable_get('adminimal_admin_menu_render', 'collapsed');
  172. // Fix the viewport, correcting the mobile device zoom.
  173. /** @todo - Mybre remove this and let the theme manage the view pont.
  174. * But i was suprised that only few "responsive" drupal themes were fixing
  175. * the viewport.
  176. */
  177. $viewport = array(
  178. '#tag' => 'meta',
  179. '#attributes' => array(
  180. 'name' => 'viewport',
  181. 'content' => 'width=device-width, initial-scale=1, maximum-scale=1',
  182. ),
  183. );
  184. drupal_add_html_head($viewport, 'viewport');
  185. }
  186. /**
  187. * Check if the user has access to use the admin menu.
  188. * @return boolean Result of access checks.
  189. */
  190. function _adminimal_admin_menu_access() {
  191. if (!user_access('access administration menu') || admin_menu_suppress(FALSE)) {
  192. return FALSE;
  193. }
  194. // Performance: Skip this entirely for AJAX requests.
  195. if (strpos($_GET['q'], 'js/') === 0) {
  196. return FALSE;
  197. }
  198. return TRUE;
  199. }