admin_menu.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. <?php
  2. /**
  3. * @file
  4. * Menu builder functions for Administration menu.
  5. */
  6. /**
  7. * Build the full administration menu tree from static and expanded dynamic items.
  8. *
  9. * @param $menu_name
  10. * The menu name to use as base for the tree.
  11. */
  12. function admin_menu_tree($menu_name) {
  13. // Get placeholder expansion arguments from hook_admin_menu_map()
  14. // implementations.
  15. module_load_include('inc', 'admin_menu', 'admin_menu.map');
  16. $expand_map = module_invoke_all('admin_menu_map');
  17. // Allow modules to alter the expansion map.
  18. drupal_alter('admin_menu_map', $expand_map);
  19. $new_map = array();
  20. foreach ($expand_map as $path => $data) {
  21. // Convert named placeholders to anonymous placeholders, since the menu
  22. // system stores paths using anonymous placeholders.
  23. $replacements = array_fill_keys(array_keys($data['arguments'][0]), '%');
  24. $data['parent'] = strtr($data['parent'], $replacements);
  25. $new_map[strtr($path, $replacements)] = $data;
  26. }
  27. $expand_map = $new_map;
  28. unset($new_map);
  29. // Retrieve dynamic menu link tree for the expansion mappings.
  30. // @todo Skip entire processing if initial $expand_map is empty and directly
  31. // return $tree?
  32. if (!empty($expand_map)) {
  33. $tree_dynamic = admin_menu_tree_dynamic($expand_map);
  34. }
  35. else {
  36. $tree_dynamic = array();
  37. }
  38. // Merge local tasks with static menu tree.
  39. $tree = menu_tree_all_data($menu_name);
  40. admin_menu_merge_tree($tree, $tree_dynamic, array());
  41. return $tree;
  42. }
  43. /**
  44. * Load menu link trees for router paths containing dynamic arguments.
  45. *
  46. * @param $expand_map
  47. * An array containing menu router path placeholder expansion argument
  48. * mappings.
  49. *
  50. * @return
  51. * An associative array whose keys are the parent paths of the menu router
  52. * paths given in $expand_map as well as the parent paths of any child link
  53. * deeper down the tree. The parent paths are used in admin_menu_merge_tree()
  54. * to check whether anything needs to be merged.
  55. *
  56. * @see hook_admin_menu_map()
  57. */
  58. function admin_menu_tree_dynamic(array $expand_map) {
  59. $p_columns = array();
  60. for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
  61. $p_columns[] = 'p' . $i;
  62. }
  63. // Fetch p* columns for all router paths to expand.
  64. $router_paths = array_keys($expand_map);
  65. $plids = db_select('menu_links', 'ml')
  66. ->fields('ml', $p_columns)
  67. ->condition('router_path', $router_paths)
  68. ->execute()
  69. ->fetchAll(PDO::FETCH_ASSOC);
  70. // Unlikely, but possible.
  71. if (empty($plids)) {
  72. return array();
  73. }
  74. // Use queried plid columns to query sub-trees for the router paths.
  75. $query = db_select('menu_links', 'ml');
  76. $query->join('menu_router', 'm', 'ml.router_path = m.path');
  77. $query
  78. ->fields('ml')
  79. ->fields('m', array_diff(drupal_schema_fields_sql('menu_router'), drupal_schema_fields_sql('menu_links')));
  80. // The retrieved menu link trees have to be ordered by depth, so parents
  81. // always come before their children for the storage logic below.
  82. foreach ($p_columns as $column) {
  83. $query->orderBy($column, 'ASC');
  84. }
  85. $db_or = db_or();
  86. foreach ($plids as $path_plids) {
  87. $db_and = db_and();
  88. // plids with value 0 may be ignored.
  89. foreach (array_filter($path_plids) as $column => $plid) {
  90. $db_and->condition($column, $plid);
  91. }
  92. $db_or->condition($db_and);
  93. }
  94. $query->condition($db_or);
  95. $result = $query
  96. ->execute()
  97. ->fetchAllAssoc('mlid', PDO::FETCH_ASSOC);
  98. // Store dynamic links grouped by parent path for later merging and assign
  99. // placeholder expansion arguments.
  100. $tree_dynamic = array();
  101. foreach ($result as $mlid => $link) {
  102. // If contained in $expand_map, then this is a (first) parent, and we need
  103. // to store by the defined 'parent' path for later merging, as well as
  104. // provide the expansion map arguments to apply to the dynamic tree.
  105. if (isset($expand_map[$link['path']])) {
  106. $parent_path = $expand_map[$link['path']]['parent'];
  107. $link['expand_map'] = $expand_map[$link['path']]['arguments'];
  108. }
  109. // Otherwise, just store this link keyed by its parent path; the expand_map
  110. // is automatically derived from parent paths.
  111. else {
  112. $parent_path = $result[$link['plid']]['path'];
  113. }
  114. $tree_dynamic[$parent_path][] = $link;
  115. }
  116. return $tree_dynamic;
  117. }
  118. /**
  119. * Walk through the entire menu tree and merge in expanded dynamic menu links.
  120. *
  121. * @param &$tree
  122. * A menu tree structure as returned by menu_tree_all_data().
  123. * @param $tree_dynamic
  124. * A dynamic menu tree structure as returned by admin_menu_tree_dynamic().
  125. * @param $expand_map
  126. * An array containing menu router path placeholder expansion argument
  127. * mappings.
  128. *
  129. * @see hook_admin_menu_map()
  130. * @see admin_menu_tree_dynamic()
  131. * @see menu_tree_all_data()
  132. */
  133. function admin_menu_merge_tree(array &$tree, array $tree_dynamic, array $expand_map) {
  134. foreach ($tree as $key => $data) {
  135. $path = $data['link']['router_path'];
  136. // Recurse into regular menu tree.
  137. if ($tree[$key]['below']) {
  138. admin_menu_merge_tree($tree[$key]['below'], $tree_dynamic, $expand_map);
  139. }
  140. // Nothing to merge, if this parent path is not in our dynamic tree.
  141. if (!isset($tree_dynamic[$path])) {
  142. continue;
  143. }
  144. // Add expanded dynamic items.
  145. foreach ($tree_dynamic[$path] as $link) {
  146. // If the dynamic item has custom placeholder expansion parameters set,
  147. // use them, otherwise keep current.
  148. if (isset($link['expand_map'])) {
  149. // If there are currently no expansion parameters, we may use the new
  150. // set immediately.
  151. if (empty($expand_map)) {
  152. $current_expand_map = $link['expand_map'];
  153. }
  154. else {
  155. // Otherwise we need to filter out elements that differ from the
  156. // current set, i.e. that are not in the same path.
  157. $current_expand_map = array();
  158. foreach ($expand_map as $arguments) {
  159. foreach ($arguments as $placeholder => $value) {
  160. foreach ($link['expand_map'] as $new_arguments) {
  161. // Skip the new argument if it doesn't contain the current
  162. // replacement placeholders or if their values differ.
  163. if (!isset($new_arguments[$placeholder]) || $new_arguments[$placeholder] != $value) {
  164. continue;
  165. }
  166. $current_expand_map[] = $new_arguments;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. else {
  173. $current_expand_map = $expand_map;
  174. }
  175. // Skip dynamic items without expansion parameters.
  176. if (empty($current_expand_map)) {
  177. continue;
  178. }
  179. // Expand anonymous to named placeholders.
  180. // @see _menu_load_objects()
  181. $path_args = explode('/', $link['path']);
  182. $load_functions = unserialize($link['load_functions']);
  183. if (is_array($load_functions)) {
  184. foreach ($load_functions as $index => $function) {
  185. if ($function) {
  186. if (is_array($function)) {
  187. list($function,) = each($function);
  188. }
  189. // Add the loader function name minus "_load".
  190. $placeholder = '%' . substr($function, 0, -5);
  191. $path_args[$index] = $placeholder;
  192. }
  193. }
  194. }
  195. $path_dynamic = implode('/', $path_args);
  196. // Create new menu items using expansion arguments.
  197. foreach ($current_expand_map as $arguments) {
  198. // Create the cartesian product for all arguments and create new
  199. // menu items for each generated combination thereof.
  200. foreach (admin_menu_expand_args($arguments) as $replacements) {
  201. $newpath = strtr($path_dynamic, $replacements);
  202. // Skip this item, if any placeholder could not be replaced.
  203. // Faster than trying to invoke _menu_translate().
  204. if (strpos($newpath, '%') !== FALSE) {
  205. continue;
  206. }
  207. $map = explode('/', $newpath);
  208. $item = admin_menu_translate($link, $map);
  209. // Skip this item, if the current user does not have access.
  210. if (empty($item)) {
  211. continue;
  212. }
  213. // Build subtree using current replacement arguments.
  214. $new_expand_map = array();
  215. foreach ($replacements as $placeholder => $value) {
  216. $new_expand_map[$placeholder] = array($value);
  217. }
  218. admin_menu_merge_tree($item, $tree_dynamic, array($new_expand_map));
  219. $tree[$key]['below'] += $item;
  220. }
  221. }
  222. }
  223. // Sort new subtree items.
  224. ksort($tree[$key]['below']);
  225. }
  226. }
  227. /**
  228. * Translate an expanded router item into a menu link suitable for rendering.
  229. *
  230. * @param $router_item
  231. * A menu router item.
  232. * @param $map
  233. * A path map with placeholders replaced.
  234. */
  235. function admin_menu_translate($router_item, $map) {
  236. _menu_translate($router_item, $map, TRUE);
  237. // Run through hook_translated_menu_link_alter() to add devel information,
  238. // if configured.
  239. $router_item['menu_name'] = 'management';
  240. // @todo Invoke as usual like _menu_link_translate().
  241. admin_menu_translated_menu_link_alter($router_item, NULL);
  242. if ($router_item['access']) {
  243. // Override mlid to make this item unique; since these items are expanded
  244. // from dynamic items, the mlid is always the same, so each item would
  245. // replace any other.
  246. // @todo Doing this instead leads to plenty of duplicate links below
  247. // admin/structure/menu; likely a hidden recursion problem.
  248. // $router_item['mlid'] = $router_item['href'] . $router_item['mlid'];
  249. $router_item['mlid'] = $router_item['href'];
  250. // Turn menu callbacks into regular menu items to make them visible.
  251. if ($router_item['type'] == MENU_CALLBACK) {
  252. $router_item['type'] = MENU_NORMAL_ITEM;
  253. }
  254. // @see _menu_tree_check_access()
  255. $key = (50000 + $router_item['weight']) . ' ' . $router_item['title'] . ' ' . $router_item['mlid'];
  256. return array($key => array(
  257. 'link' => $router_item,
  258. 'below' => array(),
  259. ));
  260. }
  261. return array();
  262. }
  263. /**
  264. * Create the cartesian product of multiple varying sized argument arrays.
  265. *
  266. * @param $arguments
  267. * A two dimensional array of arguments.
  268. *
  269. * @see hook_admin_menu_map()
  270. */
  271. function admin_menu_expand_args($arguments) {
  272. $replacements = array();
  273. // Initialize line cursors, move out array keys (placeholders) and assign
  274. // numeric keys instead.
  275. $i = 0;
  276. $placeholders = array();
  277. $new_arguments = array();
  278. foreach ($arguments as $placeholder => $values) {
  279. // Skip empty arguments.
  280. if (empty($values)) {
  281. continue;
  282. }
  283. $cursor[$i] = 0;
  284. $placeholders[$i] = $placeholder;
  285. $new_arguments[$i] = $values;
  286. $i++;
  287. }
  288. $arguments = $new_arguments;
  289. unset($new_arguments);
  290. if ($rows = count($arguments)) {
  291. do {
  292. // Collect current argument from each row.
  293. $row = array();
  294. for ($i = 0; $i < $rows; ++$i) {
  295. $row[$placeholders[$i]] = $arguments[$i][$cursor[$i]];
  296. }
  297. $replacements[] = $row;
  298. // Increment cursor position.
  299. $j = $rows - 1;
  300. $cursor[$j]++;
  301. while (!array_key_exists($cursor[$j], $arguments[$j])) {
  302. // No more arguments left: reset cursor, go to next line and increment
  303. // that cursor instead. Repeat until argument found or out of rows.
  304. $cursor[$j] = 0;
  305. if (--$j < 0) {
  306. // We're done.
  307. break 2;
  308. }
  309. $cursor[$j]++;
  310. }
  311. } while (1);
  312. }
  313. return $replacements;
  314. }
  315. /**
  316. * Build the administration menu as renderable menu links.
  317. *
  318. * @param $tree
  319. * A data structure representing the administration menu tree as returned from
  320. * menu_tree_all_data().
  321. *
  322. * @return
  323. * The complete administration menu, suitable for theme_admin_menu_links().
  324. *
  325. * @see theme_admin_menu_links()
  326. * @see admin_menu_menu_alter()
  327. */
  328. function admin_menu_links_menu($tree) {
  329. $links = array();
  330. foreach ($tree as $data) {
  331. // Skip items that are inaccessible, invisible, or link to their parent.
  332. // (MENU_DEFAULT_LOCAL_TASK), and MENU_CALLBACK-alike items that should only
  333. // appear in the breadcrumb.
  334. if (!$data['link']['access'] || $data['link']['type'] & MENU_LINKS_TO_PARENT || $data['link']['type'] == MENU_VISIBLE_IN_BREADCRUMB || $data['link']['hidden'] == 1) {
  335. continue;
  336. }
  337. // Hide 'Administer' and make child links appear on this level.
  338. // @todo Make this configurable.
  339. if ($data['link']['router_path'] == 'admin') {
  340. if ($data['below']) {
  341. $links = array_merge($links, admin_menu_links_menu($data['below']));
  342. }
  343. continue;
  344. }
  345. // Omit alias lookups.
  346. $data['link']['localized_options']['alias'] = TRUE;
  347. // Remove description to prevent mouseover tooltip clashes.
  348. unset($data['link']['localized_options']['attributes']['title']);
  349. // Make action links (typically "Add ...") appear first in dropdowns.
  350. // They might appear first already, but only as long as there is no link
  351. // that comes alphabetically first (e.g., a node type with label "Ad").
  352. if ($data['link']['type'] & MENU_IS_LOCAL_ACTION) {
  353. $data['link']['weight'] -= 1000;
  354. }
  355. $links[$data['link']['href']] = array(
  356. '#title' => $data['link']['title'],
  357. '#href' => $data['link']['href'],
  358. '#options' => $data['link']['localized_options'],
  359. '#weight' => $data['link']['weight'],
  360. );
  361. // Recurse to add any child links.
  362. $children = array();
  363. if ($data['below']) {
  364. $children = admin_menu_links_menu($data['below']);
  365. $links[$data['link']['href']] += $children;
  366. }
  367. // Handle links pointing to category/overview pages.
  368. if ($data['link']['page_callback'] == 'system_admin_menu_block_page' || $data['link']['page_callback'] == 'system_admin_config_page') {
  369. // Apply a marker for others to consume.
  370. $links[$data['link']['href']]['#is_category'] = TRUE;
  371. // Automatically hide empty categories.
  372. // Check for empty children first for performance. Only when non-empty
  373. // (typically 'admin/config'), check whether children are accessible.
  374. if (empty($children) || !element_get_visible_children($children)) {
  375. $links[$data['link']['href']]['#access'] = FALSE;
  376. }
  377. }
  378. }
  379. return $links;
  380. }
  381. /**
  382. * Build icon menu links; mostly containing maintenance helpers.
  383. *
  384. * @see theme_admin_menu_links()
  385. */
  386. function admin_menu_links_icon() {
  387. $destination = drupal_get_destination();
  388. $links = array(
  389. '#theme' => 'admin_menu_links',
  390. '#wrapper_attributes' => array('id' => 'admin-menu-icon'),
  391. '#weight' => -100,
  392. );
  393. $links['icon'] = array(
  394. '#title' => theme('admin_menu_icon'),
  395. '#attributes' => array('class' => array('admin-menu-icon')),
  396. '#href' => '<front>',
  397. '#options' => array(
  398. 'html' => TRUE,
  399. ),
  400. );
  401. // Add link to manually run cron.
  402. $links['icon']['cron'] = array(
  403. '#title' => t('Run cron'),
  404. '#weight' => 50,
  405. '#access' => user_access('administer site configuration'),
  406. '#href' => 'admin/reports/status/run-cron',
  407. );
  408. // Add link to run update.php.
  409. $links['icon']['update'] = array(
  410. '#title' => t('Run updates'),
  411. '#weight' => 50,
  412. // @see update_access_allowed()
  413. '#access' => $GLOBALS['user']->uid == 1 || !empty($GLOBALS['update_free_access']) || user_access('administer software updates'),
  414. '#href' => base_path() . 'update.php',
  415. '#options' => array(
  416. 'external' => TRUE,
  417. ),
  418. );
  419. // Add link to drupal.org.
  420. $links['icon']['drupal.org'] = array(
  421. '#title' => 'Drupal.org',
  422. '#weight' => 100,
  423. '#access' => user_access('display drupal links'),
  424. '#href' => 'http://drupal.org',
  425. );
  426. // Add links to project issue queues.
  427. foreach (module_list(FALSE, TRUE) as $module) {
  428. $info = drupal_parse_info_file(drupal_get_path('module', $module) . '/' . $module . '.info');
  429. if (!isset($info['project']) || isset($links['icon']['drupal.org'][$info['project']])) {
  430. continue;
  431. }
  432. $links['icon']['drupal.org'][$info['project']] = array(
  433. '#title' => t('@project issue queue', array('@project' => $info['name'])),
  434. '#weight' => ($info['project'] == 'drupal' ? -10 : 0),
  435. '#href' => 'http://drupal.org/project/issues/' . $info['project'],
  436. '#options' => array(
  437. 'query' => array('version' => (isset($info['core']) ? $info['core'] : 'All')),
  438. ),
  439. );
  440. }
  441. // Add items to flush caches.
  442. $links['icon']['flush-cache'] = array(
  443. '#title' => t('Flush all caches'),
  444. '#weight' => 20,
  445. '#access' => user_access('flush caches'),
  446. '#href' => 'admin_menu/flush-cache',
  447. '#options' => array(
  448. 'query' => $destination + array('token' => drupal_get_token('admin_menu/flush-cache')),
  449. ),
  450. );
  451. $caches = module_invoke_all('admin_menu_cache_info');
  452. foreach ($caches as $name => $cache) {
  453. $links['icon']['flush-cache'][$name] = array(
  454. '#title' => $cache['title'],
  455. '#href' => 'admin_menu/flush-cache/' . $name,
  456. '#options' => array(
  457. 'query' => $destination + array('token' => drupal_get_token('admin_menu/flush-cache/' . $name)),
  458. ),
  459. );
  460. }
  461. // Add Devel module menu links.
  462. if (module_exists('devel')) {
  463. $devel_tree = menu_build_tree('devel');
  464. $devel_links = admin_menu_links_menu($devel_tree);
  465. if (element_get_visible_children($devel_links)) {
  466. $links['icon']['devel'] = array(
  467. '#title' => t('Development'),
  468. '#weight' => 30,
  469. ) + $devel_links;
  470. }
  471. }
  472. return $links;
  473. }
  474. /**
  475. * Builds the account links.
  476. *
  477. * @see theme_admin_menu_links()
  478. */
  479. function admin_menu_links_account() {
  480. $links = array(
  481. '#theme' => 'admin_menu_links',
  482. '#wrapper_attributes' => array('id' => 'admin-menu-account'),
  483. '#weight' => 100,
  484. );
  485. $links['account'] = array(
  486. '#title' => format_username($GLOBALS['user']),
  487. '#weight' => -99,
  488. '#attributes' => array('class' => array('admin-menu-action', 'admin-menu-account')),
  489. '#href' => 'user/' . $GLOBALS['user']->uid,
  490. );
  491. $links['logout'] = array(
  492. '#title' => t('Log out'),
  493. '#weight' => -100,
  494. '#attributes' => array('class' => array('admin-menu-action')),
  495. '#href' => 'user/logout',
  496. );
  497. // Add Devel module switch user links.
  498. $switch_links = module_invoke('devel', 'switch_user_list');
  499. if (!empty($switch_links) && count($switch_links) > 1) {
  500. foreach ($switch_links as $uid => $link) {
  501. $links['account'][$link['title']] = array(
  502. '#title' => $link['title'],
  503. '#description' => $link['attributes']['title'],
  504. '#href' => $link['href'],
  505. '#options' => array(
  506. 'query' => $link['query'],
  507. 'html' => !empty($link['html']),
  508. ),
  509. );
  510. }
  511. }
  512. return $links;
  513. }
  514. /**
  515. * Builds user counter.
  516. *
  517. * @see theme_admin_menu_links()
  518. */
  519. function admin_menu_links_users() {
  520. $links = array(
  521. '#theme' => 'admin_menu_links',
  522. '#wrapper_attributes' => array('id' => 'admin-menu-users'),
  523. '#weight' => 150,
  524. );
  525. // Add link to show current authenticated/anonymous users.
  526. $links['user-counter'] = array(
  527. '#title' => admin_menu_get_user_count(),
  528. '#description' => t('Current anonymous / authenticated users'),
  529. '#weight' => -90,
  530. '#attributes' => array('class' => array('admin-menu-action', 'admin-menu-users')),
  531. '#href' => (user_access('administer users') ? 'admin/people/people' : 'user'),
  532. );
  533. return $links;
  534. }
  535. /**
  536. * Build search widget.
  537. *
  538. * @see theme_admin_menu_links()
  539. */
  540. function admin_menu_links_search() {
  541. $links = array(
  542. '#theme' => 'admin_menu_links',
  543. '#wrapper_attributes' => array('id' => 'admin-menu-search'),
  544. '#weight' => 180,
  545. );
  546. $links['search'] = array(
  547. '#type' => 'textfield',
  548. '#title' => t('Search'),
  549. '#title_display' => 'attribute',
  550. '#attributes' => array(
  551. 'placeholder' => t('Search'),
  552. 'class' => array('admin-menu-search'),
  553. ),
  554. );
  555. return $links;
  556. }
  557. /**
  558. * Form builder function for module settings.
  559. */
  560. function admin_menu_theme_settings() {
  561. $form['admin_menu_margin_top'] = array(
  562. '#type' => 'checkbox',
  563. '#title' => t('Adjust top margin'),
  564. '#default_value' => variable_get('admin_menu_margin_top', 1),
  565. '#description' => t('Shifts the site output down by approximately 20 pixels from the top of the viewport. If disabled, absolute- or fixed-positioned page elements may be covered by the administration menu.'),
  566. );
  567. $form['admin_menu_position_fixed'] = array(
  568. '#type' => 'checkbox',
  569. '#title' => t('Keep menu at top of page'),
  570. '#default_value' => variable_get('admin_menu_position_fixed', 1),
  571. '#description' => t('Displays the administration menu always at the top of the browser viewport (even when scrolling the page).'),
  572. );
  573. // @todo Re-confirm this with latest browser versions.
  574. $form['admin_menu_position_fixed']['#description'] .= '<br /><strong>' . t('In some browsers, this setting may result in a malformed page, an invisible cursor, non-selectable elements in forms, or other issues.') . '</strong>';
  575. $form['advanced'] = array(
  576. '#type' => 'vertical_tabs',
  577. '#title' => t('Advanced settings'),
  578. );
  579. $form['plugins'] = array(
  580. '#type' => 'fieldset',
  581. '#title' => t('Plugins'),
  582. '#group' => 'advanced',
  583. );
  584. $form['plugins']['admin_menu_components'] = array(
  585. '#type' => 'checkboxes',
  586. '#title' => t('Enabled components'),
  587. '#options' => array(
  588. 'admin_menu.icon' => t('Icon menu'),
  589. 'admin_menu.menu' => t('Administration menu'),
  590. 'admin_menu.search' => t('Search bar'),
  591. 'admin_menu.users' => t('User counts'),
  592. 'admin_menu.account' => t('Account links'),
  593. ),
  594. );
  595. $form['plugins']['admin_menu_components']['#default_value'] = array_keys(array_filter(variable_get('admin_menu_components', $form['plugins']['admin_menu_components']['#options'])));
  596. $process = element_info_property('checkboxes', '#process', array());
  597. $form['plugins']['admin_menu_components']['#process'] = array_merge(array('admin_menu_settings_process_components'), $process);
  598. $form['#attached']['js'][] = drupal_get_path('module', 'admin_menu') . '/admin_menu.admin.js';
  599. $form['tweaks'] = array(
  600. '#type' => 'fieldset',
  601. '#title' => t('System tweaks'),
  602. '#group' => 'advanced',
  603. );
  604. $form['tweaks']['admin_menu_tweak_modules'] = array(
  605. '#type' => 'checkbox',
  606. '#title' => t('Collapse module groups on the <a href="!modules-url">%modules</a> page', array(
  607. '%modules' => t('Modules'),
  608. '!modules-url' => url('admin/modules'),
  609. )),
  610. '#default_value' => variable_get('admin_menu_tweak_modules', 0),
  611. );
  612. if (module_exists('util')) {
  613. $form['tweaks']['admin_menu_tweak_modules']['#description'] .= '<br /><strong>' . t('If the Utility module was installed for this purpose, it can be safely disabled and uninstalled.') . '</strong>';
  614. }
  615. $form['tweaks']['admin_menu_tweak_permissions'] = array(
  616. '#type' => 'checkbox',
  617. '#title' => t('Collapse module groups on the <a href="@permissions-url">%permissions</a> page', array(
  618. '%permissions' => t('Permissions'),
  619. '@permissions-url' => url('admin/people/permissions'),
  620. )),
  621. '#default_value' => variable_get('admin_menu_tweak_permissions', 0),
  622. );
  623. $form['tweaks']['admin_menu_tweak_tabs'] = array(
  624. '#type' => 'checkbox',
  625. '#title' => t('Move local tasks into menu'),
  626. '#default_value' => variable_get('admin_menu_tweak_tabs', 0),
  627. '#description' => t('Moves the tabs on all pages into the administration menu. Only possible for themes using the CSS classes <code>tabs primary</code> and <code>tabs secondary</code>.'),
  628. );
  629. $form['performance'] = array(
  630. '#type' => 'fieldset',
  631. '#title' => t('Performance'),
  632. '#group' => 'advanced',
  633. );
  634. $form['performance']['admin_menu_cache_client'] = array(
  635. '#type' => 'checkbox',
  636. '#title' => t('Cache menu in client-side browser'),
  637. '#default_value' => variable_get('admin_menu_cache_client', 1),
  638. );
  639. return system_settings_form($form);
  640. }
  641. /**
  642. * #process callback for component plugin form element in admin_menu_theme_settings().
  643. */
  644. function admin_menu_settings_process_components($element) {
  645. // Assign 'rel' attributes to all options to achieve a live preview.
  646. // Unfortunately, #states relies on wrapping .form-wrapper classes, so it
  647. // cannot be used here.
  648. foreach ($element['#options'] as $key => $label) {
  649. if (!isset($element[$key]['#attributes']['rel'])) {
  650. $id = preg_replace('/[^a-z]/', '-', $key);
  651. $element[$key]['#attributes']['rel'] = '#' . $id;
  652. }
  653. }
  654. return $element;
  655. }
  656. /**
  657. * Form validation handler for admin_menu_theme_settings().
  658. */
  659. function admin_menu_theme_settings_validate(&$form, &$form_state) {
  660. // Change the configured components to Boolean values.
  661. foreach ($form_state['values']['admin_menu_components'] as $component => &$enabled) {
  662. $enabled = (bool) $enabled;
  663. }
  664. }
  665. /**
  666. * Implementation of hook_form_FORM_ID_alter().
  667. *
  668. * Extends Devel module with Administration menu developer settings.
  669. */
  670. function _admin_menu_form_devel_admin_settings_alter(&$form, $form_state) {
  671. // Shift system_settings_form buttons.
  672. $weight = isset($form['buttons']['#weight']) ? $form['buttons']['#weight'] : 0;
  673. $form['buttons']['#weight'] = $weight + 1;
  674. $form['admin_menu'] = array(
  675. '#type' => 'fieldset',
  676. '#title' => t('Administration menu settings'),
  677. '#collapsible' => TRUE,
  678. '#collapsed' => TRUE,
  679. );
  680. $display_options = array('mid', 'weight', 'pid');
  681. $display_options = array(0 => t('None'), 'mlid' => t('Menu link ID'), 'weight' => t('Weight'), 'plid' => t('Parent link ID'));
  682. $form['admin_menu']['admin_menu_display'] = array(
  683. '#type' => 'radios',
  684. '#title' => t('Display additional data for each menu item'),
  685. '#default_value' => variable_get('admin_menu_display', 0),
  686. '#options' => $display_options,
  687. '#description' => t('Display the selected items next to each menu item link.'),
  688. );
  689. $form['admin_menu']['admin_menu_show_all'] = array(
  690. '#type' => 'checkbox',
  691. '#title' => t('Display all menu items'),
  692. '#default_value' => variable_get('admin_menu_show_all', 0),
  693. '#description' => t('If enabled, all menu items are displayed regardless of your site permissions. <em>Note: Do not enable on a production site.</em>'),
  694. );
  695. }
  696. /**
  697. * Flush all caches or a specific one.
  698. *
  699. * @param $name
  700. * (optional) Name of cache to flush.
  701. */
  702. function admin_menu_flush_cache($name = NULL) {
  703. if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], current_path())) {
  704. return MENU_ACCESS_DENIED;
  705. }
  706. if (isset($name)) {
  707. $caches = module_invoke_all('admin_menu_cache_info');
  708. if (!isset($caches[$name])) {
  709. return MENU_NOT_FOUND;
  710. }
  711. }
  712. else {
  713. $caches[$name] = array(
  714. 'title' => t('Every'),
  715. 'callback' => 'drupal_flush_all_caches',
  716. );
  717. }
  718. // Pass the cache to flush forward to the callback.
  719. $function = $caches[$name]['callback'];
  720. $function($name);
  721. drupal_set_message(t('!title cache cleared.', array('!title' => $caches[$name]['title'])));
  722. // The JavaScript injects a destination request parameter pointing to the
  723. // originating page, so the user is redirected back to that page. Without
  724. // destination parameter, the redirect ends on the front page.
  725. drupal_goto();
  726. }
  727. /**
  728. * Implements hook_admin_menu_cache_info().
  729. */
  730. function admin_menu_admin_menu_cache_info() {
  731. $caches['admin_menu'] = array(
  732. 'title' => t('Administration menu'),
  733. 'callback' => '_admin_menu_flush_cache',
  734. );
  735. return $caches;
  736. }
  737. /**
  738. * Implements hook_admin_menu_cache_info() on behalf of System module.
  739. */
  740. function system_admin_menu_cache_info() {
  741. $caches = array(
  742. 'assets' => t('CSS and JavaScript'),
  743. 'cache' => t('Page and else'),
  744. 'menu' => t('Menu'),
  745. 'registry' => t('Class registry'),
  746. 'theme' => t('Theme registry'),
  747. );
  748. foreach ($caches as $name => $cache) {
  749. $caches[$name] = array(
  750. 'title' => $cache,
  751. 'callback' => '_admin_menu_flush_cache',
  752. );
  753. }
  754. return $caches;
  755. }
  756. /**
  757. * Implements hook_admin_menu_cache_info() on behalf of Update module.
  758. */
  759. function update_admin_menu_cache_info() {
  760. $caches['update'] = array(
  761. 'title' => t('Update data'),
  762. 'callback' => '_update_cache_clear',
  763. );
  764. return $caches;
  765. }
  766. /**
  767. * Flush all caches or a specific one.
  768. *
  769. * @param $name
  770. * (optional) Name of cache to flush.
  771. *
  772. * @see system_admin_menu_cache_info()
  773. */
  774. function _admin_menu_flush_cache($name = NULL) {
  775. switch ($name) {
  776. case 'admin_menu':
  777. admin_menu_flush_caches();
  778. break;
  779. case 'menu':
  780. menu_rebuild();
  781. break;
  782. case 'registry':
  783. registry_rebuild();
  784. // Fall-through to clear cache tables, since registry information is
  785. // usually the base for other data that is cached (e.g. SimpleTests).
  786. case 'cache':
  787. // Don't clear cache_form - in-progress form submissions may break.
  788. // Ordered so clearing the page cache will always be the last action.
  789. // @see drupal_flush_all_caches()
  790. $core = array('cache', 'cache_bootstrap', 'cache_filter', 'cache_page');
  791. $cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
  792. foreach ($cache_tables as $table) {
  793. cache_clear_all('*', $table, TRUE);
  794. }
  795. break;
  796. case 'assets':
  797. // Change query-strings on css/js files to enforce reload for all users.
  798. _drupal_flush_css_js();
  799. drupal_clear_css_cache();
  800. drupal_clear_js_cache();
  801. // Clear the page cache, since cached HTML pages might link to old CSS and
  802. // JS aggregates.
  803. cache_clear_all('*', 'cache_page', TRUE);
  804. break;
  805. case 'theme':
  806. system_rebuild_theme_data();
  807. drupal_theme_rebuild();
  808. break;
  809. }
  810. }
  811. /**
  812. * Preprocesses variables for theme_admin_menu_icon().
  813. */
  814. function template_preprocess_admin_menu_icon(&$variables) {
  815. // Image source might have been passed in as theme variable.
  816. if (!isset($variables['src'])) {
  817. if (theme_get_setting('toggle_favicon')) {
  818. $variables['src'] = theme_get_setting('favicon');
  819. }
  820. else {
  821. $variables['src'] = base_path() . 'misc/favicon.ico';
  822. }
  823. }
  824. // Strip the protocol without delimiters for transient HTTP/HTTPS support.
  825. // Since the menu is cached on the server-side and client-side, the cached
  826. // version might contain a HTTP link, whereas the actual page is on HTTPS.
  827. // Relative paths will work fine, but theme_get_setting() returns an
  828. // absolute URI.
  829. $variables['src'] = preg_replace('@^https?:@', '', $variables['src']);
  830. $variables['src'] = check_plain($variables['src']);
  831. $variables['alt'] = t('Home');
  832. }
  833. /**
  834. * Renders an icon to display in the administration menu.
  835. *
  836. * @ingroup themeable
  837. */
  838. function theme_admin_menu_icon($variables) {
  839. return '<img class="admin-menu-icon" src="' . $variables['src'] . '" width="16" height="16" alt="' . $variables['alt'] . '" />';
  840. }