Skip navigation
Help

menu.admin.inc

  1. drupal
    1. 6 drupal/modules/menu/menu.admin.inc
    2. 7 drupal/modules/menu/menu.admin.inc

Administrative page callbacks for menu module.

Functions & methods

NameDescription
menu_configureMenu callback; Build the form presenting menu configuration options.
menu_custom_delete_submitSubmit function for the 'Delete' button on the menu editing form.
menu_delete_menu_confirmBuild a confirm form for deletion of a custom menu.
menu_delete_menu_confirm_submitDelete a custom menu and all links in it.
menu_delete_menu_pageMenu callback; check access and get a confirm form for deletion of a custom menu.
menu_edit_itemMenu callback; Build the menu link editing form.
menu_edit_item_submitProcess menu and menu item add/edit form submissions.
menu_edit_item_validateValidate form values for a menu link being added or edited.
menu_edit_menuMenu callback; Build the form that handles the adding/editing of a custom menu.
menu_edit_menu_name_existsReturns whether a menu name already exists.
menu_edit_menu_submitSubmit function for adding or editing a custom menu.
menu_item_delete_formBuild a confirm form for deletion of a single menu link.
menu_item_delete_form_submitProcess menu delete form submissions.
menu_item_delete_pageMenu callback; Check access and present a confirm form for deleting a menu link.
menu_item_delete_submitSubmit function for the delete button on the menu item editing form.
menu_overview_formForm for editing an entire menu tree at once.
menu_overview_form_submitSubmit handler for the menu overview form.
menu_overview_pageMenu callback which shows an overview page of all the custom menus and their descriptions.
menu_reset_item_confirmMenu callback; reset a single modified menu link.
menu_reset_item_confirm_submitProcess menu reset item form submissions.
theme_menu_admin_overviewReturns HTML for a menu title and description for the menu overview page.
theme_menu_overview_formReturns HTML for the menu overview form into a table.
_menu_overview_tree_formRecursive helper function for menu_overview_form().

File

drupal/modules/menu/menu.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for menu module.
  5. */
  6. /**
  7. * Menu callback which shows an overview page of all the custom menus and their descriptions.
  8. */
  9. function menu_overview_page() {
  10. $result = db_query("SELECT * FROM {menu_custom} ORDER BY title", array(), array('fetch' => PDO::FETCH_ASSOC));
  11. $header = array(t('Title'), array('data' => t('Operations'), 'colspan' => '3'));
  12. $rows = array();
  13. foreach ($result as $menu) {
  14. $row = array(theme('menu_admin_overview', array('title' => $menu['title'], 'name' => $menu['menu_name'], 'description' => $menu['description'])));
  15. $row[] = array('data' => l(t('list links'), 'admin/structure/menu/manage/' . $menu['menu_name']));
  16. $row[] = array('data' => l(t('edit menu'), 'admin/structure/menu/manage/' . $menu['menu_name'] . '/edit'));
  17. $row[] = array('data' => l(t('add link'), 'admin/structure/menu/manage/' . $menu['menu_name'] . '/add'));
  18. $rows[] = $row;
  19. }
  20. return theme('table', array('header' => $header, 'rows' => $rows));
  21. }
  22. /**
  23. * Returns HTML for a menu title and description for the menu overview page.
  24. *
  25. * @param $variables
  26. * An associative array containing:
  27. * - title: The menu's title.
  28. * - description: The menu's description.
  29. *
  30. * @ingroup themeable
  31. */
  32. function theme_menu_admin_overview($variables) {
  33. $output = check_plain($variables['title']);
  34. $output .= '<div class="description">' . filter_xss_admin($variables['description']) . '</div>';
  35. return $output;
  36. }
  37. /**
  38. * Form for editing an entire menu tree at once.
  39. *
  40. * Shows for one menu the menu links accessible to the current user and
  41. * relevant operations.
  42. */
  43. function menu_overview_form($form, &$form_state, $menu) {
  44. global $menu_admin;
  45. $form['#attached']['css'] = array(drupal_get_path('module', 'menu') . '/menu.css');
  46. $sql = "
  47. SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.delivery_callback, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
  48. FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
  49. WHERE ml.menu_name = :menu
  50. ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC";
  51. $result = db_query($sql, array(':menu' => $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
  52. $links = array();
  53. foreach ($result as $item) {
  54. $links[] = $item;
  55. }
  56. $tree = menu_tree_data($links);
  57. $node_links = array();
  58. menu_tree_collect_node_links($tree, $node_links);
  59. // We indicate that a menu administrator is running the menu access check.
  60. $menu_admin = TRUE;
  61. menu_tree_check_access($tree, $node_links);
  62. $menu_admin = FALSE;
  63. $form = array_merge($form, _menu_overview_tree_form($tree));
  64. $form['#menu'] = $menu;
  65. if (element_children($form)) {
  66. $form['actions'] = array('#type' => 'actions');
  67. $form['actions']['submit'] = array(
  68. '#type' => 'submit',
  69. '#value' => t('Save configuration'),
  70. );
  71. }
  72. else {
  73. $form['#empty_text'] = t('There are no menu links yet. <a href="@link">Add link</a>.', array('@link' => url('admin/structure/menu/manage/'. $form['#menu']['menu_name'] .'/add')));
  74. }
  75. return $form;
  76. }
  77. /**
  78. * Recursive helper function for menu_overview_form().
  79. *
  80. * @param $tree
  81. * The menu_tree retrieved by menu_tree_data.
  82. */
  83. function _menu_overview_tree_form($tree) {
  84. $form = &drupal_static(__FUNCTION__, array('#tree' => TRUE));
  85. foreach ($tree as $data) {
  86. $title = '';
  87. $item = $data['link'];
  88. // Don't show callbacks; these have $item['hidden'] < 0.
  89. if ($item && $item['hidden'] >= 0) {
  90. $mlid = 'mlid:' . $item['mlid'];
  91. $form[$mlid]['#item'] = $item;
  92. $form[$mlid]['#attributes'] = $item['hidden'] ? array('class' => array('menu-disabled')) : array('class' => array('menu-enabled'));
  93. $form[$mlid]['title']['#markup'] = l($item['title'], $item['href'], $item['localized_options']) . ($item['hidden'] ? ' (' . t('disabled') . ')' : '');
  94. $form[$mlid]['hidden'] = array(
  95. '#type' => 'checkbox',
  96. '#title' => t('Enable @title menu link', array('@title' => $item['title'])),
  97. '#title_display' => 'invisible',
  98. '#default_value' => !$item['hidden'],
  99. );
  100. $form[$mlid]['weight'] = array(
  101. '#type' => 'weight',
  102. '#delta' => 50,
  103. '#default_value' => $item['weight'],
  104. '#title_display' => 'invisible',
  105. '#title' => t('Weight for @title', array('@title' => $item['title'])),
  106. );
  107. $form[$mlid]['mlid'] = array(
  108. '#type' => 'hidden',
  109. '#value' => $item['mlid'],
  110. );
  111. $form[$mlid]['plid'] = array(
  112. '#type' => 'hidden',
  113. '#default_value' => $item['plid'],
  114. );
  115. // Build a list of operations.
  116. $operations = array();
  117. $operations['edit'] = array('#type' => 'link', '#title' => t('edit'), '#href' => 'admin/structure/menu/item/' . $item['mlid'] . '/edit');
  118. // Only items created by the menu module can be deleted.
  119. if ($item['module'] == 'menu' || $item['updated'] == 1) {
  120. $operations['delete'] = array('#type' => 'link', '#title' => t('delete'), '#href' => 'admin/structure/menu/item/' . $item['mlid'] . '/delete');
  121. }
  122. // Set the reset column.
  123. elseif ($item['module'] == 'system' && $item['customized']) {
  124. $operations['reset'] = array('#type' => 'link', '#title' => t('reset'), '#href' => 'admin/structure/menu/item/' . $item['mlid'] . '/reset');
  125. }
  126. $form[$mlid]['operations'] = $operations;
  127. }
  128. if ($data['below']) {
  129. _menu_overview_tree_form($data['below']);
  130. }
  131. }
  132. return $form;
  133. }
  134. /**
  135. * Submit handler for the menu overview form.
  136. *
  137. * This function takes great care in saving parent items first, then items
  138. * underneath them. Saving items in the incorrect order can break the menu tree.
  139. *
  140. * @see menu_overview_form()
  141. */
  142. function menu_overview_form_submit($form, &$form_state) {
  143. // When dealing with saving menu items, the order in which these items are
  144. // saved is critical. If a changed child item is saved before its parent,
  145. // the child item could be saved with an invalid path past its immediate
  146. // parent. To prevent this, save items in the form in the same order they
  147. // are sent by $_POST, ensuring parents are saved first, then their children.
  148. // See http://drupal.org/node/181126#comment-632270
  149. $order = array_flip(array_keys($form_state['input'])); // Get the $_POST order.
  150. $form = array_merge($order, $form); // Update our original form with the new order.
  151. $updated_items = array();
  152. $fields = array('weight', 'plid');
  153. foreach (element_children($form) as $mlid) {
  154. if (isset($form[$mlid]['#item'])) {
  155. $element = $form[$mlid];
  156. // Update any fields that have changed in this menu item.
  157. foreach ($fields as $field) {
  158. if ($element[$field]['#value'] != $element[$field]['#default_value']) {
  159. $element['#item'][$field] = $element[$field]['#value'];
  160. $updated_items[$mlid] = $element['#item'];
  161. }
  162. }
  163. // Hidden is a special case, the value needs to be reversed.
  164. if ($element['hidden']['#value'] != $element['hidden']['#default_value']) {
  165. // Convert to integer rather than boolean due to PDO cast to string.
  166. $element['#item']['hidden'] = $element['hidden']['#value'] ? 0 : 1;
  167. $updated_items[$mlid] = $element['#item'];
  168. }
  169. }
  170. }
  171. // Save all our changed items to the database.
  172. foreach ($updated_items as $item) {
  173. $item['customized'] = 1;
  174. menu_link_save($item);
  175. }
  176. drupal_set_message(t('Your configuration has been saved.'));
  177. }
  178. /**
  179. * Returns HTML for the menu overview form into a table.
  180. *
  181. * @param $variables
  182. * An associative array containing:
  183. * - form: A render element representing the form.
  184. *
  185. * @ingroup themeable
  186. */
  187. function theme_menu_overview_form($variables) {
  188. $form = $variables['form'];
  189. drupal_add_tabledrag('menu-overview', 'match', 'parent', 'menu-plid', 'menu-plid', 'menu-mlid', TRUE, MENU_MAX_DEPTH - 1);
  190. drupal_add_tabledrag('menu-overview', 'order', 'sibling', 'menu-weight');
  191. $header = array(
  192. t('Menu link'),
  193. array('data' => t('Enabled'), 'class' => array('checkbox')),
  194. t('Weight'),
  195. array('data' => t('Operations'), 'colspan' => '3'),
  196. );
  197. $rows = array();
  198. foreach (element_children($form) as $mlid) {
  199. if (isset($form[$mlid]['hidden'])) {
  200. $element = &$form[$mlid];
  201. // Build a list of operations.
  202. $operations = array();
  203. foreach (element_children($element['operations']) as $op) {
  204. $operations[] = array('data' => drupal_render($element['operations'][$op]), 'class' => array('menu-operations'));
  205. }
  206. while (count($operations) < 2) {
  207. $operations[] = '';
  208. }
  209. // Add special classes to be used for tabledrag.js.
  210. $element['plid']['#attributes']['class'] = array('menu-plid');
  211. $element['mlid']['#attributes']['class'] = array('menu-mlid');
  212. $element['weight']['#attributes']['class'] = array('menu-weight');
  213. // Change the parent field to a hidden. This allows any value but hides the field.
  214. $element['plid']['#type'] = 'hidden';
  215. $row = array();
  216. $row[] = theme('indentation', array('size' => $element['#item']['depth'] - 1)) . drupal_render($element['title']);
  217. $row[] = array('data' => drupal_render($element['hidden']), 'class' => array('checkbox', 'menu-enabled'));
  218. $row[] = drupal_render($element['weight']) . drupal_render($element['plid']) . drupal_render($element['mlid']);
  219. $row = array_merge($row, $operations);
  220. $row = array_merge(array('data' => $row), $element['#attributes']);
  221. $row['class'][] = 'draggable';
  222. $rows[] = $row;
  223. }
  224. }
  225. $output = '';
  226. if (empty($rows)) {
  227. $rows[] = array(array('data' => $form['#empty_text'], 'colspan' => '7'));
  228. }
  229. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'menu-overview')));
  230. $output .= drupal_render_children($form);
  231. return $output;
  232. }
  233. /**
  234. * Menu callback; Build the menu link editing form.
  235. */
  236. function menu_edit_item($form, &$form_state, $type, $item, $menu) {
  237. if ($type == 'add' || empty($item)) {
  238. // This is an add form, initialize the menu link.
  239. $item = array('link_title' => '', 'mlid' => 0, 'plid' => 0, 'menu_name' => $menu['menu_name'], 'weight' => 0, 'link_path' => '', 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0);
  240. }
  241. $form['actions'] = array('#type' => 'actions');
  242. $form['link_title'] = array(
  243. '#type' => 'textfield',
  244. '#title' => t('Menu link title'),
  245. '#default_value' => $item['link_title'],
  246. '#description' => t('The text to be used for this link in the menu.'),
  247. '#required' => TRUE,
  248. );
  249. foreach (array('link_path', 'mlid', 'module', 'has_children', 'options') as $key) {
  250. $form[$key] = array('#type' => 'value', '#value' => $item[$key]);
  251. }
  252. // Any item created or edited via this interface is considered "customized".
  253. $form['customized'] = array('#type' => 'value', '#value' => 1);
  254. $form['original_item'] = array('#type' => 'value', '#value' => $item);
  255. $path = $item['link_path'];
  256. if (isset($item['options']['query'])) {
  257. $path .= '?' . drupal_http_build_query($item['options']['query']);
  258. }
  259. if (isset($item['options']['fragment'])) {
  260. $path .= '#' . $item['options']['fragment'];
  261. }
  262. if ($item['module'] == 'menu') {
  263. $form['link_path'] = array(
  264. '#type' => 'textfield',
  265. '#title' => t('Path'),
  266. '#default_value' => $path,
  267. '#description' => t('The path for this menu link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '<front>', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')),
  268. '#required' => TRUE,
  269. );
  270. $form['actions']['delete'] = array(
  271. '#type' => 'submit',
  272. '#value' => t('Delete'),
  273. '#access' => $item['mlid'],
  274. '#submit' => array('menu_item_delete_submit'),
  275. '#weight' => 10,
  276. );
  277. }
  278. else {
  279. $form['_path'] = array(
  280. '#type' => 'item',
  281. '#title' => t('Path'),
  282. '#description' => l($item['link_title'], $item['href'], $item['options']),
  283. );
  284. }
  285. $form['description'] = array(
  286. '#type' => 'textarea',
  287. '#title' => t('Description'),
  288. '#default_value' => isset($item['options']['attributes']['title']) ? $item['options']['attributes']['title'] : '',
  289. '#rows' => 1,
  290. '#description' => t('Shown when hovering over the menu link.'),
  291. );
  292. $form['enabled'] = array(
  293. '#type' => 'checkbox',
  294. '#title' => t('Enabled'),
  295. '#default_value' => !$item['hidden'],
  296. '#description' => t('Menu links that are not enabled will not be listed in any menu.'),
  297. );
  298. $form['expanded'] = array(
  299. '#type' => 'checkbox',
  300. '#title' => t('Show as expanded'),
  301. '#default_value' => $item['expanded'],
  302. '#description' => t('If selected and this menu link has children, the menu will always appear expanded.'),
  303. );
  304. // Generate a list of possible parents (not including this link or descendants).
  305. $options = menu_parent_options(menu_get_menus(), $item);
  306. $default = $item['menu_name'] . ':' . $item['plid'];
  307. if (!isset($options[$default])) {
  308. $default = 'navigation:0';
  309. }
  310. $form['parent'] = array(
  311. '#type' => 'select',
  312. '#title' => t('Parent link'),
  313. '#default_value' => $default,
  314. '#options' => $options,
  315. '#description' => t('The maximum depth for a link and all its children is fixed at !maxdepth. Some menu links may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)),
  316. '#attributes' => array('class' => array('menu-title-select')),
  317. );
  318. $form['weight'] = array(
  319. '#type' => 'weight',
  320. '#title' => t('Weight'),
  321. '#delta' => 50,
  322. '#default_value' => $item['weight'],
  323. '#description' => t('Optional. In the menu, the heavier links will sink and the lighter links will be positioned nearer the top.'),
  324. );
  325. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  326. return $form;
  327. }
  328. /**
  329. * Validate form values for a menu link being added or edited.
  330. */
  331. function menu_edit_item_validate($form, &$form_state) {
  332. $item = &$form_state['values'];
  333. $normal_path = drupal_get_normal_path($item['link_path']);
  334. if ($item['link_path'] != $normal_path) {
  335. drupal_set_message(t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', array('%link_path' => $item['link_path'], '%normal_path' => $normal_path)));
  336. $item['link_path'] = $normal_path;
  337. }
  338. if (!url_is_external($item['link_path'])) {
  339. $parsed_link = parse_url($item['link_path']);
  340. if (isset($parsed_link['query'])) {
  341. $item['options']['query'] = drupal_get_query_array($parsed_link['query']);
  342. }
  343. else {
  344. // Use unset() rather than setting to empty string
  345. // to avoid redundant serialized data being stored.
  346. unset($item['options']['query']);
  347. }
  348. if (isset($parsed_link['fragment'])) {
  349. $item['options']['fragment'] = $parsed_link['fragment'];
  350. }
  351. else {
  352. unset($item['options']['fragment']);
  353. }
  354. if ($item['link_path'] != $parsed_link['path']) {
  355. $item['link_path'] = $parsed_link['path'];
  356. }
  357. }
  358. if (!trim($item['link_path']) || !drupal_valid_path($item['link_path'], TRUE)) {
  359. form_set_error('link_path', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $item['link_path'])));
  360. }
  361. }
  362. /**
  363. * Submit function for the delete button on the menu item editing form.
  364. */
  365. function menu_item_delete_submit($form, &$form_state) {
  366. $form_state['redirect'] = 'admin/structure/menu/item/' . $form_state['values']['mlid'] . '/delete';
  367. }
  368. /**
  369. * Process menu and menu item add/edit form submissions.
  370. */
  371. function menu_edit_item_submit($form, &$form_state) {
  372. $item = &$form_state['values'];
  373. // The value of "hidden" is the opposite of the value
  374. // supplied by the "enabled" checkbox.
  375. $item['hidden'] = (int) !$item['enabled'];
  376. unset($item['enabled']);
  377. $item['options']['attributes']['title'] = $item['description'];
  378. list($item['menu_name'], $item['plid']) = explode(':', $item['parent']);
  379. if (!menu_link_save($item)) {
  380. drupal_set_message(t('There was an error saving the menu link.'), 'error');
  381. }
  382. else {
  383. drupal_set_message(t('Your configuration has been saved.'));
  384. }
  385. $form_state['redirect'] = 'admin/structure/menu/manage/' . $item['menu_name'];
  386. }
  387. /**
  388. * Menu callback; Build the form that handles the adding/editing of a custom menu.
  389. */
  390. function menu_edit_menu($form, &$form_state, $type, $menu = array()) {
  391. $system_menus = menu_list_system_menus();
  392. $menu += array(
  393. 'menu_name' => '',
  394. 'old_name' => !empty($menu['menu_name']) ? $menu['menu_name'] : '',
  395. 'title' => '',
  396. 'description' => '',
  397. );
  398. // Allow menu_edit_menu_submit() and other form submit handlers to determine
  399. // whether the menu already exists.
  400. $form['#insert'] = empty($menu['old_name']);
  401. $form['old_name'] = array(
  402. '#type' => 'value',
  403. '#value' => $menu['old_name'],
  404. );
  405. $form['title'] = array(
  406. '#type' => 'textfield',
  407. '#title' => t('Title'),
  408. '#default_value' => $menu['title'],
  409. '#required' => TRUE,
  410. // The title of a system menu cannot be altered.
  411. '#access' => !isset($system_menus[$menu['menu_name']]),
  412. );
  413. $form['menu_name'] = array(
  414. '#type' => 'machine_name',
  415. '#title' => t('Menu name'),
  416. '#default_value' => $menu['menu_name'],
  417. '#maxlength' => MENU_MAX_MENU_NAME_LENGTH_UI,
  418. '#description' => t('A unique name to construct the URL for the menu. It must only contain lowercase letters, numbers and hyphens.'),
  419. '#machine_name' => array(
  420. 'exists' => 'menu_edit_menu_name_exists',
  421. 'source' => array('title'),
  422. 'label' => t('URL path'),
  423. 'replace_pattern' => '[^a-z0-9-]+',
  424. 'replace' => '-',
  425. ),
  426. // A menu's machine name cannot be changed.
  427. '#disabled' => !empty($menu['old_name']) || isset($system_menus[$menu['menu_name']]),
  428. );
  429. $form['description'] = array(
  430. '#type' => 'textarea',
  431. '#title' => t('Description'),
  432. '#default_value' => $menu['description'],
  433. );
  434. $form['actions'] = array('#type' => 'actions');
  435. $form['actions']['submit'] = array(
  436. '#type' => 'submit',
  437. '#value' => t('Save'),
  438. );
  439. // Only custom menus may be deleted.
  440. $form['actions']['delete'] = array(
  441. '#type' => 'submit',
  442. '#value' => t('Delete'),
  443. '#access' => $type == 'edit' && !isset($system_menus[$menu['menu_name']]),
  444. '#submit' => array('menu_custom_delete_submit'),
  445. );
  446. return $form;
  447. }
  448. /**
  449. * Submit function for the 'Delete' button on the menu editing form.
  450. */
  451. function menu_custom_delete_submit($form, &$form_state) {
  452. $form_state['redirect'] = 'admin/structure/menu/manage/' . $form_state['values']['menu_name'] . '/delete';
  453. }
  454. /**
  455. * Menu callback; check access and get a confirm form for deletion of a custom menu.
  456. */
  457. function menu_delete_menu_page($menu) {
  458. // System-defined menus may not be deleted.
  459. $system_menus = menu_list_system_menus();
  460. if (isset($system_menus[$menu['menu_name']])) {
  461. drupal_access_denied();
  462. return;
  463. }
  464. return drupal_get_form('menu_delete_menu_confirm', $menu);
  465. }
  466. /**
  467. * Build a confirm form for deletion of a custom menu.
  468. */
  469. function menu_delete_menu_confirm($form, &$form_state, $menu) {
  470. $form['#menu'] = $menu;
  471. $caption = '';
  472. $num_links = db_query("SELECT COUNT(*) FROM {menu_links} WHERE menu_name = :menu", array(':menu' => $menu['menu_name']))->fetchField();
  473. if ($num_links) {
  474. $caption .= '<p>' . format_plural($num_links, '<strong>Warning:</strong> There is currently 1 menu link in %title. It will be deleted (system-defined items will be reset).', '<strong>Warning:</strong> There are currently @count menu links in %title. They will be deleted (system-defined links will be reset).', array('%title' => $menu['title'])) . '</p>';
  475. }
  476. $caption .= '<p>' . t('This action cannot be undone.') . '</p>';
  477. return confirm_form($form, t('Are you sure you want to delete the custom menu %title?', array('%title' => $menu['title'])), 'admin/structure/menu/manage/' . $menu['menu_name'], $caption, t('Delete'));
  478. }
  479. /**
  480. * Delete a custom menu and all links in it.
  481. */
  482. function menu_delete_menu_confirm_submit($form, &$form_state) {
  483. $menu = $form['#menu'];
  484. $form_state['redirect'] = 'admin/structure/menu';
  485. // System-defined menus may not be deleted - only menus defined by this module.
  486. $system_menus = menu_list_system_menus();
  487. if (isset($system_menus[$menu['menu_name']]) || !(db_query("SELECT 1 FROM {menu_custom} WHERE menu_name = :menu", array(':menu' => $menu['menu_name']))->fetchField())) {
  488. return;
  489. }
  490. // Reset all the menu links defined by the system via hook_menu().
  491. $result = db_query("SELECT * FROM {menu_links} ml INNER JOIN {menu_router} m ON ml.router_path = m.path WHERE ml.menu_name = :menu AND ml.module = 'system' ORDER BY m.number_parts ASC", array(':menu' => $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
  492. foreach ($result as $link) {
  493. menu_reset_item($link);
  494. }
  495. // Delete all links to the overview page for this menu.
  496. $result = db_query("SELECT mlid FROM {menu_links} ml WHERE ml.link_path = :link", array(':link' => 'admin/structure/menu/manage/' . $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
  497. foreach ($result as $link) {
  498. menu_link_delete($link['mlid']);
  499. }
  500. // Delete the custom menu and all its menu links.
  501. menu_delete($menu);
  502. $t_args = array('%title' => $menu['title']);
  503. drupal_set_message(t('The custom menu %title has been deleted.', $t_args));
  504. watchdog('menu', 'Deleted custom menu %title and all its menu links.', $t_args, WATCHDOG_NOTICE);
  505. }
  506. /**
  507. * Returns whether a menu name already exists.
  508. *
  509. * @see menu_edit_menu()
  510. * @see form_validate_machine_name()
  511. */
  512. function menu_edit_menu_name_exists($value) {
  513. // 'menu-' is added to the menu name to avoid name-space conflicts.
  514. $value = 'menu-' . $value;
  515. $custom_exists = db_query_range('SELECT 1 FROM {menu_custom} WHERE menu_name = :menu', 0, 1, array(':menu' => $value))->fetchField();
  516. $link_exists = db_query_range("SELECT 1 FROM {menu_links} WHERE menu_name = :menu", 0, 1, array(':menu' => $value))->fetchField();
  517. return $custom_exists || $link_exists;
  518. }
  519. /**
  520. * Submit function for adding or editing a custom menu.
  521. */
  522. function menu_edit_menu_submit($form, &$form_state) {
  523. $menu = $form_state['values'];
  524. $path = 'admin/structure/menu/manage/';
  525. if ($form['#insert']) {
  526. // Add 'menu-' to the menu name to help avoid name-space conflicts.
  527. $menu['menu_name'] = 'menu-' . $menu['menu_name'];
  528. $link['link_title'] = $menu['title'];
  529. $link['link_path'] = $path . $menu['menu_name'];
  530. $link['router_path'] = $path . '%';
  531. $link['module'] = 'menu';
  532. $link['plid'] = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :link AND module = :module", array(
  533. ':link' => 'admin/structure/menu',
  534. ':module' => 'system'
  535. ))
  536. ->fetchField();
  537. menu_link_save($link);
  538. menu_save($menu);
  539. }
  540. else {
  541. menu_save($menu);
  542. $result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path", array(':path' => $path . $menu['menu_name']), array('fetch' => PDO::FETCH_ASSOC));
  543. foreach ($result as $m) {
  544. $link = menu_link_load($m['mlid']);
  545. $link['link_title'] = $menu['title'];
  546. menu_link_save($link);
  547. }
  548. }
  549. drupal_set_message(t('Your configuration has been saved.'));
  550. $form_state['redirect'] = $path . $menu['menu_name'];
  551. }
  552. /**
  553. * Menu callback; Check access and present a confirm form for deleting a menu link.
  554. */
  555. function menu_item_delete_page($item) {
  556. // Links defined via hook_menu may not be deleted. Updated items are an
  557. // exception, as they can be broken.
  558. if ($item['module'] == 'system' && !$item['updated']) {
  559. drupal_access_denied();
  560. return;
  561. }
  562. return drupal_get_form('menu_item_delete_form', $item);
  563. }
  564. /**
  565. * Build a confirm form for deletion of a single menu link.
  566. */
  567. function menu_item_delete_form($form, &$form_state, $item) {
  568. $form['#item'] = $item;
  569. return confirm_form($form, t('Are you sure you want to delete the custom menu link %item?', array('%item' => $item['link_title'])), 'admin/structure/menu/manage/' . $item['menu_name']);
  570. }
  571. /**
  572. * Process menu delete form submissions.
  573. */
  574. function menu_item_delete_form_submit($form, &$form_state) {
  575. $item = $form['#item'];
  576. menu_link_delete($item['mlid']);
  577. $t_args = array('%title' => $item['link_title']);
  578. drupal_set_message(t('The menu link %title has been deleted.', $t_args));
  579. watchdog('menu', 'Deleted menu link %title.', $t_args, WATCHDOG_NOTICE);
  580. $form_state['redirect'] = 'admin/structure/menu/manage/' . $item['menu_name'];
  581. }
  582. /**
  583. * Menu callback; reset a single modified menu link.
  584. */
  585. function menu_reset_item_confirm($form, &$form_state, $item) {
  586. $form['item'] = array('#type' => 'value', '#value' => $item);
  587. return confirm_form($form, t('Are you sure you want to reset the link %item to its default values?', array('%item' => $item['link_title'])), 'admin/structure/menu/manage/' . $item['menu_name'], t('Any customizations will be lost. This action cannot be undone.'), t('Reset'));
  588. }
  589. /**
  590. * Process menu reset item form submissions.
  591. */
  592. function menu_reset_item_confirm_submit($form, &$form_state) {
  593. $item = $form_state['values']['item'];
  594. $new_item = menu_reset_item($item);
  595. drupal_set_message(t('The menu link was reset to its default settings.'));
  596. $form_state['redirect'] = 'admin/structure/menu/manage/' . $new_item['menu_name'];
  597. }
  598. /**
  599. * Menu callback; Build the form presenting menu configuration options.
  600. */
  601. function menu_configure() {
  602. $form['intro'] = array(
  603. '#type' => 'item',
  604. '#markup' => t('The menu module allows on-the-fly creation of menu links in the content authoring forms. To configure these settings for a particular content type, visit the <a href="@content-types">Content types</a> page, click the <em>edit</em> link for the content type, and go to the <em>Menu settings</em> section.', array('@content-types' => url('admin/structure/types'))),
  605. );
  606. $menu_options = menu_get_menus();
  607. $main = variable_get('menu_main_links_source', 'main-menu');
  608. $form['menu_main_links_source'] = array(
  609. '#type' => 'select',
  610. '#title' => t('Source for the Main links'),
  611. '#default_value' => variable_get('menu_main_links_source', 'main-menu'),
  612. '#empty_option' => t('No Main links'),
  613. '#options' => $menu_options,
  614. '#tree' => FALSE,
  615. '#description' => t('Select what should be displayed as the Main links (typically at the top of the page).'),
  616. );
  617. $form['menu_secondary_links_source'] = array(
  618. '#type' => 'select',
  619. '#title' => t('Source for the Secondary links'),
  620. '#default_value' => variable_get('menu_secondary_links_source', 'user-menu'),
  621. '#empty_option' => t('No Secondary links'),
  622. '#options' => $menu_options,
  623. '#tree' => FALSE,
  624. '#description' => t('Select the source for the Secondary links. An advanced option allows you to use the same source for both Main links (currently %main) and Secondary links: if your source menu has two levels of hierarchy, the top level menu links will appear in the Main links, and the children of the active link will appear in the Secondary links.', array('%main' => $main ? $menu_options[$main] : 'none')),
  625. );
  626. return system_settings_form($form);
  627. }