Skip navigation
Help

menu.module

  1. drupal
    1. 6 drupal/modules/menu/menu.module
    2. 7 drupal/modules/menu/menu.module

Allows administrators to customize the site's navigation menus.

A menu (in this context) is a hierarchical collection of links, generally used for navigation. This is not to be confused with the Menu system of menu.inc and hook_menu(), which defines page routing requests for Drupal, and also allows the defined page routing URLs to be added to the main site navigation menu.

Functions & methods

NameDescription
menu_block_infoImplements hook_block_info().
menu_block_viewImplements hook_block_view().
menu_block_view_alterImplements hook_block_view_alter().
menu_deleteDelete a custom menu and all contained links.
menu_enableImplements hook_enable().
menu_form_node_form_alterImplements hook_form_BASE_FORM_ID_alter().
menu_form_node_type_form_alterImplements hook_form_FORM_ID_alter().
menu_get_menusReturn an associative array of the custom menus names.
menu_helpImplements hook_help().
menu_loadLoad the data for a single custom menu.
menu_load_allLoad all custom menu data.
menu_menuImplements hook_menu().
menu_node_deleteImplements hook_node_delete().
menu_node_insertImplements hook_node_insert().
menu_node_prepareImplements hook_node_prepare().
menu_node_saveHelper for hook_node_insert() and hook_node_update().
menu_node_submitImplements hook_node_submit().
menu_node_updateImplements hook_node_update().
menu_overview_titleTitle callback for the menu overview page and links.
menu_parent_optionsReturn a list of menu items that are valid possible parents for the given menu item.
menu_parent_options_jsPage callback. Get all the available menus and menu items as a JavaScript array.
menu_permissionImplements hook_permission().
menu_reset_itemReset a system-defined menu link.
menu_saveSave a custom menu.
menu_themeImplements hook_theme().
_menu_get_optionsHelper function to get the items of the given menu.
_menu_parents_recurseRecursive helper function for menu_parent_options().
_menu_parent_depth_limitFind the depth limit for items in the parent select.

Constants

NameDescription
MENU_MAX_MENU_NAME_LENGTH_UIMaximum length of menu name as entered by the user. Database length is 32 and we add a menu- prefix.

File

drupal/modules/menu/menu.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Allows administrators to customize the site's navigation menus.
  5. *
  6. * A menu (in this context) is a hierarchical collection of links, generally
  7. * used for navigation. This is not to be confused with the
  8. * @link menu Menu system @endlink of menu.inc and hook_menu(), which defines
  9. * page routing requests for Drupal, and also allows the defined page routing
  10. * URLs to be added to the main site navigation menu.
  11. */
  12. /**
  13. * Maximum length of menu name as entered by the user. Database length is 32
  14. * and we add a menu- prefix.
  15. */
  16. define('MENU_MAX_MENU_NAME_LENGTH_UI', 27);
  17. /**
  18. * Implements hook_help().
  19. */
  20. function menu_help($path, $arg) {
  21. switch ($path) {
  22. case 'admin/help#menu':
  23. $output = '';
  24. $output .= '<h3>' . t('About') . '</h3>';
  25. $output .= '<p>' . t('The Menu module provides an interface for managing menus. A menu is a hierarchical collection of links, which can be within or external to the site, generally used for navigation. Each menu is rendered in a block that can be enabled and positioned through the <a href="@blocks">Blocks administration page</a>. You can view and manage menus on the <a href="@menus">Menus administration page</a>. For more information, see the online handbook entry for the <a href="@menu">Menu module</a>.', array('@blocks' => url('admin/structure/block'), '@menus' => url('admin/structure/menu'), '@menu' => 'http://drupal.org/handbook/modules/menu/')) . '</p>';
  26. $output .= '<h3>' . t('Uses') . '</h3>';
  27. $output .= '<dl>';
  28. $output .= '<dt>' . t('Managing menus') . '</dt>';
  29. $output .= '<dd>' . t('Users with the <em>Administer menus and menu items</em> permission can add, edit and delete custom menus on the <a href="@menu">Menus administration page</a>. Custom menus can be special site menus, menus of external links, or any combination of internal and external links. You may create an unlimited number of additional menus, each of which will automatically have an associated block. By selecting <em>list links</em>, you can add, edit, or delete links for a given menu. The links listing page provides a drag-and-drop interface for controlling the order of links, and creating a hierarchy within the menu.', array('@menu' => url('admin/structure/menu'), '@add-menu' => url('admin/structure/menu/add'))) . '</dd>';
  30. $output .= '<dt>' . t('Displaying menus') . '</dt>';
  31. $output .= '<dd>' . t('After you have created a menu, you must enable and position the associated block on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</dd>';
  32. $output .= '</dl>';
  33. return $output;
  34. case 'admin/structure/menu/add':
  35. return '<p>' . t('You can enable the newly-created block for this menu on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</p>';
  36. }
  37. if ($path == 'admin/structure/menu' && module_exists('block')) {
  38. return '<p>' . t('Each menu has a corresponding block that is managed on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</p>';
  39. }
  40. }
  41. /**
  42. * Implements hook_permission().
  43. */
  44. function menu_permission() {
  45. return array(
  46. 'administer menu' => array(
  47. 'title' => t('Administer menus and menu items'),
  48. ),
  49. );
  50. }
  51. /**
  52. * Implements hook_menu().
  53. */
  54. function menu_menu() {
  55. $items['admin/structure/menu'] = array(
  56. 'title' => 'Menus',
  57. 'description' => 'Add new menus to your site, edit existing menus, and rename and reorganize menu links.',
  58. 'page callback' => 'menu_overview_page',
  59. 'access callback' => 'user_access',
  60. 'access arguments' => array('administer menu'),
  61. 'file' => 'menu.admin.inc',
  62. );
  63. $items['admin/structure/menu/parents'] = array(
  64. 'title' => 'Parent menu items',
  65. 'page callback' => 'menu_parent_options_js',
  66. 'type' => MENU_CALLBACK,
  67. 'access arguments' => array(TRUE),
  68. );
  69. $items['admin/structure/menu/list'] = array(
  70. 'title' => 'List menus',
  71. 'type' => MENU_DEFAULT_LOCAL_TASK,
  72. 'weight' => -10,
  73. );
  74. $items['admin/structure/menu/add'] = array(
  75. 'title' => 'Add menu',
  76. 'page callback' => 'drupal_get_form',
  77. 'page arguments' => array('menu_edit_menu', 'add'),
  78. 'access arguments' => array('administer menu'),
  79. 'type' => MENU_LOCAL_ACTION,
  80. 'file' => 'menu.admin.inc',
  81. );
  82. $items['admin/structure/menu/settings'] = array(
  83. 'title' => 'Settings',
  84. 'page callback' => 'drupal_get_form',
  85. 'page arguments' => array('menu_configure'),
  86. 'access arguments' => array('administer menu'),
  87. 'type' => MENU_LOCAL_TASK,
  88. 'weight' => 5,
  89. 'file' => 'menu.admin.inc',
  90. );
  91. $items['admin/structure/menu/manage/%menu'] = array(
  92. 'title' => 'Customize menu',
  93. 'page callback' => 'drupal_get_form',
  94. 'page arguments' => array('menu_overview_form', 4),
  95. 'title callback' => 'menu_overview_title',
  96. 'title arguments' => array(4),
  97. 'access arguments' => array('administer menu'),
  98. 'file' => 'menu.admin.inc',
  99. );
  100. $items['admin/structure/menu/manage/%menu/list'] = array(
  101. 'title' => 'List links',
  102. 'weight' => -10,
  103. 'type' => MENU_DEFAULT_LOCAL_TASK,
  104. 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  105. );
  106. $items['admin/structure/menu/manage/%menu/add'] = array(
  107. 'title' => 'Add link',
  108. 'page callback' => 'drupal_get_form',
  109. 'page arguments' => array('menu_edit_item', 'add', NULL, 4),
  110. 'access arguments' => array('administer menu'),
  111. 'type' => MENU_LOCAL_ACTION,
  112. 'file' => 'menu.admin.inc',
  113. );
  114. $items['admin/structure/menu/manage/%menu/edit'] = array(
  115. 'title' => 'Edit menu',
  116. 'page callback' => 'drupal_get_form',
  117. 'page arguments' => array('menu_edit_menu', 'edit', 4),
  118. 'access arguments' => array('administer menu'),
  119. 'type' => MENU_LOCAL_TASK,
  120. 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  121. 'file' => 'menu.admin.inc',
  122. );
  123. $items['admin/structure/menu/manage/%menu/delete'] = array(
  124. 'title' => 'Delete menu',
  125. 'page callback' => 'menu_delete_menu_page',
  126. 'page arguments' => array(4),
  127. 'access arguments' => array('administer menu'),
  128. 'file' => 'menu.admin.inc',
  129. );
  130. $items['admin/structure/menu/item/%menu_link/edit'] = array(
  131. 'title' => 'Edit menu link',
  132. 'page callback' => 'drupal_get_form',
  133. 'page arguments' => array('menu_edit_item', 'edit', 4, NULL),
  134. 'access arguments' => array('administer menu'),
  135. 'file' => 'menu.admin.inc',
  136. );
  137. $items['admin/structure/menu/item/%menu_link/reset'] = array(
  138. 'title' => 'Reset menu link',
  139. 'page callback' => 'drupal_get_form',
  140. 'page arguments' => array('menu_reset_item_confirm', 4),
  141. 'access arguments' => array('administer menu'),
  142. 'file' => 'menu.admin.inc',
  143. );
  144. $items['admin/structure/menu/item/%menu_link/delete'] = array(
  145. 'title' => 'Delete menu link',
  146. 'page callback' => 'menu_item_delete_page',
  147. 'page arguments' => array(4),
  148. 'access arguments' => array('administer menu'),
  149. 'file' => 'menu.admin.inc',
  150. );
  151. return $items;
  152. }
  153. /**
  154. * Implements hook_theme().
  155. */
  156. function menu_theme() {
  157. return array(
  158. 'menu_overview_form' => array(
  159. 'file' => 'menu.admin.inc',
  160. 'render element' => 'form',
  161. ),
  162. 'menu_admin_overview' => array(
  163. 'file' => 'menu.admin.inc',
  164. 'variables' => array('title' => NULL, 'name' => NULL, 'description' => NULL),
  165. ),
  166. );
  167. }
  168. /**
  169. * Implements hook_enable().
  170. *
  171. * Add a link for each custom menu.
  172. */
  173. function menu_enable() {
  174. menu_rebuild();
  175. $base_link = db_query("SELECT mlid AS plid, menu_name FROM {menu_links} WHERE link_path = 'admin/structure/menu' AND module = 'system'")->fetchAssoc();
  176. $base_link['router_path'] = 'admin/structure/menu/manage/%';
  177. $base_link['module'] = 'menu';
  178. $result = db_query("SELECT * FROM {menu_custom}", array(), array('fetch' => PDO::FETCH_ASSOC));
  179. foreach ($result as $menu) {
  180. // $link is passed by reference to menu_link_save(), so we make a copy of $base_link.
  181. $link = $base_link;
  182. $link['mlid'] = 0;
  183. $link['link_title'] = $menu['title'];
  184. $link['link_path'] = 'admin/structure/menu/manage/' . $menu['menu_name'];
  185. $menu_link = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND plid = :plid", array(
  186. ':path' => $link['link_path'],
  187. ':plid' => $link['plid']
  188. ))
  189. ->fetchField();
  190. if (!$menu_link) {
  191. menu_link_save($link);
  192. }
  193. }
  194. menu_cache_clear_all();
  195. }
  196. /**
  197. * Title callback for the menu overview page and links.
  198. */
  199. function menu_overview_title($menu) {
  200. return $menu['title'];
  201. }
  202. /**
  203. * Load the data for a single custom menu.
  204. *
  205. * @param $menu_name
  206. * The unique name of a custom menu to load.
  207. * @return
  208. * Array defining the custom menu, or FALSE if the menu doesn't exist.
  209. */
  210. function menu_load($menu_name) {
  211. $all_menus = menu_load_all();
  212. return isset($all_menus[$menu_name]) ? $all_menus[$menu_name] : FALSE;
  213. }
  214. /**
  215. * Load all custom menu data.
  216. *
  217. * @return
  218. * Array of custom menu data.
  219. */
  220. function menu_load_all() {
  221. $custom_menus = &drupal_static(__FUNCTION__);
  222. if (!isset($custom_menus)) {
  223. if ($cached = cache_get('menu_custom', 'cache_menu')) {
  224. $custom_menus = $cached->data;
  225. }
  226. else {
  227. $custom_menus = db_query('SELECT * FROM {menu_custom}')->fetchAllAssoc('menu_name', PDO::FETCH_ASSOC);
  228. cache_set('menu_custom', $custom_menus, 'cache_menu');
  229. }
  230. }
  231. return $custom_menus;
  232. }
  233. /**
  234. * Save a custom menu.
  235. *
  236. * @param $menu
  237. * An array representing a custom menu:
  238. * - menu_name: The unique name of the custom menu (composed of lowercase
  239. * letters, numbers, and hyphens).
  240. * - title: The human readable menu title.
  241. * - description: The custom menu description.
  242. *
  243. * Modules should always pass a fully populated $menu when saving a custom
  244. * menu, so other modules are able to output proper status or watchdog messages.
  245. *
  246. * @see menu_load()
  247. */
  248. function menu_save($menu) {
  249. $status = db_merge('menu_custom')
  250. ->key(array('menu_name' => $menu['menu_name']))
  251. ->fields(array(
  252. 'title' => $menu['title'],
  253. 'description' => $menu['description'],
  254. ))
  255. ->execute();
  256. menu_cache_clear_all();
  257. switch ($status) {
  258. case SAVED_NEW:
  259. module_invoke_all('menu_insert', $menu);
  260. break;
  261. case SAVED_UPDATED:
  262. module_invoke_all('menu_update', $menu);
  263. break;
  264. }
  265. }
  266. /**
  267. * Delete a custom menu and all contained links.
  268. *
  269. * Note that this function deletes all menu links in a custom menu. While menu
  270. * links derived from router paths may be restored by rebuilding the menu, all
  271. * customized and custom links will be irreversibly gone. Therefore, this
  272. * function should usually be called from a user interface (form submit) handler
  273. * only, which allows the user to confirm the action.
  274. *
  275. * @param $menu
  276. * An array representing a custom menu:
  277. * - menu_name: The unique name of the custom menu.
  278. * - title: The human readable menu title.
  279. * - description: The custom menu description.
  280. *
  281. * Modules should always pass a fully populated $menu when deleting a custom
  282. * menu, so other modules are able to output proper status or watchdog messages.
  283. *
  284. * @see menu_load()
  285. *
  286. * menu_delete_links() will take care of clearing the page cache. Other modules
  287. * should take care of their menu-related data by implementing
  288. * hook_menu_delete().
  289. */
  290. function menu_delete($menu) {
  291. // Delete all links from the menu.
  292. menu_delete_links($menu['menu_name']);
  293. // Delete the custom menu.
  294. db_delete('menu_custom')
  295. ->condition('menu_name', $menu['menu_name'])
  296. ->execute();
  297. menu_cache_clear_all();
  298. module_invoke_all('menu_delete', $menu);
  299. }
  300. /**
  301. * Return a list of menu items that are valid possible parents for the given menu item.
  302. *
  303. * @param $menus
  304. * An array of menu names and titles, such as from menu_get_menus().
  305. * @param $item
  306. * The menu item or the node type for which to generate a list of parents.
  307. * If $item['mlid'] == 0 then the complete tree is returned.
  308. * @return
  309. * An array of menu link titles keyed on the a string containing the menu name
  310. * and mlid. The list excludes the given item and its children.
  311. *
  312. * @todo This has to be turned into a #process form element callback. The
  313. * 'menu_override_parent_selector' variable is entirely superfluous.
  314. */
  315. function menu_parent_options($menus, $item) {
  316. // The menu_links table can be practically any size and we need a way to
  317. // allow contrib modules to provide more scalable pattern choosers.
  318. // hook_form_alter is too late in itself because all the possible parents are
  319. // retrieved here, unless menu_override_parent_selector is set to TRUE.
  320. if (variable_get('menu_override_parent_selector', FALSE)) {
  321. return array();
  322. }
  323. $available_menus = array();
  324. if (is_array($item)) {
  325. // If $item is an array fill it with all menus given to this function.
  326. $available_menus = $menus;
  327. }
  328. else {
  329. // If $item is a node type, get all available menus for this type and
  330. // prepare a dummy menu item for _menu_parent_depth_limit().
  331. $type_menus = variable_get('menu_options_' . $item, array('main-menu' => 'main-menu'));
  332. foreach ($type_menus as $menu) {
  333. $available_menus[$menu] = $menu;
  334. }
  335. $item = array('mlid' => 0);
  336. }
  337. return _menu_get_options($menus, $available_menus, $item);
  338. }
  339. /**
  340. * Page callback.
  341. * Get all the available menus and menu items as a JavaScript array.
  342. */
  343. function menu_parent_options_js() {
  344. $available_menus = array();
  345. if (isset($_POST['menus']) && count($_POST['menus'])) {
  346. foreach ($_POST['menus'] as $menu) {
  347. $available_menus[$menu] = $menu;
  348. }
  349. }
  350. $options = _menu_get_options(menu_get_menus(), $available_menus, array('mlid' => 0));
  351. drupal_json_output($options);
  352. }
  353. /**
  354. * Helper function to get the items of the given menu.
  355. */
  356. function _menu_get_options($menus, $available_menus, $item) {
  357. // If the item has children, there is an added limit to the depth of valid parents.
  358. if (isset($item['parent_depth_limit'])) {
  359. $limit = $item['parent_depth_limit'];
  360. }
  361. else {
  362. $limit = _menu_parent_depth_limit($item);
  363. }
  364. $options = array();
  365. foreach ($menus as $menu_name => $title) {
  366. if (isset($available_menus[$menu_name])) {
  367. $tree = menu_tree_all_data($menu_name, NULL);
  368. $options[$menu_name . ':0'] = '<' . $title . '>';
  369. _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit);
  370. }
  371. }
  372. return $options;
  373. }
  374. /**
  375. * Recursive helper function for menu_parent_options().
  376. */
  377. function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) {
  378. foreach ($tree as $data) {
  379. if ($data['link']['depth'] > $depth_limit) {
  380. // Don't iterate through any links on this level.
  381. break;
  382. }
  383. if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) {
  384. $title = $indent . ' ' . truncate_utf8($data['link']['title'], 30, TRUE, FALSE);
  385. if ($data['link']['hidden']) {
  386. $title .= ' (' . t('disabled') . ')';
  387. }
  388. $options[$menu_name . ':' . $data['link']['mlid']] = $title;
  389. if ($data['below']) {
  390. _menu_parents_recurse($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit);
  391. }
  392. }
  393. }
  394. }
  395. /**
  396. * Reset a system-defined menu link.
  397. */
  398. function menu_reset_item($link) {
  399. // To reset the link to its original values, we need to retrieve its
  400. // definition from hook_menu(). Otherwise, for example, the link's menu would
  401. // not be reset, because properties like the original 'menu_name' are not
  402. // stored anywhere else. Since resetting a link happens rarely and this is a
  403. // one-time operation, retrieving the full menu router does no harm.
  404. $menu = menu_get_router();
  405. $router_item = $menu[$link['router_path']];
  406. $new_link = _menu_link_build($router_item);
  407. // Merge existing menu link's ID and 'has_children' property.
  408. foreach (array('mlid', 'has_children') as $key) {
  409. $new_link[$key] = $link[$key];
  410. }
  411. menu_link_save($new_link);
  412. return $new_link;
  413. }
  414. /**
  415. * Implements hook_block_info().
  416. */
  417. function menu_block_info() {
  418. $menus = menu_get_menus(FALSE);
  419. $blocks = array();
  420. foreach ($menus as $name => $title) {
  421. // Default "Navigation" block is handled by user.module.
  422. $blocks[$name]['info'] = check_plain($title);
  423. // Menu blocks can't be cached because each menu item can have
  424. // a custom access callback. menu.inc manages its own caching.
  425. $blocks[$name]['cache'] = DRUPAL_NO_CACHE;
  426. }
  427. return $blocks;
  428. }
  429. /**
  430. * Implements hook_block_view().
  431. */
  432. function menu_block_view($delta = '') {
  433. $menus = menu_get_menus(FALSE);
  434. $data['subject'] = check_plain($menus[$delta]);
  435. $data['content'] = menu_tree($delta);
  436. // Add contextual links for this block.
  437. if (!empty($data['content'])) {
  438. $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($delta));
  439. }
  440. return $data;
  441. }
  442. /**
  443. * Implements hook_block_view_alter().
  444. */
  445. function menu_block_view_alter(&$data, $block) {
  446. // Add contextual links for system menu blocks.
  447. if ($block->module == 'system' && !empty($data['content'])) {
  448. $system_menus = menu_list_system_menus();
  449. if (isset($system_menus[$block->delta])) {
  450. $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($block->delta));
  451. }
  452. }
  453. }
  454. /**
  455. * Implements hook_node_insert().
  456. */
  457. function menu_node_insert($node) {
  458. menu_node_save($node);
  459. }
  460. /**
  461. * Implements hook_node_update().
  462. */
  463. function menu_node_update($node) {
  464. menu_node_save($node);
  465. }
  466. /**
  467. * Helper for hook_node_insert() and hook_node_update().
  468. */
  469. function menu_node_save($node) {
  470. if (isset($node->menu)) {
  471. $link = &$node->menu;
  472. if (empty($link['enabled'])) {
  473. if (!empty($link['mlid'])) {
  474. menu_link_delete($link['mlid']);
  475. }
  476. }
  477. elseif (trim($link['link_title'])) {
  478. $link['link_title'] = trim($link['link_title']);
  479. $link['link_path'] = "node/$node->nid";
  480. if (trim($link['description'])) {
  481. $link['options']['attributes']['title'] = trim($link['description']);
  482. }
  483. else {
  484. // If the description field was left empty, remove the title attribute
  485. // from the menu link.
  486. unset($link['options']['attributes']['title']);
  487. }
  488. if (!menu_link_save($link)) {
  489. drupal_set_message(t('There was an error saving the menu link.'), 'error');
  490. }
  491. }
  492. }
  493. }
  494. /**
  495. * Implements hook_node_delete().
  496. */
  497. function menu_node_delete($node) {
  498. // Delete all menu module links that point to this node.
  499. $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu'", array(':path' => 'node/' . $node->nid), array('fetch' => PDO::FETCH_ASSOC));
  500. foreach ($result as $m) {
  501. menu_link_delete($m['mlid']);
  502. }
  503. }
  504. /**
  505. * Implements hook_node_prepare().
  506. */
  507. function menu_node_prepare($node) {
  508. if (empty($node->menu)) {
  509. // Prepare the node for the edit form so that $node->menu always exists.
  510. $menu_name = strtok(variable_get('menu_parent_' . $node->type, 'main-menu:0'), ':');
  511. $item = array();
  512. if (isset($node->nid)) {
  513. $mlid = FALSE;
  514. // Give priority to the default menu
  515. $type_menus = variable_get('menu_options_' . $node->type, array('main-menu' => 'main-menu'));
  516. if (in_array($menu_name, $type_menus)) {
  517. $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND menu_name = :menu_name AND module = 'menu' ORDER BY mlid ASC", 0, 1, array(
  518. ':path' => 'node/' . $node->nid,
  519. ':menu_name' => $menu_name,
  520. ))->fetchField();
  521. }
  522. // Check all allowed menus if a link does not exist in the default menu.
  523. if (!$mlid && !empty($type_menus)) {
  524. $mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' AND menu_name IN (:type_menus) ORDER BY mlid ASC", 0, 1, array(
  525. ':path' => 'node/' . $node->nid,
  526. ':type_menus' => array_values($type_menus),
  527. ))->fetchField();
  528. }
  529. if ($mlid) {
  530. $item = menu_link_load($mlid);
  531. }
  532. }
  533. // Set default values.
  534. $node->menu = $item + array(
  535. 'link_title' => '',
  536. 'mlid' => 0,
  537. 'plid' => 0,
  538. 'menu_name' => $menu_name,
  539. 'weight' => 0,
  540. 'options' => array(),
  541. 'module' => 'menu',
  542. 'expanded' => 0,
  543. 'hidden' => 0,
  544. 'has_children' => 0,
  545. 'customized' => 0,
  546. );
  547. }
  548. // Find the depth limit for the parent select.
  549. if (!isset($node->menu['parent_depth_limit'])) {
  550. $node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu);
  551. }
  552. }
  553. /**
  554. * Find the depth limit for items in the parent select.
  555. */
  556. function _menu_parent_depth_limit($item) {
  557. return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0);
  558. }
  559. /**
  560. * Implements hook_form_BASE_FORM_ID_alter().
  561. *
  562. * Adds menu item fields to the node form.
  563. *
  564. * @see menu_node_submit()
  565. */
  566. function menu_form_node_form_alter(&$form, $form_state) {
  567. // Generate a list of possible parents.
  568. // @todo This must be handled in a #process handler.
  569. $type = $form['#node']->type;
  570. $options = menu_parent_options(menu_get_menus(), $type);
  571. // If no possible parent menu items were found, there is nothing to display.
  572. if (empty($options)) {
  573. return;
  574. }
  575. $link = $form['#node']->menu;
  576. $form['menu'] = array(
  577. '#type' => 'fieldset',
  578. '#title' => t('Menu settings'),
  579. '#access' => user_access('administer menu'),
  580. '#collapsible' => TRUE,
  581. '#collapsed' => !$link['link_title'],
  582. '#group' => 'additional_settings',
  583. '#attached' => array(
  584. 'js' => array(drupal_get_path('module', 'menu') . '/menu.js'),
  585. ),
  586. '#tree' => TRUE,
  587. '#weight' => -2,
  588. '#attributes' => array('class' => array('menu-link-form')),
  589. );
  590. $form['menu']['enabled'] = array(
  591. '#type' => 'checkbox',
  592. '#title' => t('Provide a menu link'),
  593. '#default_value' => (int) (bool) $link['mlid'],
  594. );
  595. $form['menu']['link'] = array(
  596. '#type' => 'container',
  597. '#parents' => array('menu'),
  598. '#states' => array(
  599. 'invisible' => array(
  600. 'input[name="menu[enabled]"]' => array('checked' => FALSE),
  601. ),
  602. ),
  603. );
  604. // Populate the element with the link data.
  605. foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) {
  606. $form['menu']['link'][$key] = array('#type' => 'value', '#value' => $link[$key]);
  607. }
  608. $form['menu']['link']['link_title'] = array(
  609. '#type' => 'textfield',
  610. '#title' => t('Menu link title'),
  611. '#default_value' => $link['link_title'],
  612. );
  613. $form['menu']['link']['description'] = array(
  614. '#type' => 'textarea',
  615. '#title' => t('Description'),
  616. '#default_value' => isset($link['options']['attributes']['title']) ? $link['options']['attributes']['title'] : '',
  617. '#rows' => 1,
  618. '#description' => t('Shown when hovering over the menu link.'),
  619. );
  620. $default = ($link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0'));
  621. // @todo This will fail with the new selective menus per content type.
  622. if (!isset($options[$default])) {
  623. $default = 'navigation:0';
  624. }
  625. $form['menu']['link']['parent'] = array(
  626. '#type' => 'select',
  627. '#title' => t('Parent item'),
  628. '#default_value' => $default,
  629. '#options' => $options,
  630. '#attributes' => array('class' => array('menu-parent-select')),
  631. );
  632. $form['menu']['link']['weight'] = array(
  633. '#type' => 'weight',
  634. '#title' => t('Weight'),
  635. '#delta' => 50,
  636. '#default_value' => $link['weight'],
  637. '#description' => t('Menu links with smaller weights are displayed before links with larger weights.'),
  638. );
  639. }
  640. /**
  641. * Implements hook_node_submit().
  642. *
  643. * @see menu_form_node_form_alter()
  644. */
  645. function menu_node_submit($node, $form, $form_state) {
  646. // Decompose the selected menu parent option into 'menu_name' and 'plid', if
  647. // the form used the default parent selection widget.
  648. if (!empty($form_state['values']['menu']['parent'])) {
  649. list($node->menu['menu_name'], $node->menu['plid']) = explode(':', $form_state['values']['menu']['parent']);
  650. }
  651. }
  652. /**
  653. * Implements hook_form_FORM_ID_alter().
  654. *
  655. * Adds menu options to the node type form.
  656. */
  657. function menu_form_node_type_form_alter(&$form, $form_state) {
  658. $menu_options = menu_get_menus();
  659. $type = $form['#node_type'];
  660. $form['menu'] = array(
  661. '#type' => 'fieldset',
  662. '#title' => t('Menu settings'),
  663. '#collapsible' => TRUE,
  664. '#collapsed' => TRUE,
  665. '#attached' => array(
  666. 'js' => array(drupal_get_path('module', 'menu') . '/menu.admin.js'),
  667. ),
  668. '#group' => 'additional_settings',
  669. );
  670. $form['menu']['menu_options'] = array(
  671. '#type' => 'checkboxes',
  672. '#title' => t('Available menus'),
  673. '#default_value' => variable_get('menu_options_' . $type->type, array('main-menu')),
  674. '#options' => $menu_options,
  675. '#description' => t('The menus available to place links in for this content type.'),
  676. );
  677. // To avoid an 'illegal option' error after saving the form we have to load
  678. // all available menu items.
  679. // Otherwise it is not possible to dynamically add options to the list.
  680. // @todo Convert menu_parent_options() into a #process callback.
  681. $options = menu_parent_options(menu_get_menus(), array('mlid' => 0));
  682. $form['menu']['menu_parent'] = array(
  683. '#type' => 'select',
  684. '#title' => t('Default parent item'),
  685. '#default_value' => variable_get('menu_parent_' . $type->type, 'main-menu:0'),
  686. '#options' => $options,
  687. '#description' => t('Choose the menu item to be the default parent for a new link in the content authoring form.'),
  688. '#attributes' => array('class' => array('menu-title-select')),
  689. );
  690. // Call Drupal.menu_update_parent_list() to filter the list of
  691. // available default parent menu items based on the selected menus.
  692. drupal_add_js(
  693. '(function ($) { Drupal.menu_update_parent_list(); })(jQuery);',
  694. array('scope' => 'footer', 'type' => 'inline')
  695. );
  696. }
  697. /**
  698. * Return an associative array of the custom menus names.
  699. *
  700. * @param $all
  701. * If FALSE return only user-added menus, or if TRUE also include
  702. * the menus defined by the system.
  703. * @return
  704. * An array with the machine-readable names as the keys, and human-readable
  705. * titles as the values.
  706. */
  707. function menu_get_menus($all = TRUE) {
  708. if ($custom_menus = menu_load_all()) {
  709. if (!$all) {
  710. $custom_menus = array_diff_key($custom_menus, menu_list_system_menus());
  711. }
  712. foreach ($custom_menus as $menu_name => $menu) {
  713. $custom_menus[$menu_name] = t($menu['title']);
  714. }
  715. asort($custom_menus);
  716. }
  717. return $custom_menus;
  718. }