function menu_overview_form_submit in Drupal 6
Same name and namespace in other branches
- 7 modules/menu/menu.admin.inc \menu_overview_form_submit()
Submit handler for the menu overview form.
This function takes great care in saving parent items first, then items underneath them. Saving items in the incorrect order can break the menu tree.
See also
File
- modules/
menu/ menu.admin.inc, line 128 - Administrative page callbacks for menu module.
Code
function menu_overview_form_submit($form, &$form_state) {
// When dealing with saving menu items, the order in which these items are
// saved is critical. If a changed child item is saved before its parent,
// the child item could be saved with an invalid path past its immediate
// parent. To prevent this, save items in the form in the same order they
// are sent by $_POST, ensuring parents are saved first, then their children.
// See http://drupal.org/node/181126#comment-632270
$order = array_flip(array_keys($form['#post']));
// Get the $_POST order.
$form = array_merge($order, $form);
// Update our original form with the new order.
$updated_items = array();
$fields = array(
'expanded',
'weight',
'plid',
);
foreach (element_children($form) as $mlid) {
if (isset($form[$mlid]['#item'])) {
$element = $form[$mlid];
// Update any fields that have changed in this menu item.
foreach ($fields as $field) {
if ($element[$field]['#value'] != $element[$field]['#default_value']) {
$element['#item'][$field] = $element[$field]['#value'];
$updated_items[$mlid] = $element['#item'];
}
}
// Hidden is a special case, the value needs to be reversed.
if ($element['hidden']['#value'] != $element['hidden']['#default_value']) {
$element['#item']['hidden'] = !$element['hidden']['#value'];
$updated_items[$mlid] = $element['#item'];
}
}
}
// Save all our changed items to the database.
foreach ($updated_items as $item) {
$item['customized'] = 1;
menu_link_save($item);
}
}