admin_menu.module 31 KB

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