menu_position.admin.inc

Provides infrequently used functions and hooks for menu_position.

Functions

Namesort descending Description
menu_position_add_menu_link Adds a menu position rule.
menu_position_add_rule Adds a menu position rule.
menu_position_add_rule_form Menu callback; Adds rules.
menu_position_delete_rule Deletes a menu position rule.
menu_position_delete_rule_form Menu callback: confirms deletion of rule.
menu_position_delete_rule_form_submit Handles form submission for menu_position_delete_rule_form().
menu_position_edit_item_submit Process menu and menu item add/edit form submissions for menu_position links.
menu_position_edit_menu_link Adds a menu position rule.
menu_position_edit_rule Edits a menu position rule.
menu_position_edit_rule_form Menu callback; Edits rules.
menu_position_enable_helper Fix rules after module has been re-enabled.
menu_position_read_rule Retrieves a menu position rule from the database.
menu_position_read_rules Retrieves a menu position rule from the database.
menu_position_router Routes menu_position links to homepage; normally overridden.
menu_position_rules_form Form definition: orders rules.
menu_position_rules_form_callback Menu callback: orders rules.
menu_position_rules_form_submit Handles form submission for menu_position_rules_form().
menu_position_rule_form Returns form to add or edit a menu position rule.
menu_position_rule_form_submit Handles form submission for menu_position_rule_form().
menu_position_rule_form_validate Validates the form for menu_position_rule_form().
menu_position_settings_form Form definition: global settings for Menu position rules.
theme_menu_position_rules_order Returns HTML for the menu position rules form.
_menu_position_form_menu_edit_item_alter Implements hook_form_menu_edit_item_alter().
_menu_position_form_menu_overview_form_alter Implements hook_form_FORM_ID_alter().
_menu_position_menu_link_update Implements hook_menu_link_update().

File

sites/all/modules/local_contrib/menu_position/menu_position.admin.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides infrequently used functions and hooks for menu_position.
  5. */
  6. /**
  7. * Routes menu_position links to homepage; normally overridden.
  8. */
  9. function menu_position_router() {
  10. drupal_goto('<front>');
  11. }
  12. /**
  13. * Form definition: global settings for Menu position rules.
  14. */
  15. function menu_position_settings_form($form, &$form_state) {
  16. $form = array();
  17. $form['menu_position_active_link_display'] = array(
  18. '#type' => 'radios',
  19. '#title' => t('When a menu position rule matches:'),
  20. '#options' => array(
  21. 'child' => t("Insert the current page's title into the menu tree."),
  22. 'parent' => t('Mark the rule\'s parent menu item as being "active".'),
  23. 'none' => t('Don\'t mark any menu item as being "active".'),
  24. ),
  25. '#default_value' => variable_get('menu_position_active_link_display', 'child'),
  26. '#description' => t("By default, a matching menu position rule will insert the current page's title into the menu tree just below the rule's parent menu item."),
  27. );
  28. return system_settings_form($form);
  29. }
  30. /**
  31. * Implements hook_form_FORM_ID_alter().
  32. */
  33. function _menu_position_form_menu_overview_form_alter(&$form, &$form_state) {
  34. // Retrieve all of the rules' mlids.
  35. $rules = db_query('SELECT rid, mlid FROM {menu_position_rules} WHERE enabled = :enabled ORDER BY weight, rid', array(':enabled' => 1));
  36. foreach ($rules as $rule) {
  37. $mlid = $rule->mlid;
  38. if (!empty($form['mlid:' . $mlid]['#item']['mlid']) && $mlid == $form['mlid:' . $mlid]['#item']['mlid']) {
  39. // Remove link and "disabled" text from the menu item's title.
  40. $form['mlid:' . $mlid]['title']['#markup'] = strip_tags(str_replace(' (' . t('disabled') . ')', '', $form['mlid:' . $mlid]['title']['#markup']));
  41. // Ensure that the menu item cannot be enabled.
  42. $form['mlid:' . $mlid]['hidden']['#default_value'] = TRUE;
  43. $form['mlid:' . $mlid]['hidden']['#disabled'] = TRUE;
  44. // Alter the edit link for this menu item.
  45. $form['mlid:' . $mlid]['operations']['edit']['#href'] = 'admin/structure/menu-position/edit/' . $rule->rid;
  46. $form['mlid:' . $mlid]['operations']['edit']['#options'] = array('query' => array('destination' => $_GET['q']));
  47. }
  48. }
  49. }
  50. /**
  51. * Implements hook_form_menu_edit_item_alter().
  52. *
  53. * This handles the edge case of another module accidentally exposing (or of a
  54. * user hacking the URL to) the standard "menu link edit" form for a menu
  55. * position rule's hidden menu link. We alter the form so that it is not posible
  56. * for the link to be edited.
  57. */
  58. function _menu_position_form_menu_edit_item_alter(&$form, &$form_state) {
  59. if ($form['mlid']['#value'] == 0) {
  60. return;
  61. }
  62. // Retrieve all of the rules' mlids.
  63. $mlids = db_query('SELECT mlid FROM {menu_position_rules} WHERE mlid = :mlid ORDER BY weight, rid', array(':mlid' => (int) $form['mlid']['#value']))->fetchAll();
  64. if (!empty($mlids)) {
  65. // If the form hasn't been submitted, display a warning.
  66. if (empty($form_state['input'])) {
  67. drupal_set_message(t('This menu item cannot be edited.'), 'warning');
  68. }
  69. // Disable all the normal form elements.
  70. foreach (array('link_title', 'description', 'enabled', 'expanded', 'parent', 'weight') as $key) {
  71. $form[$key]['#disabled'] = TRUE;
  72. }
  73. // Remove the validator.
  74. $key = array_search('menu_edit_item_validate', $form['#validate']);
  75. if ($key !== FALSE) {
  76. unset($form['#validate'][$key]);
  77. }
  78. // Replace the standard submit handler with our own.
  79. $key = array_search('menu_edit_item_submit', $form['#submit']);
  80. if ($key !== FALSE) {
  81. $form['#submit'][$key] = 'menu_position_edit_item_submit';
  82. }
  83. // Replace the Save button with a Cancel button.
  84. unset($form['actions']['submit']);
  85. $form['actions']['cancel'] = array(
  86. '#type' => 'submit',
  87. '#value' => t('Cancel'),
  88. );
  89. }
  90. }
  91. /**
  92. * Implements hook_menu_link_update().
  93. */
  94. function _menu_position_menu_link_update($link) {
  95. $rules = db_query('SELECT rid, plid FROM {menu_position_rules} WHERE mlid = :mlid ORDER BY weight, rid', array(':mlid' => $link['mlid']));
  96. foreach ($rules as $rule) {
  97. // Check if the user has altered the parent menu item.
  98. if ($link['plid'] != $rule->plid) {
  99. // Update the rule with the new parent.
  100. db_update('menu_position_rules')
  101. ->fields(array(
  102. 'menu_name' => $link['menu_name'],
  103. 'plid' => $link['plid'],
  104. ))
  105. ->condition('rid', $rule->rid)
  106. ->execute();
  107. }
  108. }
  109. }
  110. /**
  111. * Process menu and menu item add/edit form submissions for menu_position links.
  112. */
  113. function menu_position_edit_item_submit($form, &$form_state) {
  114. // Redirect to the menu edit form and display a message.
  115. list($menu_name, ) = explode(':', $form['parent']['#default_value']);
  116. $form_state['redirect'] = 'admin/structure/menu/manage/' . $menu_name;
  117. drupal_set_message(t('Your configuration was not saved.'), 'error');
  118. }
  119. /**
  120. * Fix rules after module has been re-enabled.
  121. *
  122. * During menu_position_enable(), existing rules are flagged with a zero-value
  123. * mlid. We fix that here.
  124. */
  125. function menu_position_enable_helper() {
  126. // Find rules with zero-value menu links.
  127. $rules = db_query('SELECT rid, plid, admin_title FROM {menu_position_rules} WHERE enabled = :enabled AND mlid = :mlid', array(':enabled' => 1, ':mlid' => 0))->fetchAll();
  128. if (!empty($rules)) {
  129. drupal_set_message(t('Existing menu position rules were discovered and have now been re-configured so they will continue to work.'));
  130. }
  131. foreach ($rules as $rule) {
  132. $mlid = menu_position_add_menu_link($rule->rid, $rule->plid, $rule->admin_title);
  133. db_update('menu_position_rules')
  134. ->fields(array('mlid' => $mlid))
  135. ->condition('rid', $rule->rid)
  136. ->execute();
  137. }
  138. }
  139. /**
  140. * Menu callback: orders rules.
  141. */
  142. function menu_position_rules_form_callback() {
  143. // This is a total hack. @see menu_position_enable(). You shouldn't be doing
  144. // non-Form API stuff in a form definition. So we've created this wrapper
  145. // callback to run the hack and then return the form definition.
  146. menu_position_enable_helper();
  147. return drupal_get_form('menu_position_rules_form');
  148. }
  149. /**
  150. * Form definition: orders rules.
  151. */
  152. function menu_position_rules_form($form, &$form_state) {
  153. // We're re-using classes from the menu module.
  154. $form['#attached']['css'] = array(drupal_get_path('module', 'menu') . '/menu.css');
  155. $rules = db_query('SELECT rid, admin_title, plid, menu_name, enabled, weight FROM {menu_position_rules} ORDER BY weight, rid')->fetchAll();
  156. $delta = count($rules);
  157. $menus = menu_get_menus();
  158. // Default message if no rules.
  159. if ($delta == 0) {
  160. $form['rules'] = array(
  161. '#markup' => '<p>' . t('No rules have been created yet.') . '</p>',
  162. );
  163. }
  164. else {
  165. $form['rules'] = array(
  166. '#tree' => TRUE,
  167. '#theme' => 'menu_position_rules_order',
  168. );
  169. foreach ($rules as $rule) {
  170. $menu_link = menu_link_load($rule->plid);
  171. $form['rules'][$rule->rid] = array(
  172. 'title' => array(
  173. '#markup' => '<strong>' . check_plain($rule->admin_title) . '</strong> (' . t('Positioned under: %title', array('%title' => check_plain($menu_link['title']))) . ')',
  174. ),
  175. 'menu_name' => array(
  176. '#markup' => check_plain($menus[$rule->menu_name]),
  177. ),
  178. 'enabled' => array(
  179. '#type' => 'checkbox',
  180. '#default_value' => $rule->enabled,
  181. ),
  182. 'weight' => array(
  183. '#type' => 'weight',
  184. '#default_value' => $rule->weight,
  185. '#delta' => max($delta, 5),
  186. '#id' => 'edit-rule-' . $rule->rid,
  187. ),
  188. 'operations' => array(
  189. 'edit-link' => array(
  190. '#type' => 'link',
  191. '#title' => t('edit'),
  192. '#href' => 'admin/structure/menu-position/edit/' . $rule->rid,
  193. ),
  194. 'delete-link' => array(
  195. '#type' => 'link',
  196. '#title' => t('delete'),
  197. '#href' => 'admin/structure/menu-position/delete/' . $rule->rid,
  198. ),
  199. ),
  200. );
  201. }
  202. $form['submit'] = array(
  203. '#type' => 'submit',
  204. '#value' => t('Save'),
  205. );
  206. }
  207. return $form;
  208. }
  209. /**
  210. * Handles form submission for menu_position_rules_form().
  211. */
  212. function menu_position_rules_form_submit($form, &$form_state) {
  213. foreach ($form_state['values']['rules'] as $rid => $rule) {
  214. $fields = array(
  215. 'enabled' => $rule['enabled'],
  216. 'weight' => $rule['weight'],
  217. );
  218. $db_rule = db_query('SELECT * FROM {menu_position_rules} WHERE rid = :rid', array(':rid' => $rid))->fetchObject();
  219. if (!$rule['enabled']) {
  220. // If the rule has been disabled, remove the menu link.
  221. menu_link_delete($db_rule->mlid);
  222. }
  223. elseif (!$db_rule->enabled) {
  224. // If the rule has been enabled, add a menu link.
  225. $fields['mlid'] = menu_position_add_menu_link($rid, $db_rule->plid, $db_rule->admin_title);
  226. }
  227. db_update('menu_position_rules')
  228. ->fields($fields)
  229. ->condition('rid', $rid)
  230. ->execute();
  231. }
  232. drupal_set_message(t('The new rules ordering has been applied.'));
  233. }
  234. /**
  235. * Returns HTML for the menu position rules form.
  236. */
  237. function theme_menu_position_rules_order($variables) {
  238. $element = $variables['element'];
  239. drupal_add_tabledrag('menu-position-rules', 'order', 'sibling', 'rule-weight');
  240. $variables = array(
  241. 'header' => array(
  242. t('Rule'),
  243. t('Affected menu'),
  244. array(
  245. 'data' => t('Enabled'),
  246. 'class' => array('checkbox'),
  247. ),
  248. t('Weight'),
  249. array(
  250. 'data' => t('Operations'),
  251. 'colspan' => '2',
  252. ),
  253. ),
  254. 'rows' => array(),
  255. 'attributes' => array('id' => 'menu-position-rules'),
  256. );
  257. // Generate table of draggable menu names.
  258. foreach (element_children($element) as $rule) {
  259. // Add special classes to be used for tabledrag.js.
  260. $element[$rule]['weight']['#attributes']['class'] = array('rule-weight');
  261. // Render the title, enabled, and weight columns.
  262. $data = array(
  263. drupal_render($element[$rule]['title']),
  264. drupal_render($element[$rule]['menu_name']),
  265. array(
  266. 'data' => drupal_render($element[$rule]['enabled']),
  267. 'class' => array('checkbox', 'menu-enabled'),
  268. ),
  269. drupal_render($element[$rule]['weight']),
  270. );
  271. // Render the operations links.
  272. foreach (element_children($element[$rule]['operations']) as $op) {
  273. $data[] = array(
  274. 'data' => drupal_render($element[$rule]['operations'][$op]),
  275. 'class' => array('menu-operations'),
  276. );
  277. }
  278. $variables['rows'][] = array(
  279. 'data' => $data,
  280. 'class' => array('draggable'),
  281. );
  282. }
  283. return theme('table', $variables);
  284. }
  285. /**
  286. * Menu callback; Adds rules.
  287. */
  288. function menu_position_add_rule_form($form, &$form_state) {
  289. return menu_position_rule_form($form, $form_state);
  290. }
  291. /**
  292. * Menu callback; Edits rules.
  293. */
  294. function menu_position_edit_rule_form($form, &$form_state, $rid = 0) {
  295. // Make sure rid is set.
  296. if ($rid == 0) {
  297. drupal_goto('admin/structure/menu-position');
  298. return;
  299. }
  300. // Grab the rule from the database.
  301. $form_state['#menu-position-rule'] = menu_position_read_rule($rid);
  302. return menu_position_rule_form($form, $form_state);
  303. }
  304. /**
  305. * Returns form to add or edit a menu position rule.
  306. */
  307. function menu_position_rule_form($form, &$form_state) {
  308. // Set the default values.
  309. $rid = !empty($form_state['#menu-position-rule']['rid']) ? $form_state['#menu-position-rule']['rid'] : '';
  310. $admin_title = !empty($form_state['#menu-position-rule']['admin_title']) ? $form_state['#menu-position-rule']['admin_title'] : '';
  311. $menu_name = !empty($form_state['#menu-position-rule']['menu_name']) ? $form_state['#menu-position-rule']['menu_name'] : '';
  312. $plid = !empty($form_state['#menu-position-rule']['plid']) ? $form_state['#menu-position-rule']['plid'] : NULL;
  313. $mlid = !empty($form_state['#menu-position-rule']['mlid']) ? $form_state['#menu-position-rule']['mlid'] : NULL;
  314. $form['rid'] = array(
  315. '#type' => 'hidden',
  316. '#value' => $rid,
  317. );
  318. $form['admin_title'] = array(
  319. '#type' => 'textfield',
  320. '#default_value' => $admin_title,
  321. '#title' => t('Administrative title'),
  322. '#description' => t('This title will be used administratively to identify this rule.'),
  323. '#required' => TRUE,
  324. );
  325. // Parent menu item.
  326. if ($mlid) {
  327. $options = menu_parent_options(menu_get_menus(), menu_link_load($mlid));
  328. $default = $menu_name . ':' . $plid;
  329. }
  330. else {
  331. $options = menu_parent_options(menu_get_menus(), array('mlid' => 0));
  332. $default = 'main-menu:0';
  333. }
  334. $form['plid'] = array(
  335. '#type' => 'select',
  336. '#title' => t('Parent menu item'),
  337. '#required' => TRUE,
  338. '#options' => $options,
  339. '#default_value' => $default,
  340. '#description' => t('Select the place in the menu where the rule should position its menu links.'),
  341. );
  342. // Place holder for all condition plug-ins.
  343. // Visibility settings.
  344. $form['conditions_title'] = array(
  345. '#type' => 'item',
  346. '#title' => t('Conditions'),
  347. '#description' => t('All the conditions must be met before a rule is applied.'),
  348. );
  349. $form['conditions'] = array(
  350. '#type' => 'vertical_tabs',
  351. );
  352. $form['submit'] = array(
  353. '#type' => 'submit',
  354. '#value' => t('Save'),
  355. );
  356. if ($rid) {
  357. $form['delete'] = array(
  358. '#type' => 'submit',
  359. '#value' => t('Delete'),
  360. );
  361. }
  362. // Add conditions.
  363. foreach (menu_position_get_plugins() as $plugin) {
  364. // Load the required include file.
  365. if (!empty($plugin['file'])) {
  366. $file = pathinfo($plugin['file']);
  367. // Allow plugins to be in a sub-directory.
  368. if ($file['dirname']) {
  369. $file['filename'] = $file['dirname'] . '/' . $file['filename'];
  370. }
  371. module_load_include($file['extension'], $plugin['module'], $file['filename']);
  372. }
  373. // Call form callback to add additional form elements.
  374. $function = $plugin['form_callback'];
  375. if (function_exists($function)) {
  376. $function($form, $form_state);
  377. }
  378. }
  379. // Form validation and submission.
  380. $form['#validate'][] = 'menu_position_rule_form_validate';
  381. $form['#submit'][] = 'menu_position_rule_form_submit';
  382. return $form;
  383. }
  384. /**
  385. * Validates the form for menu_position_rule_form().
  386. */
  387. function menu_position_rule_form_validate($form, &$form_state) {
  388. // Check if the user deleted the rule.
  389. if (!empty($form['delete']) && $form_state['values']['op'] == $form['delete']['#value']) {
  390. drupal_goto('admin/structure/menu-position/delete/' . $form_state['values']['rid']);
  391. return;
  392. }
  393. // Don't allow the user to select a menu name instead of a menu item.
  394. list($menu_name, $plid) = explode(':', $form_state['values']['plid']);
  395. if ($plid == 0) {
  396. form_set_error('plid', t('Please select a menu item. You have selected the name of a menu.'));
  397. }
  398. }
  399. /**
  400. * Handles form submission for menu_position_rule_form().
  401. */
  402. function menu_position_rule_form_submit($form, &$form_state) {
  403. list($menu_name, $plid) = explode(':', $form_state['values']['plid']);
  404. $rule = array(
  405. 'admin_title' => $form_state['values']['admin_title'],
  406. 'conditions' => isset($form_state['values']['conditions']) ? $form_state['values']['conditions'] : array(),
  407. 'menu_name' => $menu_name,
  408. 'plid' => $plid,
  409. );
  410. // Add the rule to the database.
  411. if ($form_state['values']['rid'] == '') {
  412. menu_position_add_rule($rule);
  413. drupal_set_message(t('Rule has been added.'));
  414. }
  415. // Update an exisiting rule.
  416. else {
  417. $rule['rid'] = $form_state['values']['rid'];
  418. menu_position_edit_rule($rule);
  419. drupal_set_message(t('Rule has been modified.'));
  420. }
  421. $form_state['redirect'] = 'admin/structure/menu-position';
  422. }
  423. /**
  424. * Adds a menu position rule.
  425. *
  426. * @param $rule
  427. * An associate array defining the new rule to be created. Must contain the
  428. * following keys:
  429. * - admin_title: The administrative title of the rule.
  430. * - conditions: An associative array whose keys are the machine names of the
  431. * plugins actively configured in the rule. The value of each array element
  432. * is array containing the necessary variables for that plugin.
  433. * - menu_name: The machine name of the menu where this rule is positioned.
  434. * - plid: The mlid of the parent menu link specified in the rule.
  435. */
  436. function menu_position_add_rule($rule) {
  437. $fields = array(
  438. 'admin_title' => $rule['admin_title'],
  439. 'conditions' => serialize($rule['conditions']),
  440. 'menu_name' => $rule['menu_name'],
  441. 'plid' => $rule['plid'],
  442. );
  443. $rid = db_insert('menu_position_rules')
  444. ->fields($fields)
  445. ->execute();
  446. $mlid = menu_position_add_menu_link($rid, $rule['plid'], $rule['admin_title']);
  447. // Now add the mlid back to the rule.
  448. db_update('menu_position_rules')
  449. ->fields(array('mlid' => $mlid))
  450. ->condition('rid', $rid)
  451. ->execute();
  452. }
  453. /**
  454. * Adds a menu position rule.
  455. *
  456. * @param $rid
  457. * ID of the rule needing a menu link.
  458. * @param $plid
  459. * The mlid of the parent menu link specified in the rule.
  460. * @param $admin_title
  461. * The administrative title of the rule.
  462. * @return
  463. * The mlid of the rule's new menu link.
  464. */
  465. function menu_position_add_menu_link($rid, $plid, $admin_title) {
  466. // Add a menu link to handle matching pages. Passing NULL as the mlid will
  467. // cause menu_link_save() to add a new menu link.
  468. return menu_position_edit_menu_link($rid, NULL, $plid, $admin_title);
  469. }
  470. /**
  471. * Retrieves a menu position rule from the database.
  472. *
  473. * @param $rid
  474. * The ID of the requested rule.
  475. * @return
  476. * An associative array for the requested rule.
  477. */
  478. function menu_position_read_rule($rid) {
  479. $rule = db_query('SELECT * FROM {menu_position_rules} WHERE rid = :rid', array(':rid' => $rid))->fetchAssoc();
  480. $rule['conditions'] = unserialize($rule['conditions']);
  481. return $rule;
  482. }
  483. /**
  484. * Retrieves a menu position rule from the database.
  485. *
  486. * @return
  487. * An associative array for all rules with keys equal to the rid of each rule.
  488. */
  489. function menu_position_read_rules() {
  490. $query = db_query('SELECT * FROM {menu_position_rules} ORDER BY weight, rid');
  491. $rules = array();
  492. foreach ($query as $rule) {
  493. $rule['conditions'] = unserialize($rule['conditions']);
  494. $rules[$rule['rid']] = $rule;
  495. }
  496. return $rules;
  497. }
  498. /**
  499. * Edits a menu position rule.
  500. *
  501. * @param $rule
  502. * An associate array defining the rule to be edited. Must contain the
  503. * following keys:
  504. * - rid: The rule ID.
  505. * - admin_title: The administrative title of the rule.
  506. * - conditions: An associative array whose keys are the machine names of the
  507. * plugins actively configured in the rule. The value of each array element
  508. * is array containing the necessary variables for that plugin.
  509. * - menu_name: The machine name of the menu where this rule is positioned.
  510. * - plid: The mlid of the parent menu link specified in the rule.
  511. */
  512. function menu_position_edit_rule($rule) {
  513. $fields = array(
  514. 'admin_title' => $rule['admin_title'],
  515. 'conditions' => serialize($rule['conditions']),
  516. 'menu_name' => $rule['menu_name'],
  517. 'plid' => $rule['plid'],
  518. );
  519. // Update the rule.
  520. db_update('menu_position_rules')
  521. ->condition('rid', $rule['rid'])
  522. ->fields($fields)
  523. ->execute();
  524. // Update the link.
  525. $mlid = db_query('SELECT mlid FROM {menu_position_rules} WHERE rid = :rid', array(':rid' => $rule['rid']))->fetchField();
  526. menu_position_edit_menu_link($rule['rid'], $mlid, $rule['plid'], $rule['admin_title']);
  527. }
  528. /**
  529. * Adds a menu position rule.
  530. *
  531. * @param $rid
  532. * ID of the rule needing a menu link.
  533. * @param $mlid
  534. * The mlid of the menu link used in the rule.
  535. * @param $plid
  536. * The mlid of the parent menu link specified in the rule.
  537. * @param $admin_title
  538. * The administrative title of the rule.
  539. * @return
  540. * The mlid of the rule's new menu link.
  541. */
  542. function menu_position_edit_menu_link($rid, $mlid, $plid, $admin_title) {
  543. // Add a menu link to handle matching pages.
  544. $item = array(
  545. 'link_path' => 'menu-position/' . $rid,
  546. 'link_title' => $admin_title . ' (menu position rule)',
  547. 'mlid' => $mlid,
  548. 'plid' => $plid,
  549. 'hidden' => 1,
  550. 'module' => 'menu_position',
  551. 'options' => array(
  552. 'alter' => TRUE,
  553. 'attributes' => array('class' => array('menu-position-link')),
  554. ),
  555. );
  556. // If this is an existing menu link, get the existing weight.
  557. if ($item['mlid']) {
  558. $existing_item = db_query("SELECT plid, weight FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $item['mlid']))->fetchAssoc();
  559. $item['weight'] = ($existing_item['plid'] == $plid) ? $existing_item['weight'] : 0;
  560. // If the rule has a new parent, update the old parent.
  561. if ($existing_item['plid'] != $item['plid']) {
  562. $old_parent = menu_link_load($existing_item['plid']);
  563. $old_parent['options']['alter'] = FALSE;
  564. menu_link_save($old_parent);
  565. }
  566. }
  567. // Update the new parent.
  568. $parent = menu_link_load($item['plid']);
  569. $parent['options']['alter'] = TRUE;
  570. menu_link_save($parent);
  571. return menu_link_save($item);
  572. }
  573. /**
  574. * Menu callback: confirms deletion of rule.
  575. */
  576. function menu_position_delete_rule_form($form, &$form_state, $rid = 0) {
  577. // Make sure rid is set.
  578. if ($rid == 0) {
  579. drupal_goto('admin/structure/menu-position');
  580. return;
  581. }
  582. $form['rid'] = array('#type' => 'hidden', '#value' => $rid);
  583. $title = db_query('SELECT admin_title FROM {menu_position_rules} WHERE rid = :rid', array(':rid' => $rid))->fetchField();
  584. return confirm_form($form, t('Are you sure you want to delete the %title rule?', array('%title' => $title)), 'admin/structure/menu-position/edit/' . $rid, NULL, t('Delete'), t('Cancel'));
  585. }
  586. /**
  587. * Handles form submission for menu_position_delete_rule_form().
  588. */
  589. function menu_position_delete_rule_form_submit($form, &$form_state) {
  590. $title = db_query('SELECT admin_title FROM {menu_position_rules} WHERE rid = :rid', array(':rid' => $form_state['values']['rid']))->fetchField();
  591. menu_position_delete_rule($form_state['values']['rid']);
  592. drupal_set_message(t('The %title rule has been deleted.', array('%title' => $title)));
  593. $form_state['redirect'] = 'admin/structure/menu-position';
  594. }
  595. /**
  596. * Deletes a menu position rule.
  597. */
  598. function menu_position_delete_rule($rid) {
  599. db_delete('menu_position_rules')
  600. ->condition('rid', $rid)
  601. ->execute();
  602. menu_link_delete(NULL, 'menu-position/' . $rid);
  603. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.