Commit 115285f3 authored by Gábor Hojtsy's avatar Gábor Hojtsy

#165675 by pwolanin: only show allowed parent items in menu/book select fields...

#165675 by pwolanin: only show allowed parent items in menu/book select fields to avoid going deeper then what is allowed
parent f48af90a
......@@ -384,12 +384,16 @@ function _book_parent_select($book_link) {
$form['#prefix'] .= '<em>'. t('No book selected.') .'</em>';
}
else {
// If the item has children, there is an added limit to the depth of valid parents.
$limit = MENU_MAX_DEPTH - 1 - ($book_link['has_children'] ? menu_link_children_relative_depth($book_link) : 0);
$form = array(
'#type' => 'select',
'#title' => t('Parent item'),
'#default_value' => $book_link['plid'],
'#description' => t('The parent page in the book.'),
'#options' => book_toc($book_link['bid'], array($book_link['mlid'])),
'#description' => t('The parent page in the book. The maximum depth for a book and all child pages is !maxdepth. Some pages in the selected book may not be available as parents if selecting them would exceed this limit.', array('!maxdepth' => MENU_MAX_DEPTH)),
'#options' => book_toc($book_link['bid'], array($book_link['mlid']), $limit),
'#attributes' => array('class' => 'book-title-select'),
);
}
return $form;
......@@ -415,6 +419,7 @@ function _book_add_form_elements(&$form, $node) {
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => TRUE,
'#attributes' => array('class' => 'book-outline-form'),
);
foreach (array('menu_name', 'mlid', 'nid', 'router_path', 'has_children', 'options', 'module', 'original_bid') as $key) {
$form['book'][$key] = array(
......@@ -434,11 +439,18 @@ function _book_add_form_elements(&$form, $node) {
'#description' => t('Pages at a given level are ordered first by weight and then by title.'),
);
$options = array();
foreach (book_get_books() as $book) {
$options[$book['nid']] = $book['title'];
}
$nid = isset($node->nid) ? $node->nid : 'new';
if (isset($node->nid) && ($nid == $node->book['original_bid']) && $node->book['has_children'] && (MENU_MAX_DEPTH - 1 - menu_link_children_relative_depth($node->book) == 0)) {
// This is the top level node in a maximum depth book and thus cannot be moved.
$options[$node->nid] = $node->title;
}
else {
foreach (book_get_books() as $book) {
$options[$book['nid']] = $book['title'];
}
}
if (user_access('create new books') && ($nid == 'new' || ($nid != $node->book['original_bid']))) {
// The node can become a new book, if it is not one already.
$options = array($nid => '<'. t('create a new book') .'>') + $options;
......@@ -457,6 +469,7 @@ function _book_add_form_elements(&$form, $node) {
'#access' => (bool)$options,
'#description' => t('Your page will be a part of the selected book.'),
'#weight' => -5,
'#attributes' => array('class' => 'book-title-select'),
);
}
......@@ -491,13 +504,13 @@ function book_outline_form(&$form_state, $node) {
$form['book']['#collapsible'] = FALSE;
$form['book']['update'] = array(
$form['update'] = array(
'#type' => 'submit',
'#value' => $node->book['original_bid'] ? t('Update book outline') : t('Add to book outline'),
'#weight' => 15,
);
$form['book']['remove'] = array(
$form['remove'] = array(
'#type' => 'submit',
'#value' => t('Remove from book outline'),
'#access' => $node->nid != $node->book['bid'] && $node->book['bid'],
......@@ -951,12 +964,16 @@ function theme_book_navigation($book_link) {
/**
* A recursive helper function for book_toc().
*/
function _book_toc_recurse($tree, $indent, &$toc, $exclude) {
function _book_toc_recurse($tree, $indent, &$toc, $exclude, $depth_limit) {
foreach ($tree as $data) {
if ($data['link']['depth'] > $depth_limit) {
// Don't iterate through any links on this level.
break;
}
if (!in_array($data['link']['mlid'], $exclude)) {
$toc[$data['link']['mlid']] = $indent .' '. truncate_utf8($data['link']['title'], 30, TRUE, TRUE);
if ($data['below'] && $data['link']['depth'] < MENU_MAX_DEPTH - 1) {
_book_toc_recurse($data['below'], $indent .'--', $toc, $exclude);
if ($data['below']) {
_book_toc_recurse($data['below'], $indent .'--', $toc, $exclude, $depth_limit);
}
}
}
......@@ -970,14 +987,16 @@ function _book_toc_recurse($tree, $indent, &$toc, $exclude) {
* @param $exclude
* Optional array of mlid values. Any link whose mlid is in this array
* will be excluded (along with its children).
* @param $depth_limit
* Any link deeper than this value will be excluded (along with its children).
* @return
* An array of mlid, title pairs for use as options for selecting a book page.
*/
function book_toc($bid, $exclude = array()) {
function book_toc($bid, $exclude = array(), $depth_limit) {
$tree = menu_tree_all_data(book_menu_name($bid));
$toc = array();
_book_toc_recurse($tree, '', $toc, $exclude);
_book_toc_recurse($tree, '', $toc, $exclude, $depth_limit);
return $toc;
}
......
......@@ -270,19 +270,27 @@ function menu_flip_item($hide, $item) {
*/
function menu_edit_item(&$form_state, $type, $item, $menu) {
$form['menu'] = array(
'#type' => 'fieldset',
'#title' => t('Menu settings'),
'#collapsible' => FALSE,
'#tree' => TRUE,
'#weight' => -2,
'#attributes' => array('class' => 'menu-item-form'),
);
if ($type == 'add' || empty($item)) {
// This is an add form, initialize the menu link.
$item = array('link_title' => '', 'mlid' => 0, 'plid' => 0, 'menu_name' => $menu['menu_name'], 'weight' => 0, 'link_path' => '', 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0);
}
foreach (array('link_path', 'mlid', 'module', 'hidden', 'has_children', 'options') as $key) {
$form[$key] = array('#type' => 'value', '#value' => $item[$key]);
$form['menu'][$key] = array('#type' => 'value', '#value' => $item[$key]);
}
// Any item created or edited via this interface is considered "customized".
$form['customized'] = array('#type' => 'value', '#value' => 1);
$form['original_item'] = array('#type' => 'value', '#value' => $item);
$form['menu']['customized'] = array('#type' => 'value', '#value' => 1);
$form['menu']['original_item'] = array('#type' => 'value', '#value' => $item);
if ($item['module'] == 'menu') {
$form['link_path'] = array(
$form['menu']['link_path'] = array(
'#type' => 'textfield',
'#title' => t('Path'),
'#default_value' => $item['link_path'],
......@@ -298,26 +306,26 @@ function menu_edit_item(&$form_state, $type, $item, $menu) {
);
}
else {
$form['_path'] = array(
$form['menu']['_path'] = array(
'#type' => 'item',
'#title' => t('Path'),
'#description' => l($item['link_title'], $item['href'], $item['options']),
);
}
$form['link_title'] = array('#type' => 'textfield',
$form['menu']['link_title'] = array('#type' => 'textfield',
'#title' => t('Menu link title'),
'#default_value' => $item['link_title'],
'#description' => t('The link text corresponding to this item that should appear in the menu.'),
'#required' => TRUE,
);
$form['description'] = array(
$form['menu']['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => isset($item['options']['attributes']['title']) ? $item['options']['attributes']['title'] : '',
'#rows' => 1,
'#description' => t('The description displayed when hovering over a menu item.'),
);
$form['expanded'] = array(
$form['menu']['expanded'] = array(
'#type' => 'checkbox',
'#title' => t('Expanded'),
'#default_value' => $item['expanded'],
......@@ -330,13 +338,15 @@ function menu_edit_item(&$form_state, $type, $item, $menu) {
if (!isset($options[$default])) {
$default = 'navigation:0';
}
$form['parent'] = array(
$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['weight'] = array(
$form['menu']['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $item['weight'],
......@@ -352,7 +362,7 @@ function menu_edit_item(&$form_state, $type, $item, $menu) {
* Validate form values for a menu link being added or edited.
*/
function menu_edit_item_validate($form, &$form_state) {
$item = $form_state['values'];
$item = $form_state['values']['menu'];
if (!trim($item['link_path']) || !menu_valid_path($item)) {
form_set_error('link_path', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $item['link_path'])));
}
......@@ -362,19 +372,20 @@ function menu_edit_item_validate($form, &$form_state) {
* Submit function for the delete button on the menu item editing form.
*/
function menu_item_delete_submit($form, &$form_state) {
$form_state['redirect'] = 'admin/build/menu/item/'. $form_state['values']['mlid'] .'/delete';
$form_state['redirect'] = 'admin/build/menu/item/'. $form_state['values']['menu']['mlid'] .'/delete';
}
/**
* Process menu and menu item add/edit form submissions.
*/
function menu_edit_item_submit($form, &$form_state) {
$form_state['values']['options']['attributes']['title'] = $form_state['values']['description'];
list($form_state['values']['menu_name'], $form_state['values']['plid']) = explode(':', $form_state['values']['parent']);
if (!menu_link_save($form_state['values'])) {
$item = $form_state['values']['menu'];
$item['options']['attributes']['title'] = $item['description'];
list($item['menu_name'], $item['plid']) = explode(':', $item['parent']);
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/'. $form_state['values']['menu_name'];
$form_state['redirect'] = 'admin/build/menu-customize/'. $item['menu_name'];
}
/**
......@@ -391,10 +402,13 @@ function menu_edit_item_submit($form, &$form_state) {
*/
function menu_parent_options($menus, $item) {
// If the item has children, there is an added limit to the depth of valid parents.
$limit = MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0);
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']);
_menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit);
}
return $options;
}
......@@ -402,16 +416,20 @@ function menu_parent_options($menus, $item) {
/**
* Recursive helper function for menu_parent_options().
*/
function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude) {
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') .')';
}
$options[$menu_name .':'. $data['link']['mlid']] = $title;
if ($data['below'] && $data['link']['depth'] < MENU_MAX_DEPTH - 1) {
_menu_parents_recurse($data['below'], $menu_name, $indent .'--', $options, $exclude);
if ($data['below']) {
_menu_parents_recurse($data['below'], $menu_name, $indent .'--', $options, $exclude, $depth_limit);
}
}
}
......@@ -728,6 +746,7 @@ function menu_form_alter(&$form, $form_state, $form_id) {
'#collapsed' => FALSE,
'#tree' => TRUE,
'#weight' => -2,
'#attributes' => array('class' => 'menu-item-form'),
);
$item = $form['#node']->menu;
......@@ -763,6 +782,8 @@ function menu_form_alter(&$form, $form_state, $form_id) {
'#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['#submit'][] = 'menu_node_form_submit';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment