Index: modules/menu/menu.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/menu/menu.admin.inc,v retrieving revision 1.7 diff -u -r1.7 menu.admin.inc --- modules/menu/menu.admin.inc 17 Nov 2007 14:25:23 -0000 1.7 +++ modules/menu/menu.admin.inc 18 Nov 2007 08:03:18 -0000 @@ -162,7 +224,6 @@ * Menu callback; Build the menu link editing form. */ function menu_edit_item(&$form_state, $type, $item, $menu) { - $form['menu'] = array( '#type' => 'fieldset', '#title' => t('Menu settings'), @@ -233,18 +294,13 @@ ); // Generate a list of possible parents (not including this item or descendants). - $options = menu_parent_options(menu_get_menus(), $item); $default = $item['menu_name'] .':'. $item['plid']; if (!isset($options[$default])) { $default = 'navigation:0'; } - $form['menu']['parent'] = array( - '#type' => 'select', - '#title' => t('Parent item'), - '#default_value' => $default, - '#options' => $options, - '#description' => t('The maximum depth for an item and all its children is fixed at !maxdepth. Some menu items may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)), - '#attributes' => array('class' => 'menu-title-select'), + $form['menu']['parents'] = array( + '#type' => 'menu_parent_select', + '#mlid' => $item['mlid'], ); $form['menu']['weight'] = array( '#type' => 'weight', @@ -293,11 +349,21 @@ function menu_edit_item_submit($form, &$form_state) { $item = $form_state['values']['menu']; $item['options']['attributes']['title'] = $item['description']; - list($item['menu_name'], $item['plid']) = explode(':', $item['parent']); + $max_depth = -1; + $form = $form['menu']['parents']['parents']; + foreach (element_children($form) as $plid) { + $element = $form[$plid]; + if (isset($element['select']) && $element['select']['#value'] > 0 && $element['#depth'] > $max_depth) { + $item['plid'] = $element['select']['#value']; + $max_depth = $element['#depth']; + } + } if (!menu_link_save($item)) { drupal_set_message(t('There was an error saving the menu link.'), 'error'); } $form_state['redirect'] = 'admin/build/menu-customize/'. $item['menu_name']; + // As long as the storage is not empty, Form API rebuilds the form. + unset($form_state['storage']['mlid']); } /** Index: modules/menu/menu.module =================================================================== RCS file: /cvs/drupal/drupal/modules/menu/menu.module,v retrieving revision 1.148 diff -u -r1.148 menu.module --- modules/menu/menu.module 17 Nov 2007 14:25:23 -0000 1.148 +++ modules/menu/menu.module 18 Nov 2007 08:03:18 -0000 @@ -136,6 +136,12 @@ 'type' => MENU_CALLBACK, 'file' => 'menu.admin.inc', ); + $items['menu/js'] = array( + 'title' => 'Menu JavaScript callback', + 'page callback' => 'menu_form_js', + 'access arguments' => array('administer menu'), + 'type' => MENU_CALLBACK, + ); return $items; } @@ -149,6 +155,9 @@ 'file' => 'menu.admin.inc', 'arguments' => array('form' => NULL), ), + 'menu_parent_select' => array( + 'arguments' => array('form' => NULL), + ), ); } @@ -189,56 +198,119 @@ return db_fetch_array(db_query("SELECT * FROM {menu_custom} WHERE menu_name = '%s'", $menu_name)); } +function menu_elements() { + $types['menu_parent_select'] = array( + '#process' => array('menu_parent_select_process'), + '#input' => TRUE, + // Does not really matter but we set a #value just in case someone themes + // this element. + '#value' => '', + '#tree' => TRUE, + ); + return $types; +} + /** - * Return a list of menu items that are valid possible parents for the given menu item. - * - * @param $menus - * An array of menu names and titles, such as from menu_get_menus(). - * @param $item - * The menu item for which to generate a list of parents. - * If $item['mlid'] == 0 then the complete tree is returned. - * @return - * An array of menu link titles keyed on the a string containing the menu name - * and mlid. The list excludes the given item and its children. + * Creates a parent selector fieldset for a given menu link. */ -function menu_parent_options($menus, $item) { - - // If the item has children, there is an added limit to the depth of valid parents. - if (isset($item['parent_depth_limit'])) { - $limit = $item['parent_depth_limit']; +function menu_parent_select_process($form, $edit, &$form_state) { + $original_mlid = $form['#mlid']; + if (isset($form_state['storage']['mlid'])) { + $mlid = $form_state['storage']['mlid']; + $collapsed = FALSE; } else { - $limit = _menu_parent_depth_limit($item); - } - - foreach ($menus as $menu_name => $title) { - $tree = menu_tree_all_data($menu_name, NULL); - $options[$menu_name .':0'] = '<'. $title .'>'; - _menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit); + $mlid = $original_mlid; + $collapsed = TRUE; } - return $options; + $item = menu_link_load($mlid); + $tree = menu_tree_all_data($item['menu_name'], $item); + $form = array_merge($form, array( + '#type' => 'fieldset', + '#title' => t('Parents'), + '#collapsible' => TRUE, + '#collapsed' => $collapsed, + '#theme' => 'menu_parent_select', + )); + $form['parents'] = _menu_parent_select_recurse($tree, $original_mlid, TRUE); + $form['parents']['#prefix'] = ''; + $form['parents']['submit'] = array( + '#type' => 'submit', + '#value' => t('Update'), + '#menu_parent_select' => &$form['parents'], + '#submit' => array('_menu_parent_selector_update'), + '#ahah' => array( + 'path' => 'menu/js', + 'wrapper' => $form['#id'] .'-wrapper', + 'selector' => '#'. $form['#id'] .'-wrapper select', + 'event' => 'change', + 'progress' => 'none', + ), + ); + return $form; } -/** - * Recursive helper function for menu_parent_options(). - */ -function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) { - foreach ($tree as $data) { - if ($data['link']['depth'] > $depth_limit) { - // Don't iterate through any links on this level. - break; - } - if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) { - $title = $indent .' '. truncate_utf8($data['link']['title'], 30, TRUE, FALSE); - if ($data['link']['hidden']) { - $title .= ' ('. t('disabled') .')'; +function _menu_parent_select_recurse($tree, $original_mlid, $reset = FALSE) { + static $form; + if ($reset) { + $form = array(); + } + foreach ($tree as $item) { + if (isset($item['link'])) { + $link = $item['link']; + // Skip the current element and all its children. + if ($link['mlid'] == $original_mlid) { + continue; } - $options[$menu_name .':'. $data['link']['mlid']] = $title; - if ($data['below']) { - _menu_parents_recurse($data['below'], $menu_name, $indent .'--', $options, $exclude, $depth_limit); + if (!$link['hidden']) { + $plid = $link['plid']; + $mlid = $link['mlid']; + if (!isset($form[$plid])) { + $form[$plid]['select'] = array( + '#type' => 'select', + '#default_value' => -1, + '#size' => 6, + '#multiple' => FALSE, + ); + $depth = $link['depth']; + if ($depth > 1) { + $form[$plid]['select']['#options'][-1] = t('-----'); + } + $form[$plid]['#depth'] = $depth; + } + $form[$plid]['select']['#options'][$mlid] = $link['title']; + if ($link['in_active_trail']) { + $form[$plid]['select']['#default_value'] = $mlid; + } } } + if (!empty($item['below'])) { + _menu_parent_select_recurse($item['below'], $original_mlid); + } } + return $form; +} + +function _menu_parent_selector_update($form, &$form_state) { + // We took care to make our selects easily findable. + $form = $form_state['clicked_button']['#menu_parent_select']; + $min_depth = MENU_MAX_DEPTH + 1; + // We need the first element that was changed. + foreach (element_children($form) as $plid) { + $element = $form[$plid]; + if (isset($element['select']) && $element['select']['#default_value'] != $element['select']['#value'] && $element['select']['#value'] > 0 && $element['#depth'] < $min_depth) { + $form_state['storage']['mlid'] = $element['select']['#value']; + $min_depth = $element['#depth']; + } + } +} + +function theme_menu_parent_select($form) { + drupal_add_css(drupal_get_path('module', 'menu') .'/menu.css'); + // No need to show the button if we have javascript. + drupal_add_js('if (Drupal.jsEnabled) { $(document).ready(function() { $("#'. $form['parents']['submit']['#id'] .'").hide(); }); }', 'inline'); + return drupal_render($form); } /** @@ -474,3 +546,29 @@ $menu_admin = FALSE; return $item && $item['access']; } + +function menu_form_js() { + // Testing JSON. + drupal_json(array('status' => TRUE, 'data' => 'test')); + exit(); + + $form_state = array('submitted' => FALSE); + $cache = cache_get('form_'. $_POST['form_build_id'], 'cache_form'); + $form = $cache->data + array( + '#post' => $_POST, + '#programmed' => FALSE, + ); + $form_id = $form['#parameters'][0]; + // This will update form_state['storage'] + $form = form_builder($form_id, $form, $form_state); + // And this will act on it. + $form = form_builder($form_id, $form, $form_state); + cache_set('form_'. $_POST['form_build_id'], $form, 'cache_form', $cache->expire); + + $parent_form = $form_state['clicked_button']['#menu_parent_select']; + unset($parent_form['#prefix'], $parent_form['#suffix']); // Prevent duplicate wrappers. + $parent_form[$delta]['#attributes']['class'] = empty($parent_form[$delta]['#attributes']['class']) ? 'ahah-new-content' : $parent_form[$delta]['#attributes']['class'] .' ahah-new-content'; + $parent_form[$delta]['chvotes']['#value'] = 0; + $output = theme('status_messages') . drupal_render($parent_form); + drupal_json(array('status' => TRUE, 'data' => $output)); +} Index: includes/form.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/form.inc,v retrieving revision 1.245 diff -u -r1.245 form.inc --- includes/form.inc 13 Nov 2007 14:04:07 -0000 1.245 +++ includes/form.inc 18 Nov 2007 08:03:18 -0000 @@ -913,8 +913,7 @@ if (isset($form['#process']) && !$form['#processed']) { foreach ($form['#process'] as $process) { if (function_exists($process)) { - $args = array_merge(array($form), array(isset($edit) ? $edit : NULL), array($form_state), array($complete_form)); - $form = call_user_func_array($process, $args); + $form = $process($form, isset($edit) ? $edit : NULL, $form_state, $complete_form); } } $form['#processed'] = TRUE; Index: modules/menu/menu.css =================================================================== RCS file: modules/menu/menu.css diff -N modules/menu/menu.css --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/menu/menu.css 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,4 @@ +.menu-parent-select select, +.menu-parent-select input { + float: left; /* RTL */ +} Index: modules/menu/menu-rtl.css =================================================================== RCS file: modules/menu/menu-rtl.css diff -N modules/menu/menu-rtl.css --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/menu/menu-rtl.css 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,4 @@ +.menu-parent-select select, +.menu-parent-select input { + float: right; +}