admin_menu.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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. $function = key($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(
  257. $key => array(
  258. 'link' => $router_item,
  259. 'below' => array(),
  260. ),
  261. );
  262. }
  263. return array();
  264. }
  265. /**
  266. * Create the cartesian product of multiple varying sized argument arrays.
  267. *
  268. * @param $arguments
  269. * A two dimensional array of arguments.
  270. *
  271. * @see hook_admin_menu_map()
  272. */
  273. function admin_menu_expand_args($arguments) {
  274. $replacements = array();
  275. // Initialize line cursors, move out array keys (placeholders) and assign
  276. // numeric keys instead.
  277. $i = 0;
  278. $placeholders = array();
  279. $new_arguments = array();
  280. foreach ($arguments as $placeholder => $values) {
  281. // Skip empty arguments.
  282. if (empty($values)) {
  283. continue;
  284. }
  285. $cursor[$i] = 0;
  286. $placeholders[$i] = $placeholder;
  287. $new_arguments[$i] = $values;
  288. $i++;
  289. }
  290. $arguments = $new_arguments;
  291. unset($new_arguments);
  292. if ($rows = count($arguments)) {
  293. do {
  294. // Collect current argument from each row.
  295. $row = array();
  296. for ($i = 0; $i < $rows; ++$i) {
  297. $row[$placeholders[$i]] = $arguments[$i][$cursor[$i]];
  298. }
  299. $replacements[] = $row;
  300. // Increment cursor position.
  301. $j = $rows - 1;
  302. $cursor[$j]++;
  303. while (!array_key_exists($cursor[$j], $arguments[$j])) {
  304. // No more arguments left: reset cursor, go to next line and increment
  305. // that cursor instead. Repeat until argument found or out of rows.
  306. $cursor[$j] = 0;
  307. if (--$j < 0) {
  308. // We're done.
  309. break 2;
  310. }
  311. $cursor[$j]++;
  312. }
  313. } while (1);
  314. }
  315. return $replacements;
  316. }
  317. /**
  318. * Build the administration menu as renderable menu links.
  319. *
  320. * @param $tree
  321. * A data structure representing the administration menu tree as returned from
  322. * menu_tree_all_data().
  323. *
  324. * @return
  325. * The complete administration menu, suitable for theme_admin_menu_links().
  326. *
  327. * @see theme_admin_menu_links()
  328. * @see admin_menu_menu_alter()
  329. */
  330. function admin_menu_links_menu($tree) {
  331. $links = array();
  332. foreach ($tree as $data) {
  333. // Skip items that are inaccessible, invisible, or link to their parent.
  334. // (MENU_DEFAULT_LOCAL_TASK), and MENU_CALLBACK-alike items that should only
  335. // appear in the breadcrumb.
  336. if (!$data['link']['access'] || $data['link']['type'] & MENU_LINKS_TO_PARENT || $data['link']['type'] == MENU_VISIBLE_IN_BREADCRUMB || $data['link']['hidden'] == 1) {
  337. continue;
  338. }
  339. // Hide 'Administer' and make child links appear on this level.
  340. // @todo Make this configurable.
  341. if ($data['link']['router_path'] == 'admin') {
  342. if ($data['below']) {
  343. $links = array_merge($links, admin_menu_links_menu($data['below']));
  344. }
  345. continue;
  346. }
  347. // Omit alias lookups.
  348. $data['link']['localized_options']['alias'] = TRUE;
  349. // Remove description to prevent mouseover tooltip clashes.
  350. unset($data['link']['localized_options']['attributes']['title']);
  351. // Make action links (typically "Add ...") appear first in dropdowns.
  352. // They might appear first already, but only as long as there is no link
  353. // that comes alphabetically first (e.g., a node type with label "Ad").
  354. if ($data['link']['type'] & MENU_IS_LOCAL_ACTION) {
  355. $data['link']['weight'] -= 1000;
  356. }
  357. $links[$data['link']['href']] = array(
  358. '#title' => $data['link']['title'],
  359. '#href' => $data['link']['href'],
  360. '#options' => $data['link']['localized_options'],
  361. '#weight' => $data['link']['weight'],
  362. );
  363. // Recurse to add any child links.
  364. $children = array();
  365. if ($data['below']) {
  366. $children = admin_menu_links_menu($data['below']);
  367. $links[$data['link']['href']] += $children;
  368. }
  369. // Handle links pointing to category/overview pages.
  370. if ($data['link']['page_callback'] == 'system_admin_menu_block_page' || $data['link']['page_callback'] == 'system_admin_config_page') {
  371. // Apply a marker for others to consume.
  372. $links[$data['link']['href']]['#is_category'] = TRUE;
  373. // Automatically hide empty categories.
  374. // Check for empty children first for performance. Only when non-empty
  375. // (typically 'admin/config'), check whether children are accessible.
  376. if (empty($children) || !element_get_visible_children($children)) {
  377. $links[$data['link']['href']]['#access'] = FALSE;
  378. }
  379. }
  380. }
  381. return $links;
  382. }
  383. /**
  384. * Build icon menu links; mostly containing maintenance helpers.
  385. *
  386. * @see theme_admin_menu_links()
  387. */
  388. function admin_menu_links_icon() {
  389. $destination = drupal_get_destination();
  390. $links = array(
  391. '#theme' => 'admin_menu_links',
  392. '#wrapper_attributes' => array('id' => 'admin-menu-icon'),
  393. '#weight' => -100,
  394. );
  395. $links['icon'] = array(
  396. '#title' => theme('admin_menu_icon'),
  397. '#attributes' => array('class' => array('admin-menu-icon')),
  398. '#href' => '<front>',
  399. '#options' => array(
  400. 'html' => TRUE,
  401. ),
  402. );
  403. // Add link to manually run cron.
  404. $links['icon']['cron'] = array(
  405. '#title' => t('Run cron'),
  406. '#weight' => 50,
  407. '#access' => user_access('administer site configuration'),
  408. '#href' => 'admin/reports/status/run-cron',
  409. );
  410. // Add link to run update.php.
  411. $links['icon']['update'] = array(
  412. '#title' => t('Run updates'),
  413. '#weight' => 50,
  414. // @see update_access_allowed()
  415. '#access' => $GLOBALS['user']->uid == 1 || !empty($GLOBALS['update_free_access']) || user_access('administer software updates'),
  416. '#href' => base_path() . 'update.php',
  417. '#options' => array(
  418. 'external' => TRUE,
  419. ),
  420. );
  421. // Add link to drupal.org.
  422. $links['icon']['drupal.org'] = array(
  423. '#title' => 'Drupal.org',
  424. '#weight' => 100,
  425. '#access' => user_access('display drupal links'),
  426. '#href' => 'http://drupal.org',
  427. );
  428. if (variable_get('admin_menu_issue_queues', TRUE)) {
  429. // Add links to project issue queues.
  430. foreach (module_list(FALSE, TRUE) as $module) {
  431. $info = drupal_parse_info_file(drupal_get_path('module', $module) . '/' . $module . '.info');
  432. if (!isset($info['project']) || isset($links['icon']['drupal.org'][$info['project']])) {
  433. continue;
  434. }
  435. $links['icon']['drupal.org'][$info['project']] = array(
  436. '#title' => t('@project issue queue', array('@project' => $info['name'])),
  437. '#weight' => ($info['project'] == 'drupal' ? -10 : 0),
  438. '#href' => 'http://drupal.org/project/issues/' . $info['project'],
  439. '#options' => array(
  440. 'query' => array('version' => (isset($info['core']) ? $info['core'] : 'All')),
  441. ),
  442. );
  443. }
  444. }
  445. // Add items to flush caches.
  446. $links['icon']['flush-cache'] = array(
  447. '#title' => t('Flush all caches'),
  448. '#weight' => 20,
  449. '#access' => user_access('flush caches'),
  450. '#href' => 'admin_menu/flush-cache',
  451. '#options' => array(
  452. 'query' => $destination + array('token' => drupal_get_token('admin_menu/flush-cache')),
  453. ),
  454. );
  455. $caches = module_invoke_all('admin_menu_cache_info');
  456. foreach ($caches as $name => $cache) {
  457. $links['icon']['flush-cache'][$name] = array(
  458. '#title' => $cache['title'],
  459. '#href' => 'admin_menu/flush-cache/' . $name,
  460. '#options' => array(
  461. 'query' => $destination + array('token' => drupal_get_token('admin_menu/flush-cache/' . $name)),
  462. ),
  463. );
  464. }
  465. // Add Devel module menu links.
  466. if (module_exists('devel')) {
  467. $devel_tree = menu_build_tree('devel');
  468. $devel_links = admin_menu_links_menu($devel_tree);
  469. if (element_get_visible_children($devel_links)) {
  470. $links['icon']['devel'] = array(
  471. '#title' => t('Development'),
  472. '#weight' => 30,
  473. ) + $devel_links;
  474. }
  475. }
  476. return $links;
  477. }
  478. /**
  479. * Builds the account links.
  480. *
  481. * @see theme_admin_menu_links()
  482. */
  483. function admin_menu_links_account() {
  484. $links = array(
  485. '#theme' => 'admin_menu_links',
  486. '#wrapper_attributes' => array('id' => 'admin-menu-account'),
  487. '#weight' => 100,
  488. );
  489. $links['account'] = array(
  490. '#title' => format_username($GLOBALS['user']),
  491. '#weight' => -99,
  492. '#attributes' => array('class' => array('admin-menu-action', 'admin-menu-account')),
  493. '#href' => 'user/' . $GLOBALS['user']->uid,
  494. );
  495. $links['logout'] = array(
  496. '#title' => t('Log out'),
  497. '#weight' => -100,
  498. '#attributes' => array('class' => array('admin-menu-action')),
  499. '#href' => 'user/logout',
  500. );
  501. // Add Devel module switch user links.
  502. $switch_links = module_invoke('devel', 'switch_user_list');
  503. if (!empty($switch_links) && count($switch_links) > 1) {
  504. foreach ($switch_links as $uid => $link) {
  505. $links['account'][$link['title']] = array(
  506. '#title' => $link['title'],
  507. '#description' => $link['attributes']['title'],
  508. '#href' => $link['href'],
  509. '#options' => array(
  510. 'query' => $link['query'],
  511. 'html' => !empty($link['html']),
  512. ),
  513. );
  514. }
  515. }
  516. return $links;
  517. }
  518. /**
  519. * Builds user counter.
  520. *
  521. * @see theme_admin_menu_links()
  522. */
  523. function admin_menu_links_users() {
  524. $links = array(
  525. '#theme' => 'admin_menu_links',
  526. '#wrapper_attributes' => array('id' => 'admin-menu-users'),
  527. '#weight' => 150,
  528. );
  529. // Add link to show current authenticated/anonymous users.
  530. $links['user-counter'] = array(
  531. '#title' => admin_menu_get_user_count(),
  532. '#description' => t('Current anonymous / authenticated users'),
  533. '#weight' => -90,
  534. '#attributes' => array('class' => array('admin-menu-action', 'admin-menu-users')),
  535. '#href' => (user_access('administer users') ? 'admin/people' : 'user'),
  536. );
  537. return $links;
  538. }
  539. /**
  540. * Build search widget.
  541. *
  542. * @see theme_admin_menu_links()
  543. */
  544. function admin_menu_links_search() {
  545. $links = array(
  546. '#theme' => 'admin_menu_links',
  547. '#wrapper_attributes' => array('id' => 'admin-menu-search'),
  548. '#weight' => 180,
  549. );
  550. $links['search'] = array(
  551. '#type' => 'textfield',
  552. '#title' => t('Search'),
  553. '#title_display' => 'attribute',
  554. '#attributes' => array(
  555. 'placeholder' => t('Search'),
  556. 'class' => array('admin-menu-search'),
  557. ),
  558. );
  559. return $links;
  560. }
  561. /**
  562. * Form builder function for module settings.
  563. */
  564. function admin_menu_theme_settings() {
  565. $form['admin_menu_margin_top'] = array(
  566. '#type' => 'checkbox',
  567. '#title' => t('Adjust top margin'),
  568. '#default_value' => variable_get('admin_menu_margin_top', 1),
  569. '#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.'),
  570. );
  571. $form['admin_menu_position_fixed'] = array(
  572. '#type' => 'checkbox',
  573. '#title' => t('Keep menu at top of page'),
  574. '#default_value' => variable_get('admin_menu_position_fixed', 1),
  575. '#description' => t('Displays the administration menu always at the top of the browser viewport (even when scrolling the page).'),
  576. );
  577. // @todo Re-confirm this with latest browser versions.
  578. $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>';
  579. $form['advanced'] = array(
  580. '#type' => 'vertical_tabs',
  581. '#title' => t('Advanced settings'),
  582. );
  583. $form['plugins'] = array(
  584. '#type' => 'fieldset',
  585. '#title' => t('Plugins'),
  586. '#group' => 'advanced',
  587. );
  588. $form['plugins']['admin_menu_components'] = array(
  589. '#type' => 'checkboxes',
  590. '#title' => t('Enabled components'),
  591. '#options' => array(
  592. 'admin_menu.icon' => t('Icon menu'),
  593. 'admin_menu.menu' => t('Administration menu'),
  594. 'admin_menu.search' => t('Search bar'),
  595. 'admin_menu.users' => t('User counts'),
  596. 'admin_menu.account' => t('Account links'),
  597. ),
  598. );
  599. $form['plugins']['admin_menu_components']['#default_value'] = array_keys(array_filter(variable_get('admin_menu_components', $form['plugins']['admin_menu_components']['#options'])));
  600. $process = element_info_property('checkboxes', '#process', array());
  601. $form['plugins']['admin_menu_components']['#process'] = array_merge(array('admin_menu_settings_process_components'), $process);
  602. $form['#attached']['js'][] = drupal_get_path('module', 'admin_menu') . '/admin_menu.admin.js';
  603. $form['tweaks'] = array(
  604. '#type' => 'fieldset',
  605. '#title' => t('System tweaks'),
  606. '#group' => 'advanced',
  607. );
  608. $form['tweaks']['admin_menu_tweak_modules'] = array(
  609. '#type' => 'checkbox',
  610. '#title' => t('Collapse module groups on the <a href="!modules-url">%modules</a> page', array(
  611. '%modules' => t('Modules'),
  612. '!modules-url' => url('admin/modules'),
  613. )),
  614. '#default_value' => variable_get('admin_menu_tweak_modules', 0),
  615. );
  616. if (module_exists('util')) {
  617. $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>';
  618. }
  619. $form['tweaks']['admin_menu_tweak_permissions'] = array(
  620. '#type' => 'checkbox',
  621. '#title' => t('Collapse module groups on the <a href="@permissions-url">%permissions</a> page', array(
  622. '%permissions' => t('Permissions'),
  623. '@permissions-url' => url('admin/people/permissions'),
  624. )),
  625. '#default_value' => variable_get('admin_menu_tweak_permissions', 0),
  626. );
  627. $form['tweaks']['admin_menu_tweak_tabs'] = array(
  628. '#type' => 'checkbox',
  629. '#title' => t('Move local tasks into menu'),
  630. '#default_value' => variable_get('admin_menu_tweak_tabs', 0),
  631. '#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>.'),
  632. );
  633. $form['performance'] = array(
  634. '#type' => 'fieldset',
  635. '#title' => t('Performance'),
  636. '#group' => 'advanced',
  637. );
  638. $form['performance']['admin_menu_cache_client'] = array(
  639. '#type' => 'checkbox',
  640. '#title' => t('Cache menu in client-side browser'),
  641. '#default_value' => variable_get('admin_menu_cache_client', 1),
  642. );
  643. $form['performance']['admin_menu_issue_queues'] = array(
  644. '#type' => 'checkbox',
  645. '#title' => t('Show Issue Queue links in icon menu'),
  646. '#default_value' => variable_get('admin_menu_issue_queues', 1),
  647. );
  648. return system_settings_form($form);
  649. }
  650. /**
  651. * #process callback for component plugin form element in admin_menu_theme_settings().
  652. */
  653. function admin_menu_settings_process_components($element) {
  654. // Assign 'rel' attributes to all options to achieve a live preview.
  655. // Unfortunately, #states relies on wrapping .form-wrapper classes, so it
  656. // cannot be used here.
  657. foreach ($element['#options'] as $key => $label) {
  658. if (!isset($element[$key]['#attributes']['rel'])) {
  659. $id = preg_replace('/[^a-z]/', '-', $key);
  660. $element[$key]['#attributes']['rel'] = '#' . $id;
  661. }
  662. }
  663. return $element;
  664. }
  665. /**
  666. * Form validation handler for admin_menu_theme_settings().
  667. */
  668. function admin_menu_theme_settings_validate(&$form, &$form_state) {
  669. // Change the configured components to Boolean values.
  670. foreach ($form_state['values']['admin_menu_components'] as $component => &$enabled) {
  671. $enabled = (bool) $enabled;
  672. }
  673. }
  674. /**
  675. * Implementation of hook_form_FORM_ID_alter().
  676. *
  677. * Extends Devel module with Administration menu developer settings.
  678. */
  679. function _admin_menu_form_devel_admin_settings_alter(&$form, $form_state) {
  680. // Shift system_settings_form buttons.
  681. $weight = isset($form['buttons']['#weight']) ? $form['buttons']['#weight'] : 0;
  682. $form['buttons']['#weight'] = $weight + 1;
  683. $form['admin_menu'] = array(
  684. '#type' => 'fieldset',
  685. '#title' => t('Administration menu settings'),
  686. '#collapsible' => TRUE,
  687. '#collapsed' => TRUE,
  688. );
  689. $display_options = array('mid', 'weight', 'pid');
  690. $display_options = array(0 => t('None'), 'mlid' => t('Menu link ID'), 'weight' => t('Weight'), 'plid' => t('Parent link ID'));
  691. $form['admin_menu']['admin_menu_display'] = array(
  692. '#type' => 'radios',
  693. '#title' => t('Display additional data for each menu item'),
  694. '#default_value' => variable_get('admin_menu_display', 0),
  695. '#options' => $display_options,
  696. '#description' => t('Display the selected items next to each menu item link.'),
  697. );
  698. $form['admin_menu']['admin_menu_show_all'] = array(
  699. '#type' => 'checkbox',
  700. '#title' => t('Display all menu items'),
  701. '#default_value' => variable_get('admin_menu_show_all', 0),
  702. '#description' => t('If enabled, all menu items are displayed regardless of your site permissions. <em>Note: Do not enable on a production site.</em>'),
  703. );
  704. }
  705. /**
  706. * Flush all caches or a specific one.
  707. *
  708. * @param $name
  709. * (optional) Name of cache to flush.
  710. */
  711. function admin_menu_flush_cache($name = NULL) {
  712. if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], current_path())) {
  713. return MENU_ACCESS_DENIED;
  714. }
  715. if (isset($name)) {
  716. $caches = module_invoke_all('admin_menu_cache_info');
  717. if (!isset($caches[$name])) {
  718. return MENU_NOT_FOUND;
  719. }
  720. $message = t('@title cache cleared.', array('@title' => $caches[$name]['title']));
  721. }
  722. else {
  723. $caches[$name] = array(
  724. 'title' => t('Every'),
  725. 'callback' => 'drupal_flush_all_caches',
  726. );
  727. $message = t('All caches cleared.');
  728. }
  729. // Pass the cache to flush forward to the callback.
  730. $function = $caches[$name]['callback'];
  731. $function($name);
  732. drupal_set_message($message);
  733. // The JavaScript injects a destination request parameter pointing to the
  734. // originating page, so the user is redirected back to that page. Without
  735. // destination parameter, the redirect ends on the front page.
  736. drupal_goto();
  737. }
  738. /**
  739. * Implements hook_admin_menu_cache_info().
  740. */
  741. function admin_menu_admin_menu_cache_info() {
  742. $caches['admin_menu'] = array(
  743. 'title' => t('Administration menu'),
  744. 'callback' => '_admin_menu_flush_cache',
  745. );
  746. return $caches;
  747. }
  748. /**
  749. * Implements hook_admin_menu_cache_info() on behalf of System module.
  750. */
  751. function system_admin_menu_cache_info() {
  752. $caches = array(
  753. 'assets' => t('CSS and JavaScript'),
  754. 'cache' => t('Page and else'),
  755. 'menu' => t('Menu'),
  756. 'registry' => t('Class registry'),
  757. 'theme' => t('Theme registry'),
  758. );
  759. foreach ($caches as $name => $cache) {
  760. $caches[$name] = array(
  761. 'title' => $cache,
  762. 'callback' => '_admin_menu_flush_cache',
  763. );
  764. }
  765. return $caches;
  766. }
  767. /**
  768. * Implements hook_admin_menu_cache_info() on behalf of Update module.
  769. */
  770. function update_admin_menu_cache_info() {
  771. $caches['update'] = array(
  772. 'title' => t('Update data'),
  773. 'callback' => '_update_cache_clear',
  774. );
  775. return $caches;
  776. }
  777. /**
  778. * Flush all caches or a specific one.
  779. *
  780. * @param $name
  781. * (optional) Name of cache to flush.
  782. *
  783. * @see system_admin_menu_cache_info()
  784. */
  785. function _admin_menu_flush_cache($name = NULL) {
  786. switch ($name) {
  787. case 'admin_menu':
  788. admin_menu_flush_caches();
  789. break;
  790. case 'menu':
  791. menu_rebuild();
  792. break;
  793. case 'registry':
  794. registry_rebuild();
  795. // Fall-through to clear cache tables, since registry information is
  796. // usually the base for other data that is cached (e.g. SimpleTests).
  797. case 'cache':
  798. // Don't clear cache_form - in-progress form submissions may break.
  799. // Ordered so clearing the page cache will always be the last action.
  800. // @see drupal_flush_all_caches()
  801. $core = array('cache', 'cache_bootstrap', 'cache_filter', 'cache_page');
  802. $cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
  803. foreach ($cache_tables as $table) {
  804. cache_clear_all('*', $table, TRUE);
  805. }
  806. break;
  807. case 'assets':
  808. // Change query-strings on css/js files to enforce reload for all users.
  809. _drupal_flush_css_js();
  810. drupal_clear_css_cache();
  811. drupal_clear_js_cache();
  812. // Clear the page cache, since cached HTML pages might link to old CSS and
  813. // JS aggregates.
  814. cache_clear_all('*', 'cache_page', TRUE);
  815. break;
  816. case 'theme':
  817. system_rebuild_theme_data();
  818. drupal_theme_rebuild();
  819. break;
  820. }
  821. }
  822. /**
  823. * Preprocesses variables for theme_admin_menu_icon().
  824. */
  825. function template_preprocess_admin_menu_icon(&$variables) {
  826. // Image source might have been passed in as theme variable.
  827. if (!isset($variables['src'])) {
  828. if (theme_get_setting('toggle_favicon')) {
  829. $variables['src'] = theme_get_setting('favicon');
  830. }
  831. else {
  832. $variables['src'] = base_path() . 'misc/favicon.ico';
  833. }
  834. }
  835. // Strip the protocol without delimiters for transient HTTP/HTTPS support.
  836. // Since the menu is cached on the server-side and client-side, the cached
  837. // version might contain a HTTP link, whereas the actual page is on HTTPS.
  838. // Relative paths will work fine, but theme_get_setting() returns an
  839. // absolute URI.
  840. $variables['src'] = preg_replace('@^https?:@', '', $variables['src']);
  841. $variables['src'] = check_plain($variables['src']);
  842. $variables['alt'] = t('Home');
  843. }
  844. /**
  845. * Renders an icon to display in the administration menu.
  846. *
  847. * @ingroup themeable
  848. */
  849. function theme_admin_menu_icon($variables) {
  850. return '<img class="admin-menu-icon" src="' . $variables['src'] . '" width="16" height="16" alt="' . $variables['alt'] . '" />';
  851. }