Submit function for adding or editing a custom menu.
function menu_edit_menu_submit($form, &$form_state) {
$menu = $form_state['values'];
$path = 'admin/structure/menu/manage/';
if ($form['#insert']) {
// Add 'menu-' to the menu name to help avoid name-space conflicts.
$menu['id'] = 'menu-' . $menu['id'];
$system_link = entity_load_multiple_by_properties('menu_link', array(
'link_path' => 'admin/structure/menu',
'module' => 'system',
));
$system_link = reset($system_link);
$menu_link = entity_create('menu_link', array(
'link_title' => $menu['label'],
'link_path' => $path . $menu['id'],
'router_path' => $path . '%',
'plid' => $system_link
->id(),
));
$menu_link
->save();
menu_save($menu);
}
else {
menu_save($menu);
$menu_links = entity_load_multiple_by_properties('menu_link', array(
'link_path' => $path . $menu['id'],
));
foreach ($menu_links as $menu_link) {
$menu_link->link_title = $menu['label'];
$menu_link
->save();
}
}
drupal_set_message(t('Your configuration has been saved.'));
$form_state['redirect'] = $path . $menu['id'];
}