menu.admin.inc 28 KB

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