menu.module

You are here

  1. 7 modules/menu/menu.module
  2. 4.6 modules/menu.module
  3. 4.7 modules/menu.module
  4. 5 modules/menu/menu.module
  5. 6 modules/menu/menu.module

Allows administrators to customize the site navigation menu.

Functions

Namesort descending Description
menu_block Implementation of hook_block().
menu_enable Implementation of hook_enable()
menu_form_alter Implementation of hook_form_alter(). Adds menu item fields to the node form.
menu_get_menus Return an associative array of the custom menus names.
menu_help Implementation of hook_help().
menu_load Load the data for a single custom menu.
menu_menu Implementation of hook_menu().
menu_nodeapi Implementation of hook_nodeapi().
menu_node_form_submit Decompose the selected menu parent option into the menu_name and plid.
menu_overview_title Title callback for the menu overview page and links.
menu_parent_options Return a list of menu items that are valid possible parents for the given menu item.
menu_perm Implementation of hook_perm().
menu_reset_item Reset a system-defined menu item.
menu_theme Implemenation of hook_theme().
_menu_parents_recurse Recursive helper function for menu_parent_options().
_menu_parent_depth_limit Find the depth limit for items in the parent select.

Constants

Namesort descending Description
MENU_MAX_MENU_NAME_LENGTH_UI Maximum length of menu name as entered by the user. Database length is 32 and we add a menu- prefix.

File

modules/menu/menu.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Allows administrators to customize the site navigation menu.
  5. */
  6. /**
  7. * Maximum length of menu name as entered by the user. Database length is 32
  8. * and we add a menu- prefix.
  9. */
  10. define('MENU_MAX_MENU_NAME_LENGTH_UI', 27);
  11. /**
  12. * Implementation of hook_help().
  13. */
  14. function menu_help($path, $arg) {
  15. switch ($path) {
  16. case 'admin/help#menu':
  17. $output = '<p>'. t("The menu module provides an interface to control and customize Drupal's powerful menu system. Menus are a hierarchical collection of links, or menu items, used to navigate a website, and are positioned and displayed using Drupal's flexible block system. By default, three menus are created during installation: <em>Navigation</em>, <em>Primary links</em>, and <em>Secondary links</em>. The <em>Navigation</em> menu contains most links necessary for working with and navigating your site, and is often displayed in either the left or right sidebar. Most Drupal themes also provide support for <em>Primary links</em> and <em>Secondary links</em>, by displaying them in either the header or footer of each page. By default, <em>Primary links</em> and <em>Secondary links</em> contain no menu items but may be configured to contain custom menu items specific to your site.") .'</p>';
  18. $output .= '<p>'. t('The <a href="@menu">menus page</a> displays all menus currently available on your site. Select a menu from this list to add or edit a menu item, or to rearrange items within the menu. Create new menus using the <a href="@add-menu">add menu page</a> (the block containing a new menu must also be enabled on the <a href="@blocks">blocks administration page</a>).', array('@menu' => url('admin/build/menu'), '@add-menu' => url('admin/build/menu/add'), '@blocks' => url('admin/build/block'))) .'</p>';
  19. $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@menu">Menu module</a>.', array('@menu' => 'http://drupal.org/handbook/modules/menu/')) .'</p>';
  20. return $output;
  21. case 'admin/build/menu':
  22. return '<p>'. t('Menus are a collection of links (menu items) used to navigate a website. The menus currently available on your site are displayed below. Select a menu from this list to manage its menu items.') .'</p>';
  23. case 'admin/build/menu/add':
  24. return '<p>'. t('Enter the name for your new menu. Remember to enable the newly created block in the <a href="@blocks">blocks administration page</a>.', array('@blocks' => url('admin/build/block'))) .'</p>';
  25. case 'admin/build/menu-customize/%':
  26. return '<p>'. t('To rearrange menu items, grab a drag-and-drop handle under the <em>Menu item</em> column and drag the items (or group of items) to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Remember that your changes will not be saved until you click the <em>Save configuration</em> button at the bottom of the page.') .'</p>';
  27. case 'admin/build/menu/item/add':
  28. return '<p>'. t('Enter the title and path for your new menu item.') .'</p>';
  29. }
  30. }
  31. /**
  32. * Implementation of hook_perm().
  33. */
  34. function menu_perm() {
  35. return array('administer menu');
  36. }
  37. /**
  38. * Implementation of hook_menu().
  39. */
  40. function menu_menu() {
  41. $items['admin/build/menu'] = array(
  42. 'title' => 'Menus',
  43. 'description' => "Control your site's navigation menu, primary links and secondary links, as well as rename and reorganize menu items.",
  44. 'page callback' => 'menu_overview_page',
  45. 'access callback' => 'user_access',
  46. 'access arguments' => array('administer menu'),
  47. 'file' => 'menu.admin.inc',
  48. );
  49. $items['admin/build/menu/list'] = array(
  50. 'title' => 'List menus',
  51. 'type' => MENU_DEFAULT_LOCAL_TASK,
  52. 'weight' => -10,
  53. 'file' => 'menu.admin.inc',
  54. );
  55. $items['admin/build/menu/add'] = array(
  56. 'title' => 'Add menu',
  57. 'page callback' => 'drupal_get_form',
  58. 'page arguments' => array('menu_edit_menu', 'add'),
  59. 'access arguments' => array('administer menu'),
  60. 'type' => MENU_LOCAL_TASK,
  61. 'file' => 'menu.admin.inc',
  62. );
  63. $items['admin/build/menu/settings'] = array(
  64. 'title' => 'Settings',
  65. 'page callback' => 'drupal_get_form',
  66. 'page arguments' => array('menu_configure'),
  67. 'access arguments' => array('administer menu'),
  68. 'type' => MENU_LOCAL_TASK,
  69. 'weight' => 5,
  70. 'file' => 'menu.admin.inc',
  71. );
  72. $items['admin/build/menu-customize/%menu'] = array(
  73. 'title' => 'Customize menu',
  74. 'page callback' => 'drupal_get_form',
  75. 'page arguments' => array('menu_overview_form', 3),
  76. 'title callback' => 'menu_overview_title',
  77. 'title arguments' => array(3),
  78. 'access arguments' => array('administer menu'),
  79. 'type' => MENU_CALLBACK,
  80. 'file' => 'menu.admin.inc',
  81. );
  82. $items['admin/build/menu-customize/%menu/list'] = array(
  83. 'title' => 'List items',
  84. 'weight' => -10,
  85. 'type' => MENU_DEFAULT_LOCAL_TASK,
  86. 'file' => 'menu.admin.inc',
  87. );
  88. $items['admin/build/menu-customize/%menu/add'] = array(
  89. 'title' => 'Add item',
  90. 'page callback' => 'drupal_get_form',
  91. 'page arguments' => array('menu_edit_item', 'add', NULL, 3),
  92. 'access arguments' => array('administer menu'),
  93. 'type' => MENU_LOCAL_TASK,
  94. 'file' => 'menu.admin.inc',
  95. );
  96. $items['admin/build/menu-customize/%menu/edit'] = array(
  97. 'title' => 'Edit menu',
  98. 'page callback' => 'drupal_get_form',
  99. 'page arguments' => array('menu_edit_menu', 'edit', 3),
  100. 'access arguments' => array('administer menu'),
  101. 'type' => MENU_LOCAL_TASK,
  102. 'file' => 'menu.admin.inc',
  103. );
  104. $items['admin/build/menu-customize/%menu/delete'] = array(
  105. 'title' => 'Delete menu',
  106. 'page callback' => 'menu_delete_menu_page',
  107. 'page arguments' => array(3),
  108. 'access arguments' => array('administer menu'),
  109. 'type' => MENU_CALLBACK,
  110. 'file' => 'menu.admin.inc',
  111. );
  112. $items['admin/build/menu/item/%menu_link/edit'] = array(
  113. 'title' => 'Edit menu item',
  114. 'page callback' => 'drupal_get_form',
  115. 'page arguments' => array('menu_edit_item', 'edit', 4, NULL),
  116. 'access arguments' => array('administer menu'),
  117. 'type' => MENU_CALLBACK,
  118. 'file' => 'menu.admin.inc',
  119. );
  120. $items['admin/build/menu/item/%menu_link/reset'] = array(
  121. 'title' => 'Reset menu item',
  122. 'page callback' => 'drupal_get_form',
  123. 'page arguments' => array('menu_reset_item_confirm', 4),
  124. 'access arguments' => array('administer menu'),
  125. 'type' => MENU_CALLBACK,
  126. 'file' => 'menu.admin.inc',
  127. );
  128. $items['admin/build/menu/item/%menu_link/delete'] = array(
  129. 'title' => 'Delete menu item',
  130. 'page callback' => 'menu_item_delete_page',
  131. 'page arguments' => array(4),
  132. 'access arguments' => array('administer menu'),
  133. 'type' => MENU_CALLBACK,
  134. 'file' => 'menu.admin.inc',
  135. );
  136. return $items;
  137. }
  138. /**
  139. * Implemenation of hook_theme().
  140. */
  141. function menu_theme() {
  142. return array(
  143. 'menu_overview_form' => array(
  144. 'file' => 'menu.admin.inc',
  145. 'arguments' => array('form' => NULL),
  146. ),
  147. );
  148. }
  149. /**
  150. * Implementation of hook_enable()
  151. *
  152. * Add a link for each custom menu.
  153. */
  154. function menu_enable() {
  155. menu_rebuild();
  156. $base_link = db_fetch_array(db_query("SELECT mlid AS plid, menu_name from {menu_links} WHERE link_path = 'admin/build/menu' AND module = 'system'"));
  157. $base_link['router_path'] = 'admin/build/menu-customize/%';
  158. $base_link['module'] = 'menu';
  159. $result = db_query("SELECT * FROM {menu_custom}");
  160. while ($menu = db_fetch_array($result)) {
  161. // $link is passed by reference to menu_link_save(), so we make a copy of $base_link.
  162. $link = $base_link;
  163. $link['mlid'] = 0;
  164. $link['link_title'] = $menu['title'];
  165. $link['link_path'] = 'admin/build/menu-customize/'. $menu['menu_name'];
  166. if (!db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s' AND plid = %d", $link['link_path'], $link['plid']))) {
  167. menu_link_save($link);
  168. }
  169. }
  170. menu_cache_clear_all();
  171. }
  172. /**
  173. * Title callback for the menu overview page and links.
  174. */
  175. function menu_overview_title($menu) {
  176. return $menu['title'];
  177. }
  178. /**
  179. * Load the data for a single custom menu.
  180. */
  181. function menu_load($menu_name) {
  182. return db_fetch_array(db_query("SELECT * FROM {menu_custom} WHERE menu_name = '%s'", $menu_name));
  183. }
  184. /**
  185. * Return a list of menu items that are valid possible parents for the given menu item.
  186. *
  187. * @param $menus
  188. * An array of menu names and titles, such as from menu_get_menus().
  189. * @param $item
  190. * The menu item for which to generate a list of parents.
  191. * If $item['mlid'] == 0 then the complete tree is returned.
  192. * @return
  193. * An array of menu link titles keyed on the a string containing the menu name
  194. * and mlid. The list excludes the given item and its children.
  195. */
  196. function menu_parent_options($menus, $item) {
  197. // The menu_links table can be practically any size and we need a way to
  198. // allow contrib modules to provide more scalable pattern choosers.
  199. // hook_form_alter is too late in itself because all the possible parents are
  200. // retrieved here, unless menu_override_parent_selector is set to TRUE.
  201. if (variable_get('menu_override_parent_selector', FALSE)) {
  202. return array();
  203. }
  204. // If the item has children, there is an added limit to the depth of valid parents.
  205. if (isset($item['parent_depth_limit'])) {
  206. $limit = $item['parent_depth_limit'];
  207. }
  208. else {
  209. $limit = _menu_parent_depth_limit($item);
  210. }
  211. foreach ($menus as $menu_name => $title) {
  212. $tree = menu_tree_all_data($menu_name, NULL);
  213. $options[$menu_name .':0'] = '<'. $title .'>';
  214. _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit);
  215. }
  216. return $options;
  217. }
  218. /**
  219. * Recursive helper function for menu_parent_options().
  220. */
  221. function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) {
  222. foreach ($tree as $data) {
  223. if ($data['link']['depth'] > $depth_limit) {
  224. // Don't iterate through any links on this level.
  225. break;
  226. }
  227. if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) {
  228. $title = $indent .' '. truncate_utf8($data['link']['title'], 30, TRUE, FALSE);
  229. if ($data['link']['hidden']) {
  230. $title .= ' ('. t('disabled') .')';
  231. }
  232. $options[$menu_name .':'. $data['link']['mlid']] = $title;
  233. if ($data['below']) {
  234. _menu_parents_recurse($data['below'], $menu_name, $indent .'--', $options, $exclude, $depth_limit);
  235. }
  236. }
  237. }
  238. }
  239. /**
  240. * Reset a system-defined menu item.
  241. */
  242. function menu_reset_item($item) {
  243. $new_item = _menu_link_build(menu_get_item($item['router_path']));
  244. foreach (array('mlid', 'has_children') as $key) {
  245. $new_item[$key] = $item[$key];
  246. }
  247. menu_link_save($new_item);
  248. return $new_item;
  249. }
  250. /**
  251. * Implementation of hook_block().
  252. */
  253. function menu_block($op = 'list', $delta = 0) {
  254. $menus = menu_get_menus();
  255. // The Navigation menu is handled by the user module.
  256. unset($menus['navigation']);
  257. if ($op == 'list') {
  258. $blocks = array();
  259. foreach ($menus as $name => $title) {
  260. $blocks[$name]['info'] = check_plain($title);
  261. // Menu blocks can't be cached because each menu item can have
  262. // a custom access callback. menu.inc manages its own caching.
  263. $blocks[$name]['cache'] = BLOCK_NO_CACHE;
  264. }
  265. return $blocks;
  266. }
  267. else if ($op == 'view') {
  268. $data['subject'] = check_plain($menus[$delta]);
  269. $data['content'] = menu_tree($delta);
  270. return $data;
  271. }
  272. }
  273. /**
  274. * Implementation of hook_nodeapi().
  275. */
  276. function menu_nodeapi(&$node, $op) {
  277. switch ($op) {
  278. case 'insert':
  279. case 'update':
  280. if (isset($node->menu)) {
  281. $item = &$node->menu;
  282. if (!empty($item['delete'])) {
  283. menu_link_delete($item['mlid']);
  284. }
  285. elseif (trim($item['link_title'])) {
  286. $item['link_title'] = trim($item['link_title']);
  287. $item['link_path'] = "node/$node->nid";
  288. if (!$item['customized']) {
  289. $item['options']['attributes']['title'] = trim($node->title);
  290. }
  291. if (!menu_link_save($item)) {
  292. drupal_set_message(t('There was an error saving the menu link.'), 'error');
  293. }
  294. }
  295. }
  296. break;
  297. case 'delete':
  298. // Delete all menu module links that point to this node.
  299. $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND module = 'menu'", $node->nid);
  300. while ($m = db_fetch_array($result)) {
  301. menu_link_delete($m['mlid']);
  302. }
  303. break;
  304. case 'prepare':
  305. if (empty($node->menu)) {
  306. // Prepare the node for the edit form so that $node->menu always exists.
  307. $menu_name = variable_get('menu_default_node_menu', 'primary-links');
  308. $item = array();
  309. if (isset($node->nid)) {
  310. // Give priority to the default menu
  311. $mlid = db_result(db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND menu_name = '%s' AND module = 'menu' ORDER BY mlid ASC", $node->nid, $menu_name, 0, 1));
  312. // Check all menus if a link does not exist in the default menu.
  313. if (!$mlid) {
  314. $mlid = db_result(db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND module = 'menu' ORDER BY mlid ASC", $node->nid, 0, 1));
  315. }
  316. if ($mlid) {
  317. $item = menu_link_load($mlid);
  318. }
  319. }
  320. // Set default values.
  321. $node->menu = $item + array('link_title' => '', 'mlid' => 0, 'plid' => 0, 'menu_name' => $menu_name, 'weight' => 0, 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0, 'customized' => 0);
  322. }
  323. // Find the depth limit for the parent select.
  324. if (!isset($node->menu['parent_depth_limit'])) {
  325. $node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu);
  326. }
  327. break;
  328. }
  329. }
  330. /**
  331. * Find the depth limit for items in the parent select.
  332. */
  333. function _menu_parent_depth_limit($item) {
  334. return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0);
  335. }
  336. /**
  337. * Implementation of hook_form_alter(). Adds menu item fields to the node form.
  338. */
  339. function menu_form_alter(&$form, $form_state, $form_id) {
  340. if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
  341. // Note - doing this to make sure the delete checkbox stays in the form.
  342. $form['#cache'] = TRUE;
  343. $form['menu'] = array(
  344. '#type' => 'fieldset',
  345. '#title' => t('Menu settings'),
  346. '#access' => user_access('administer menu'),
  347. '#collapsible' => TRUE,
  348. '#collapsed' => FALSE,
  349. '#tree' => TRUE,
  350. '#weight' => -2,
  351. '#attributes' => array('class' => 'menu-item-form'),
  352. );
  353. $item = $form['#node']->menu;
  354. if ($item['mlid']) {
  355. // There is an existing link.
  356. $form['menu']['delete'] = array(
  357. '#type' => 'checkbox',
  358. '#title' => t('Delete this menu item.'),
  359. );
  360. }
  361. if (!$item['link_title']) {
  362. $form['menu']['#collapsed'] = TRUE;
  363. }
  364. foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) {
  365. $form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]);
  366. }
  367. $form['menu']['#item'] = $item;
  368. $form['menu']['link_title'] = array('#type' => 'textfield',
  369. '#title' => t('Menu link title'),
  370. '#default_value' => $item['link_title'],
  371. '#description' => t('The link text corresponding to this item that should appear in the menu. Leave blank if you do not wish to add this post to the menu.'),
  372. '#required' => FALSE,
  373. );
  374. // Generate a list of possible parents (not including this item or descendants).
  375. $options = menu_parent_options(menu_get_menus(), $item);
  376. $default = $item['menu_name'] .':'. $item['plid'];
  377. if (!isset($options[$default])) {
  378. $default = 'primary-links:0';
  379. }
  380. $form['menu']['parent'] = array(
  381. '#type' => 'select',
  382. '#title' => t('Parent item'),
  383. '#default_value' => $default,
  384. '#options' => $options,
  385. '#description' => t('The maximum depth for an item and all its children is fixed at !maxdepth. Some menu items may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)),
  386. '#attributes' => array('class' => 'menu-title-select'),
  387. );
  388. $form['#submit'][] = 'menu_node_form_submit';
  389. $form['menu']['weight'] = array(
  390. '#type' => 'weight',
  391. '#title' => t('Weight'),
  392. '#delta' => 50,
  393. '#default_value' => $item['weight'],
  394. '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
  395. );
  396. }
  397. }
  398. /**
  399. * Decompose the selected menu parent option into the menu_name and plid.
  400. */
  401. function menu_node_form_submit($form, &$form_state) {
  402. list($form_state['values']['menu']['menu_name'], $form_state['values']['menu']['plid']) = explode(':', $form_state['values']['menu']['parent']);
  403. }
  404. /**
  405. * Return an associative array of the custom menus names.
  406. *
  407. * @param $all
  408. * If FALSE return only user-added menus, or if TRUE also include
  409. * the menus defined by the system.
  410. * @return
  411. * An array with the machine-readable names as the keys, and human-readable
  412. * titles as the values.
  413. */
  414. function menu_get_menus($all = TRUE) {
  415. $system_menus = menu_list_system_menus();
  416. $sql = 'SELECT * FROM {menu_custom}';
  417. if (!$all) {
  418. $sql .= ' WHERE menu_name NOT IN ('. implode(',', array_fill(0, count($system_menus), "'%s'")) .')';
  419. }
  420. $sql .= ' ORDER BY title';
  421. $result = db_query($sql, $system_menus);
  422. $rows = array();
  423. while ($r = db_fetch_array($result)) {
  424. $rows[$r['menu_name']] = $r['title'];
  425. }
  426. return $rows;
  427. }