function 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

menu_overview_form()

File

modules/menu/menu.admin.inc, line 161

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_state['input']));
    // Get the $_POST order.
    $form = array_merge($order, $form);
    // Update our original form with the new order.
    $updated_items = array();
    $fields = array(
        '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']) {
                // Convert to integer rather than boolean due to PDO cast to string.
                $element['#item']['hidden'] = $element['hidden']['#value'] ? 0 : 1;
                $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);
    }
    drupal_set_message(t('Your configuration has been saved.'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.