admin_menu.module 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. <?php
  2. /**
  3. * @file
  4. * Render an administrative menu as a dropdown menu at the top of the window.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function admin_menu_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/config/administration/admin_menu':
  12. return '<p>' . t('The administration menu module provides a dropdown menu arranged for one- or two-click access to most administrative tasks and other common destinations (to users with the proper permissions). Use the settings below to customize the appearance of the menu.') . '</p>';
  13. case 'admin/help#admin_menu':
  14. $output = '';
  15. $output .= '<p>' . t('The administration menu module provides a dropdown menu arranged for one- or two-click access to most administrative tasks and other common destinations (to users with the proper permissions). Administration menu also displays the number of anonymous and authenticated users, and allows modules to add their own custom menu items. Integration with the menu varies from module to module; the contributed module <a href="@drupal">Devel</a>, for instance, makes strong use of the administration menu module to provide quick access to development tools.', array('@drupal' => 'http://drupal.org/project/devel')) . '</p>';
  16. $output .= '<p>' . t('The administration menu <a href="@settings">settings page</a> allows you to modify some elements of the menu\'s behavior and appearance. Since the appearance of the menu is dependent on your site theme, substantial customizations require modifications to your site\'s theme and CSS files. See the advanced module README.txt file for more information on theme and CSS customizations.', array('@settings' => url('admin/config/administration/admin_menu'))) . '</p>';
  17. $output .= '<p>' . t('The menu items displayed in the administration menu depend upon the actual permissions of the viewer. First, the administration menu is only displayed to users in roles with the <em>Access administration menu</em> (admin_menu module) permission. Second, a user must be a member of a role with the <em>Access administration pages</em> (system module) permission to view administrative links. And, third, only currently permitted links are displayed; for example, if a user is not a member of a role with the permissions <em>Administer permissions</em> (user module) and <em>Administer users</em> (user module), the <em>User management</em> menu item is not displayed.') . '</p>';
  18. return $output;
  19. }
  20. }
  21. /**
  22. * Implements hook_permission().
  23. */
  24. function admin_menu_permission() {
  25. return array(
  26. 'access administration menu' => array(
  27. 'title' => t('Access administration menu'),
  28. 'description' => t('Display the administration menu at the top of each page.'),
  29. ),
  30. 'flush caches' => array(
  31. 'title' => t('Flush caches'),
  32. 'description' => t('Access links to flush caches in the administration menu.'),
  33. ),
  34. 'display drupal links' => array(
  35. 'title' => t('Display Drupal links'),
  36. 'description' => t('Provide Drupal.org links in the administration menu.'),
  37. ),
  38. );
  39. }
  40. /**
  41. * Implements hook_theme().
  42. */
  43. function admin_menu_theme() {
  44. return array(
  45. 'admin_menu_links' => array(
  46. 'render element' => 'elements',
  47. ),
  48. 'admin_menu_icon' => array(
  49. 'variables' => array('src' => NULL, 'alt' => NULL),
  50. 'file' => 'admin_menu.inc',
  51. ),
  52. );
  53. }
  54. /**
  55. * Implements hook_menu().
  56. */
  57. function admin_menu_menu() {
  58. // AJAX callback.
  59. // @see http://drupal.org/project/js
  60. $items['js/admin_menu/cache'] = array(
  61. 'page callback' => 'admin_menu_js_cache',
  62. 'delivery callback' => 'admin_menu_deliver',
  63. 'access arguments' => array('access administration menu'),
  64. 'type' => MENU_CALLBACK,
  65. );
  66. // Module settings.
  67. $items['admin/config/administration'] = array(
  68. 'title' => 'Administration',
  69. 'description' => 'Administration tools.',
  70. 'page callback' => 'system_admin_menu_block_page',
  71. 'access arguments' => array('access administration pages'),
  72. 'file' => 'system.admin.inc',
  73. 'file path' => drupal_get_path('module', 'system'),
  74. );
  75. $items['admin/config/administration/admin_menu'] = array(
  76. 'title' => 'Administration menu',
  77. 'description' => 'Adjust administration menu settings.',
  78. 'page callback' => 'drupal_get_form',
  79. 'page arguments' => array('admin_menu_theme_settings'),
  80. 'access arguments' => array('administer site configuration'),
  81. 'file' => 'admin_menu.inc',
  82. );
  83. // Menu link callbacks.
  84. $items['admin_menu/toggle-modules'] = array(
  85. 'page callback' => 'admin_menu_toggle_modules',
  86. 'access arguments' => array('administer modules'),
  87. 'type' => MENU_CALLBACK,
  88. 'file' => 'admin_menu.inc',
  89. );
  90. $items['admin_menu/flush-cache'] = array(
  91. 'page callback' => 'admin_menu_flush_cache',
  92. 'access arguments' => array('flush caches'),
  93. 'type' => MENU_CALLBACK,
  94. 'file' => 'admin_menu.inc',
  95. );
  96. return $items;
  97. }
  98. /**
  99. * Implements hook_menu_alter().
  100. */
  101. function admin_menu_menu_alter(&$items) {
  102. // Flush client-side caches whenever the menu is rebuilt.
  103. admin_menu_flush_caches();
  104. }
  105. /**
  106. * Implements hook_menu_link_insert().
  107. */
  108. function admin_menu_menu_link_insert($link) {
  109. // Flush all of our caches to pick up the link.
  110. admin_menu_flush_caches();
  111. }
  112. /**
  113. * Implements hook_menu_link_update().
  114. */
  115. function admin_menu_menu_link_update($link) {
  116. // Flush all of our caches to pick up the link.
  117. admin_menu_flush_caches();
  118. }
  119. /**
  120. * Implements hook_menu_link_delete().
  121. */
  122. function admin_menu_menu_link_delete($link) {
  123. // Flush all of our caches to pick up the link.
  124. admin_menu_flush_caches();
  125. }
  126. /**
  127. * Implements hook_system_info_alter().
  128. *
  129. * Indicate that the 'page_bottom' region (in which the administration menu
  130. * is displayed) is an overlay supplemental region that should be refreshed
  131. * whenever its content is updated.
  132. *
  133. * @see toolbar_system_info_alter()
  134. */
  135. function admin_menu_system_info_alter(&$info, $file, $type) {
  136. if ($type == 'theme') {
  137. $info['overlay_supplemental_regions'][] = 'page_bottom';
  138. }
  139. }
  140. /**
  141. * Implements hook_page_build().
  142. */
  143. function admin_menu_page_build(&$page) {
  144. if (!user_access('access administration menu') || admin_menu_suppress(FALSE)) {
  145. return;
  146. }
  147. // Performance: Skip this entirely for AJAX requests.
  148. if (strpos($_GET['q'], 'js/') === 0) {
  149. return;
  150. }
  151. global $user, $language;
  152. $path = drupal_get_path('module', 'admin_menu');
  153. $page['page_bottom']['admin_menu'] = array(
  154. '#attached' => array(),
  155. );
  156. $attached = &$page['page_bottom']['admin_menu']['#attached'];
  157. $options = array('every_page' => TRUE);
  158. $attached['css'][$path . '/admin_menu.css'] = $options;
  159. if ($user->uid == 1) {
  160. $attached['css'][$path . '/admin_menu.uid1.css'] = $options;
  161. }
  162. // Previous versions used the 'defer' attribute to increase browser rendering
  163. // performance. At least starting with Firefox 3.6, deferred .js files are
  164. // loaded, but Drupal.behaviors are not contained in the DOM when drupal.js
  165. // executes Drupal.attachBehaviors().
  166. $attached['js'][$path . '/admin_menu.js'] = $options;
  167. // Destination query strings are applied via JS.
  168. $settings['destination'] = drupal_http_build_query(drupal_get_destination());
  169. // Determine whether we need to show all components and disable all caches.
  170. $complete = FALSE;
  171. if (current_path() == 'admin/config/administration/admin_menu' && $_SERVER['REQUEST_METHOD'] == 'GET') {
  172. $complete = TRUE;
  173. }
  174. // If the client supports JavaScript and we have a cached menu for the current
  175. // user, only output the hash for the client-side HTTP cache callback URL.
  176. $cid = 'admin_menu:' . $user->uid . ':' . session_id() . ':' . $language->language;
  177. if (!$complete && !empty($_COOKIE['has_js']) && ($hash = admin_menu_cache_get($cid))) {
  178. $settings['hash'] = $hash;
  179. // The base path to use for cache requests depends on whether clean URLs
  180. // are enabled, whether Drupal runs in a sub-directory, and on the language
  181. // system configuration. url() already provides us the proper path and also
  182. // invokes potentially existing custom_url_rewrite() functions, which may
  183. // add further required components to the URL to provide context. Due to
  184. // those components, and since url('') returns only base_path() when clean
  185. // URLs are disabled, we need to use a replacement token as path. Yuck.
  186. $settings['basePath'] = url('admin_menu');
  187. }
  188. // Otherwise, add the full menu to the page.
  189. else {
  190. $page['page_bottom']['admin_menu']['#markup'] = admin_menu_output($complete);
  191. }
  192. $replacements = module_invoke_all('admin_menu_replacements', $complete);
  193. if (!empty($replacements)) {
  194. $settings['replacements'] = $replacements;
  195. }
  196. if ($setting = variable_get('admin_menu_margin_top', 1)) {
  197. $settings['margin_top'] = $setting;
  198. // @todo Drupal.behaviors.adminMenuMarginTop is obsolete, but
  199. // hook_page_build() does not allow to set a CSS class on the body yet.
  200. // @see http://drupal.org/node/1473548, http://drupal.org/node/1194528
  201. //$page['#attributes']['class'][] = 'admin-menu';
  202. }
  203. if ($setting = variable_get('admin_menu_position_fixed', 1)) {
  204. $settings['position_fixed'] = $setting;
  205. // In fixed positioning, supply a callback function for tableheader.js to
  206. // allow it to determine the top viewport offset.
  207. // @see admin_menu.js, toolbar.js
  208. $attached['js'][] = array(
  209. 'data' => array('tableHeaderOffset' => 'Drupal.admin.height'),
  210. 'type' => 'setting',
  211. );
  212. }
  213. if ($setting = variable_get('admin_menu_tweak_tabs', 0)) {
  214. $settings['tweak_tabs'] = $setting;
  215. }
  216. if ($_GET['q'] == 'admin/modules' || strpos($_GET['q'], 'admin/modules/list') === 0) {
  217. $settings['tweak_modules'] = variable_get('admin_menu_tweak_modules', 0);
  218. }
  219. if ($_GET['q'] == 'admin/people/permissions' || $_GET['q'] == 'admin/people/permissions/list') {
  220. $settings['tweak_permissions'] = variable_get('admin_menu_tweak_permissions', 0);
  221. }
  222. $attached['js'][] = array(
  223. 'data' => array('admin_menu' => $settings),
  224. 'type' => 'setting',
  225. );
  226. }
  227. /**
  228. * Suppress display of administration menu.
  229. *
  230. * This function should be called from within another module's page callback
  231. * (preferably using module_invoke()) when the menu should not be displayed.
  232. * This is useful for modules that implement popup pages or other special
  233. * pages where the menu would be distracting or break the layout.
  234. *
  235. * @param $set
  236. * Defaults to TRUE. If called before hook_footer(), the menu will not be
  237. * displayed. If FALSE is passed, the suppression state is returned.
  238. */
  239. function admin_menu_suppress($set = TRUE) {
  240. static $suppress = FALSE;
  241. // drupal_add_js() must only be invoked once.
  242. if (!empty($set) && $suppress === FALSE) {
  243. $suppress = TRUE;
  244. drupal_add_js(array('admin_menu' => array('suppress' => 1)), 'setting');
  245. }
  246. return $suppress;
  247. }
  248. /**
  249. * Implements hook_js().
  250. */
  251. function admin_menu_js() {
  252. return array(
  253. 'cache' => array(
  254. 'callback' => 'admin_menu_js_cache',
  255. 'includes' => array('common', 'theme', 'unicode'),
  256. 'dependencies' => array('devel', 'filter', 'user'),
  257. ),
  258. );
  259. }
  260. /**
  261. * Retrieve a client-side cache hash from cache.
  262. *
  263. * The hash cache is consulted more than once per request; we therefore cache
  264. * the results statically to avoid multiple database requests.
  265. *
  266. * This should only be used for client-side cache hashes. Use cache_menu for
  267. * administration menu content.
  268. *
  269. * @param $cid
  270. * The cache ID of the data to retrieve.
  271. */
  272. function admin_menu_cache_get($cid) {
  273. $cache = &drupal_static(__FUNCTION__, array());
  274. if (!variable_get('admin_menu_cache_client', TRUE)) {
  275. return FALSE;
  276. }
  277. if (!array_key_exists($cid, $cache)) {
  278. $cache[$cid] = cache_get($cid, 'cache_admin_menu');
  279. if ($cache[$cid] && isset($cache[$cid]->data)) {
  280. $cache[$cid] = $cache[$cid]->data;
  281. }
  282. }
  283. return $cache[$cid];
  284. }
  285. /**
  286. * Store a client-side cache hash in persistent cache.
  287. *
  288. * This should only be used for client-side cache hashes. Use cache_menu for
  289. * administration menu content.
  290. *
  291. * @param $cid
  292. * The cache ID of the data to retrieve.
  293. */
  294. function admin_menu_cache_set($cid, $data) {
  295. if (variable_get('admin_menu_cache_client', TRUE)) {
  296. cache_set($cid, $data, 'cache_admin_menu');
  297. }
  298. }
  299. /**
  300. * Menu callback; Output administration menu for HTTP caching via AJAX request.
  301. *
  302. * @see admin_menu_deliver()
  303. */
  304. function admin_menu_js_cache() {
  305. global $conf;
  306. // Suppress Devel module.
  307. $GLOBALS['devel_shutdown'] = FALSE;
  308. // Enforce page caching.
  309. $conf['cache'] = 1;
  310. drupal_page_is_cacheable(TRUE);
  311. // If we have a cache, serve it.
  312. // @see _drupal_bootstrap_page_cache()
  313. $cache = drupal_page_get_cache();
  314. if (is_object($cache)) {
  315. header('X-Drupal-Cache: HIT');
  316. // Restore the metadata cached with the page.
  317. $_GET['q'] = $cache->data['path'];
  318. date_default_timezone_set(drupal_get_user_timezone());
  319. drupal_serve_page_from_cache($cache);
  320. // We are done.
  321. exit;
  322. }
  323. // Otherwise, create a new page response (that will be cached).
  324. header('X-Drupal-Cache: MISS');
  325. // The Expires HTTP header is the heart of the client-side HTTP caching. The
  326. // additional server-side page cache only takes effect when the client
  327. // accesses the callback URL again (e.g., after clearing the browser cache or
  328. // when force-reloading a Drupal page).
  329. $max_age = 3600 * 24 * 365;
  330. drupal_add_http_header('Expires', gmdate(DATE_RFC1123, REQUEST_TIME + $max_age));
  331. drupal_add_http_header('Cache-Control', 'private, max-age=' . $max_age);
  332. // Retrieve and return the rendered menu.
  333. return admin_menu_output();
  334. }
  335. /**
  336. * Delivery callback for client-side HTTP caching.
  337. *
  338. * @see admin_menu_js_cache()
  339. */
  340. function admin_menu_deliver($page_callback_result) {
  341. drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
  342. // Send appropriate language header for browsers.
  343. global $language;
  344. drupal_add_http_header('Content-Language', $language->language);
  345. // The page callback is always admin_menu_js_cache(), which always returns a
  346. // string, and is only accessed when the user actually has access to it.
  347. // Therefore, we do not care for the other possible page callback results.
  348. print $page_callback_result;
  349. // Perform end-of-request tasks. The page cache is created here.
  350. drupal_page_footer();
  351. }
  352. /**
  353. * Implements hook_admin_menu_replacements().
  354. */
  355. function admin_menu_admin_menu_replacements($complete) {
  356. $items = array();
  357. // If the complete menu is output, then it is uncached and will contain the
  358. // current counts already.
  359. if (!$complete) {
  360. // Check whether the users count component is enabled.
  361. $components = variable_get('admin_menu_components', array());
  362. if (!empty($components['admin_menu.users']) && ($user_count = admin_menu_get_user_count())) {
  363. // Replace the counters in the cached menu output with current counts.
  364. $items['.admin-menu-users a'] = $user_count;
  365. }
  366. }
  367. return $items;
  368. }
  369. /**
  370. * Return count of online anonymous/authenticated users.
  371. *
  372. * @see user_block(), user.module
  373. */
  374. function admin_menu_get_user_count() {
  375. $interval = REQUEST_TIME - variable_get('user_block_seconds_online', 900);
  376. $count_anon = admin_menu_session_count($interval, TRUE);
  377. $count_auth = admin_menu_session_count($interval, FALSE);
  378. return t('@count-anon / @count-auth', array('@count-anon' => $count_anon, '@count-auth' => $count_auth));
  379. }
  380. /**
  381. * Counts how many users are active on the site.
  382. *
  383. * Counts how many users have sessions which have been active since the
  384. * specified time. Can count either anonymous sessions or authenticated
  385. * sessions.
  386. *
  387. * @param $timestamp
  388. * A Unix timestamp. Users who have been active since this time will be
  389. * counted. The default is 0, which counts all existing sessions.
  390. * @param $anonymous
  391. * TRUE counts only anonymous users. FALSE counts only authenticated users.
  392. *
  393. * @return
  394. * The number of users with sessions.
  395. *
  396. * @todo There are mostly no anonymous sessions anymore. Split this into a
  397. * separate module providing proper user statistics.
  398. */
  399. function admin_menu_session_count($timestamp = 0, $anonymous = TRUE) {
  400. $query = db_select('sessions');
  401. $query->addExpression('COUNT(sid)', 'count');
  402. $query->condition('timestamp', $timestamp, '>=');
  403. $query->condition('uid', 0, $anonymous ? '=' : '>');
  404. return $query->execute()->fetchField();
  405. }
  406. /**
  407. * Build the administration menu output.
  408. *
  409. * @param bool $complete
  410. * (optional) Whether to build to the complete menu including all components
  411. * and ignore the cache. Defaults to FALSE. Internally used for the settings
  412. * page.
  413. */
  414. function admin_menu_output($complete = FALSE) {
  415. global $user, $language;
  416. $cache_server_enabled = !$complete && variable_get('admin_menu_cache_server', TRUE);
  417. $cid = 'admin_menu:' . $user->uid . ':' . session_id() . ':' . $language->language;
  418. // Try to load and output administration menu from server-side cache.
  419. // @todo Duplicates the page cache? Page cache ID contains the hash that is
  420. // generated at the bottom of this function, which is based on $content,
  421. // but logically identical to the $cid. Investigate whether not only the
  422. // cache_menu but also the cache_admin_menu could be dropped; the
  423. // client-side HTTP cache hash check could be based on a cid lookup in
  424. // cache_page instead? (i.e., one cache to rule them all) However,
  425. // cache_page is cleared very often.
  426. if ($cache_server_enabled) {
  427. $cache = cache_get($cid, 'cache_menu');
  428. if ($cache && isset($cache->data)) {
  429. $content = $cache->data;
  430. }
  431. }
  432. // Rebuild the output.
  433. if (!isset($content)) {
  434. // Retrieve enabled components to display and make them available for others.
  435. $components = variable_get('admin_menu_components', array());
  436. $components += array(
  437. 'admin_menu.menu' => TRUE,
  438. 'admin_menu.icon' => TRUE,
  439. 'admin_menu.account' => TRUE,
  440. );
  441. $content['#components'] = $components;
  442. $content['#complete'] = $complete;
  443. // Add site name as CSS class for development/staging theming purposes. We
  444. // leverage the cookie domain instead of HTTP_HOST to account for many (but
  445. // not all) multi-domain setups (e.g. language-based sub-domains).
  446. $classes = 'admin-menu-site' . drupal_strtolower(preg_replace('/[^a-zA-Z0-9-]/', '-', $GLOBALS['cookie_domain']));
  447. // Displace overlay.
  448. // @see Drupal.overlay.create
  449. // @see toolbar_preprocess_toolbar()
  450. if (module_exists('overlay')) {
  451. $classes .= ' overlay-displace-top';
  452. }
  453. // @todo Always output container to harden JS-less support.
  454. $content['#prefix'] = '<div id="admin-menu" class="' . $classes . '"><div id="admin-menu-wrapper">';
  455. $content['#suffix'] = '</div></div>';
  456. // Load menu builder functions.
  457. module_load_include('inc', 'admin_menu');
  458. // @todo Move the below callbacks into hook_admin_menu_build()
  459. // implementations (and $module.admin_menu.inc).
  460. // Add administration menu.
  461. if (!empty($components['admin_menu.menu']) || $complete) {
  462. $content['menu'] = admin_menu_links_menu(admin_menu_tree('management'));
  463. $content['menu']['#theme'] = 'admin_menu_links';
  464. $content['menu']['#wrapper_attributes']['id'] = 'admin-menu-menu';
  465. // Ensure the menu tree is rendered between the icon and user links.
  466. $content['menu']['#weight'] = 0;
  467. }
  468. // Add menu additions.
  469. if (!empty($components['admin_menu.icon']) || $complete) {
  470. $content['icon'] = admin_menu_links_icon();
  471. }
  472. if (!empty($components['admin_menu.account']) || $complete) {
  473. $content['account'] = admin_menu_links_account();
  474. }
  475. if (!empty($components['admin_menu.users']) || $complete) {
  476. $content['users'] = admin_menu_links_users();
  477. }
  478. if (!empty($components['admin_menu.search']) || $complete) {
  479. $content['search'] = admin_menu_links_search();
  480. }
  481. // Allow modules to enhance the menu.
  482. // Uses '_output' suffix for consistency with the alter hook (see below).
  483. foreach (module_implements('admin_menu_output_build') as $module) {
  484. $function = $module . '_admin_menu_output_build';
  485. $function($content);
  486. }
  487. // Allow modules to alter the output.
  488. // The '_output' suffix is required to prevent hook implementation function
  489. // name clashes with the contributed Admin module.
  490. drupal_alter('admin_menu_output', $content);
  491. $content = drupal_render($content);
  492. // Cache the menu for this user.
  493. if ($cache_server_enabled) {
  494. cache_set($cid, $content, 'cache_menu');
  495. }
  496. }
  497. // Store the new hash for this user.
  498. if (!empty($_COOKIE['has_js']) && !$complete) {
  499. admin_menu_cache_set($cid, md5($content));
  500. }
  501. return $content;
  502. }
  503. /**
  504. * Implements hook_admin_menu_output_build().
  505. */
  506. function admin_menu_admin_menu_output_build(&$content) {
  507. if (!isset($content['menu'])) {
  508. return;
  509. }
  510. // Unassign weights for categories below Configuration.
  511. // An alphabetical order is more natural for a dropdown menu.
  512. if (isset($content['menu']['admin/config'])) {
  513. foreach (element_children($content['menu']['admin/config']) as $key) {
  514. $content['menu']['admin/config'][$key]['#weight_original'] = $content['menu']['admin/config'][$key]['#weight'];
  515. unset($content['menu']['admin/config'][$key]['#weight']);
  516. }
  517. }
  518. // Retrieve the "Add content" link tree.
  519. $link = db_query("SELECT * FROM {menu_links} WHERE router_path = 'node/add' AND module = 'system'")->fetchAssoc();
  520. $conditions = array();
  521. for ($i = 1; $i < MENU_MAX_DEPTH; $i++) {
  522. if (!empty($link["p$i"])) {
  523. $conditions["p$i"] = $link["p$i"];
  524. }
  525. }
  526. $tree = menu_build_tree($link['menu_name'], array(
  527. 'conditions' => $conditions,
  528. 'min_depth' => $link['depth'],
  529. ));
  530. $links = admin_menu_links_menu($tree);
  531. if (!empty($links)) {
  532. // If the user has access to the top-level "Content" category, insert the
  533. // "Add content" link tree there.
  534. if (isset($content['menu']['admin/content'])) {
  535. $content['menu']['admin/content'] += $links;
  536. }
  537. // Otherwise make insert "Add content" as top-level category.
  538. else {
  539. $key = key($links);
  540. $links[$key]['#weight'] = -100;
  541. $content['menu'] += $links;
  542. }
  543. }
  544. }
  545. /**
  546. * Implements hook_admin_menu_output_alter().
  547. */
  548. function admin_menu_admin_menu_output_alter(&$content) {
  549. foreach ($content['menu'] as $key => $link) {
  550. // Move local tasks on 'admin' into icon menu.
  551. if ($key == 'admin/tasks' || $key == 'admin/index') {
  552. $content['icon']['icon'][$key] = $link;
  553. unset($content['menu'][$key]);
  554. }
  555. }
  556. }
  557. /**
  558. * Render a themed list of links.
  559. *
  560. * @param $variables
  561. * - elements: A renderable array of links using the following keys:
  562. * - #attributes: Optional array of attributes for the list item, processed
  563. * via drupal_attributes().
  564. * - #title: Title of the link, passed to l().
  565. * - #href: Optional path of the link, passed to l(). When omitted, the
  566. * element's '#title' is rendered without link.
  567. * - #description: Optional alternative text for the link, passed to l().
  568. * - #options: Optional alternative text for the link, passed to l().
  569. * The array key of each child element itself is passed as path for l().
  570. */
  571. function theme_admin_menu_links($variables) {
  572. $destination = &drupal_static('admin_menu_destination');
  573. $elements = $variables['elements'];
  574. if (!isset($destination)) {
  575. $destination = drupal_get_destination();
  576. $destination = $destination['destination'];
  577. }
  578. // The majority of items in the menu are sorted already, but since modules
  579. // may add or change arbitrary items anywhere, there is no way around sorting
  580. // everything again. element_sort() is not sufficient here, as it
  581. // intentionally retains the order of elements having the same #weight,
  582. // whereas menu links are supposed to be ordered by #weight and #title.
  583. uasort($elements, 'admin_menu_element_sort');
  584. $elements['#sorted'] = TRUE;
  585. $output = '';
  586. foreach (element_children($elements) as $path) {
  587. // Early-return nothing if user does not have access.
  588. if (isset($elements[$path]['#access']) && !$elements[$path]['#access']) {
  589. continue;
  590. }
  591. $elements[$path] += array(
  592. '#attributes' => array(),
  593. '#options' => array(),
  594. );
  595. // Render children to determine whether this link is expandable.
  596. if (isset($elements[$path]['#type']) || isset($elements[$path]['#theme']) || isset($elements[$path]['#pre_render'])) {
  597. $elements[$path]['#children'] = drupal_render($elements[$path]);
  598. }
  599. else {
  600. $elements[$path]['#children'] = theme('admin_menu_links', array('elements' => $elements[$path]));
  601. if (!empty($elements[$path]['#children'])) {
  602. $elements[$path]['#attributes']['class'][] = 'expandable';
  603. }
  604. if (isset($elements[$path]['#attributes']['class'])) {
  605. $elements[$path]['#attributes']['class'] = $elements[$path]['#attributes']['class'];
  606. }
  607. }
  608. $link = '';
  609. // Handle menu links.
  610. if (isset($elements[$path]['#href'])) {
  611. // Strip destination query string from href attribute and apply a CSS class
  612. // for our JavaScript behavior instead.
  613. if (isset($elements[$path]['#options']['query']['destination']) && $elements[$path]['#options']['query']['destination'] == $destination) {
  614. unset($elements[$path]['#options']['query']['destination']);
  615. $elements[$path]['#options']['attributes']['class'][] = 'admin-menu-destination';
  616. }
  617. $link = l($elements[$path]['#title'], $elements[$path]['#href'], $elements[$path]['#options']);
  618. }
  619. // Handle plain text items, but do not interfere with menu additions.
  620. elseif (!isset($elements[$path]['#type']) && isset($elements[$path]['#title'])) {
  621. if (!empty($elements[$path]['#options']['html'])) {
  622. $title = $elements[$path]['#title'];
  623. }
  624. else {
  625. $title = check_plain($elements[$path]['#title']);
  626. }
  627. $attributes = '';
  628. if (isset($elements[$path]['#options']['attributes'])) {
  629. $attributes = drupal_attributes($elements[$path]['#options']['attributes']);
  630. }
  631. $link = '<span' . $attributes . '>' . $title . '</span>';
  632. }
  633. $output .= '<li' . drupal_attributes($elements[$path]['#attributes']) . '>';
  634. $output .= $link . $elements[$path]['#children'];
  635. $output .= '</li>';
  636. }
  637. // @todo #attributes probably required for UL, but already used for LI.
  638. // @todo Use $element['#children'] here instead.
  639. if ($output) {
  640. $elements['#wrapper_attributes']['class'][] = 'dropdown';
  641. $attributes = drupal_attributes($elements['#wrapper_attributes']);
  642. $output = "\n" . '<ul' . $attributes . '>' . $output . '</ul>';
  643. }
  644. return $output;
  645. }
  646. /**
  647. * Function used by uasort to sort structured arrays by #weight AND #title.
  648. */
  649. function admin_menu_element_sort($a, $b) {
  650. // @see element_sort()
  651. $a_weight = isset($a['#weight']) ? $a['#weight'] : 0;
  652. $b_weight = isset($b['#weight']) ? $b['#weight'] : 0;
  653. if ($a_weight == $b_weight) {
  654. // @see element_sort_by_title()
  655. $a_title = isset($a['#title']) ? $a['#title'] : '';
  656. $b_title = isset($b['#title']) ? $b['#title'] : '';
  657. return strnatcasecmp($a_title, $b_title);
  658. }
  659. return ($a_weight < $b_weight) ? -1 : 1;
  660. }
  661. /**
  662. * Implements hook_translated_menu_link_alter().
  663. *
  664. * Here is where we make changes to links that need dynamic information such
  665. * as the current page path or the number of users.
  666. */
  667. function admin_menu_translated_menu_link_alter(&$item, $map) {
  668. global $user, $base_url;
  669. static $access_all;
  670. if ($item['menu_name'] != 'admin_menu') {
  671. return;
  672. }
  673. // Check whether additional development output is enabled.
  674. if (!isset($access_all)) {
  675. $access_all = variable_get('admin_menu_show_all', 0) && module_exists('devel');
  676. }
  677. // Prepare links that would not be displayed normally.
  678. if ($access_all && !$item['access']) {
  679. $item['access'] = TRUE;
  680. // Prepare for http://drupal.org/node/266596
  681. if (!isset($item['localized_options'])) {
  682. _menu_item_localize($item, $map, TRUE);
  683. }
  684. }
  685. // Don't waste cycles altering items that are not visible
  686. if (!$item['access']) {
  687. return;
  688. }
  689. // Add developer information to all links, if enabled.
  690. if ($extra = variable_get('admin_menu_display', 0)) {
  691. $item['title'] .= ' ' . $extra[0] . ': ' . $item[$extra];
  692. }
  693. }
  694. /**
  695. * Implements hook_flush_caches().
  696. *
  697. * Flushes client-side caches.
  698. *
  699. * @param int $uid
  700. * (optional) A user ID to limit the cache flush to.
  701. */
  702. function admin_menu_flush_caches($uid = NULL) {
  703. // A call to menu_rebuild() will trigger potentially thousands of calls into
  704. // menu_link_save(), for which admin_menu has to implement the corresponding
  705. // CRUD hooks, in order to take up any menu link changes, since any menu link
  706. // change could affect the admin menu (which essentially is an aggregate) and
  707. // since there is no other way to get notified about stale caches. The cache
  708. // only needs to be flushed once though, so we prevent a ton of needless
  709. // subsequent calls with this static.
  710. // @see http://drupal.org/node/918538
  711. $was_flushed = &drupal_static(__FUNCTION__, array());
  712. // $uid can be NULL. PHP automatically converts that into '' (empty string),
  713. // which is different to uid 0 (zero).
  714. if (isset($was_flushed[$uid])) {
  715. return;
  716. }
  717. $was_flushed[$uid] = TRUE;
  718. $cid = 'admin_menu:';
  719. if (isset($uid)) {
  720. $cid .= $uid . ':';
  721. }
  722. // Flush cached output of admin_menu.
  723. cache_clear_all($cid, 'cache_menu', TRUE);
  724. // Flush client-side cache hashes.
  725. drupal_static_reset('admin_menu_cache_get');
  726. // db_table_exists() required for SimpleTest.
  727. if (db_table_exists('cache_admin_menu')) {
  728. cache_clear_all(isset($uid) ? $cid : '*', 'cache_admin_menu', TRUE);
  729. }
  730. }
  731. /**
  732. * Implements hook_form_alter().
  733. */
  734. function admin_menu_form_alter(&$form, &$form_state, $form_id) {
  735. $global_flush_ids = array(
  736. 'admin_menu_theme_settings' => 1,
  737. // Update links for clean/non-clean URLs.
  738. 'system_clean_url_settings' => 1,
  739. // Incorporate changed user permissions.
  740. 'user_admin_permissions' => 1,
  741. // Removing a role potentially means less permissions.
  742. 'user_admin_role_delete_confirm' => 1,
  743. // User name and roles may be changed on the user account form.
  744. 'user_profile_form' => 1,
  745. );
  746. if (isset($global_flush_ids[$form_id])) {
  747. $form['#submit'][] = 'admin_menu_form_alter_flush_cache_submit';
  748. // Optionally limit the cache flush to a certain user ID.
  749. $form_state['admin_menu_uid'] = NULL;
  750. if ($form_id == 'user_profile_form') {
  751. $form_state['admin_menu_uid'] = $form_state['user']->uid;
  752. }
  753. }
  754. // UX: Add a confirmation to the permissions form to ask the user whether to
  755. // auto-enable the 'access administration menu' permission along with
  756. // 'access administration pages'.
  757. if ($form_id == 'user_admin_permissions') {
  758. $form['#attached']['js'][] = drupal_get_path('module', 'admin_menu') . '/admin_menu.admin.js';
  759. }
  760. }
  761. /**
  762. * Form submission handler to flush Administration menu caches.
  763. */
  764. function admin_menu_form_alter_flush_cache_submit($form, &$form_state) {
  765. admin_menu_flush_caches($form_state['admin_menu_uid']);
  766. }
  767. /**
  768. * Implements hook_form_FORM_ID_alter().
  769. *
  770. * Extends Devel module with Administration menu developer settings.
  771. */
  772. function admin_menu_form_devel_admin_settings_alter(&$form, &$form_state) {
  773. form_load_include($form_state, 'inc', 'admin_menu');
  774. _admin_menu_form_devel_admin_settings_alter($form, $form_state);
  775. }