Commit cef7ca77 authored by Gerhard Killesreiter's avatar Gerhard Killesreiter
Browse files

#55132, reformatting of menu.module, patch by Zen.

parent 6ca5a9a2
......@@ -6,63 +6,17 @@
* Allows administrators to customize the site navigation menu.
*/
/**
* Implementation of hook_menu().
*/
function menu_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'admin/menu', 'title' => t('menus'),
'callback' => 'menu_overview',
'access' => user_access('administer menu'));
$items[] = array('path' => 'admin/menu/item/edit', 'title' => t('edit menu item'),
'callback' => 'menu_edit_item',
'access' => user_access('administer menu'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/menu/item/reset', 'title' => t('reset menu item'),
'callback' => 'menu_reset_item',
'access' => user_access('administer menu'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/menu/item/disable', 'title' => t('disable menu item'),
'callback' => 'menu_disable_item',
'access' => user_access('administer menu'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/menu/item/delete', 'title' => t('delete menu item'),
'callback' => 'menu_edit_item_delete',
'access' => user_access('administer menu'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/menu/list', 'title' => t('list'),
'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
$items[] = array('path' => 'admin/menu/menu/add', 'title' => t('add menu'),
'callback' => 'menu_add_menu',
'access' => user_access('administer menu'),
'type' => MENU_LOCAL_TASK);
$items[] = array('path' => 'admin/menu/item/add', 'title' => t('add menu item'),
'callback' => 'menu_edit_item',
'access' => user_access('administer menu'),
'type' => MENU_LOCAL_TASK);
$items[] = array('path' => 'admin/settings/menu',
'title' => t('menus'),
'callback' => 'menu_configure');
}
return $items;
}
/**
* Implementation of hook_help().
*/
function menu_help($section) {
switch ($section) {
case 'admin/help#menu':
$output = t('<p>The menu module allows for customization of the menus. Menus are useful for providing navigation in your site. The main menu for navigation is the navigation menu. Menus appear in blocks on your site.</p>
$output = t('<p>The menu module allows for customization of the menus. Menus are useful for providing navigation in your site. The main menu for navigation is the navigation menu. Menus appear in blocks on your site.</p>
<ul>
<li>On the administer menu page administrators can \"edit\" to change the title, description, parent or weight of the menu item. Under the \"operations\" column, click on \"enable/disable\" to toggle the menu item on or off. Menu items which are disabled are not deleted; they are merely not available for navigating the site in the sidebar menu block. Note that the default menu items generated by the menu module cannot be deleted, only disabled.</li>
<li>Using the \"add menu\" tab submit a title for a new custom menu. Once submitted, the new menu will appear in a list toward the bottom of the administer menu page underneath the main navigation menu.</li>
<li>Use the \"add menu item\" tab to create new links in either the navigation or a custom menu. Select the parent item to place the new link within an existing menu structure. For top level menu items, choose the name of the menu in which the link is to be added.</li>
<li>On the administer menu page administrators can "edit" to change the title, description, parent or weight of the menu item. Under the "operations" column, click on "enable/disable" to toggle the menu item on or off. Menu items which are disabled are not deleted; they are merely not available for navigating the site in the sidebar menu block. Note that the default menu items generated by the menu module cannot be deleted, only disabled.</li>
<li>Using the "add menu" tab submit a title for a new custom menu. Once submitted, the new menu will appear in a list toward the bottom of the administer menu page underneath the main navigation menu.</li>
<li>Use the "add menu item" tab to create new links in either the navigation or a custom menu. Select the parent item to place the new link within an existing menu structure. For top level menu items, choose the name of the menu in which the link is to be added.</li>
<li>To turn off the navigation menu block, administer the block page.</li>
</ul>
');
......@@ -89,67 +43,59 @@ function menu_help($section) {
}
}
/**
* Menu callback; presents menu configuration options.
* Implementation of hook_menu().
*/
function menu_configure() {
$menu = menu_get_menu();
$root_menus = menu_get_root_menus();
$primary_options = $root_menus;
$primary_options[0] = t('No primary links');
$form['settings_links'] = array(
'#type' => 'fieldset',
'#title' => t('Primary links settings'),
);
$form['settings_links']['intro'] = array(
'#type' => 'item',
'#value' => t('Primary links is a navigation system which usually (depending on your theme) appears at the top-right of the browser window. There are usually two rows of links, primary and secondary. You may control which links appear in this area by choosing a menu from which the links will be generated and then placing your links into the menu using the <a href="%menu">menu administration</a> or the menu settings pane on each post authoring form.', array('%menu' => url('admin/menu'))),
);
$form['settings_links']['menu_primary_menu'] = array(
'#type' => 'select',
'#title' => t('Menu containing primary links'),
'#default_value' => variable_get('menu_primary_menu', 0),
'#options' => $primary_options,
);
$secondary_options = $root_menus;
$secondary_options[0] = t('No secondary links');
$form['settings_links']['menu_secondary_menu'] = array(
'#type' => 'select',
'#title' => t('Menu containing secondary links'),
'#default_value' => variable_get('menu_secondary_menu', 0),
'#options' => $secondary_options,
'#description' => t('If you select the same menu as primary links then secondary links will display the appropriate second level of your navigation hierarchy.'),
);
$form['settings_authoring'] = array(
'#type' => 'fieldset',
'#title' => t('Post authoring form settings'),
);
function menu_menu($may_cache) {
$items = array();
$form['settings_authoring']['intro'] = array(
'#type' => 'item',
'#value' => t('On each post authoring form there is a menu settings pane. This setting allows you to limit what is displayed in the parent item drop-down menu of that pane. This can be used to force new menu items to be created in the primary links menu or to hide admin menu items.'),
);
if ($may_cache) {
$items[] = array('path' => 'admin/menu',
'title' => t('menus'),
'callback' => 'menu_overview',
'access' => user_access('administer menu'));
$items[] = array('path' => 'admin/menu/item/edit',
'title' => t('edit menu item'),
'callback' => 'menu_edit_item',
'access' => user_access('administer menu'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/menu/item/reset',
'title' => t('reset menu item'),
'callback' => 'menu_reset_item',
'access' => user_access('administer menu'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/menu/item/disable',
'title' => t('disable menu item'),
'callback' => 'menu_disable_item',
'access' => user_access('administer menu'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/menu/item/delete',
'title' => t('delete menu item'),
'callback' => 'menu_edit_item_delete',
'access' => user_access('administer menu'),
'type' => MENU_CALLBACK);
$authoring_options = $root_menus;
$authoring_options[0] = t('Show all menus');
$items[] = array('path' => 'admin/menu/list',
'title' => t('list'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10);
$items[] = array('path' => 'admin/menu/menu/add',
'title' => t('add menu'),
'callback' => 'menu_add_menu',
'access' => user_access('administer menu'),
'type' => MENU_LOCAL_TASK);
$items[] = array('path' => 'admin/menu/item/add',
'title' => t('add menu item'),
'callback' => 'menu_edit_item',
'access' => user_access('administer menu'),
'type' => MENU_LOCAL_TASK);
$form['settings_authoring']['menu_parent_items'] = array(
'#type' => 'select',
'#title' => t('Restrict parent items to'),
'#default_value' => variable_get('menu_parent_items', 0),
'#options' => $authoring_options,
'#description' => t('Choose the menu from which parent items will be made available. Only this menu item and its children will be shown.'),
);
$items[] = array('path' => 'admin/settings/menu',
'title' => t('menus'),
'callback' => 'menu_configure');
}
return system_settings_form('menu_configure', $form);
return $items;
}
/**
......@@ -211,12 +157,136 @@ function menu_perm() {
}
/**
* Menu callback; present the main menu management page.
* Implementation of hook_form_alter().
* Add menu item fields to the node form.
*/
function menu_overview() {
menu_rebuild();
function menu_form_alter($form_id, &$form) {
if (user_access('administer menu') && isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
$edit = isset($_POST['edit']) ? $_POST['edit'] : '';
$edit['nid'] = $form['nid']['#value'];
return menu_overview_tree();
$item = array();
if ($edit['nid'] > 0) {
$item = db_fetch_array(db_query("SELECT * FROM {menu} WHERE path = 'node/%d'", $edit['nid']));
if (is_array($edit['menu'])) {
$item = !is_array($item) ? $edit['menu'] : (($_POST['op'] == t('Preview')) ? array_merge($item, $edit['menu']) : array_merge($edit['menu'], $item));
}
}
$form['menu'] = array('#type' => 'fieldset',
'#title' => t('Menu settings'),
'#collapsible' => TRUE,
'#collapsed' => empty($item['title']),
'#tree' => TRUE,
'#weight' => 30,
);
$form['menu']['title'] = array('#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $item['title'],
'#description' => t('The name to display for this link.'),
);
$form['menu']['description'] = array('#type' => 'textfield',
'#title' => t('Description'),
'#default_value' => $item['description'],
'#description' => t('The description displayed when hovering over a menu item.'),
);
// Generate a list of possible parents.
$options = menu_parent_options($item['mid'], variable_get('menu_parent_items', 0));
$form['menu']['pid'] = array('#type' => 'select',
'#title' => t('Parent item'),
'#default_value' => $item['pid'],
'#options' => $options,
);
$form['menu']['path'] = array('#type' => 'hidden',
'#value' => $item['path'],
);
$form['menu']['weight'] = array('#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $item['weight'],
'#delta' => 10,
'#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
);
$form['menu']['mid'] = array('#type' => 'hidden',
'#value' => $item['mid'] ? $item['mid'] : 0,
);
$form['menu']['type'] = array('#type' => 'hidden',
'#value' => $item['type'] ? $item['type'] : MENU_CUSTOM_ITEM,
);
if ($item['mid'] > 0) {
$form['menu']['delete'] = array('#type' => 'checkbox',
'#title' => t('Check to delete this menu item.'),
'#default_value' => $item['delete'],
);
$form['menu']['advanced'] = array('#type' => 'item',
'#value' => t('You may also <a href="%edit">edit the advanced settings</a> for this menu item.', array('%edit' => url("admin/menu/item/edit/{$item['mid']}"))),
);
}
}
}
/**
* Menu callback; presents menu configuration options.
*/
function menu_configure() {
$menu = menu_get_menu();
$root_menus = menu_get_root_menus();
$primary_options = $root_menus;
$primary_options[0] = t('No primary links');
$form['settings_links'] = array('#type' => 'fieldset',
'#title' => t('Primary and secondary links settings'),
);
$form['settings_links']['intro'] = array('#type' => 'item',
'#value' => t('Primary and secondary links provide a navigational menu system which usually (depending on your theme) appears at the top-right of the browser window. The links displayed can be generated either from a custom list created via the <a href="%menu">menu administration</a> page or from a built-in list of menu items such as the navigation menu links.', array('%menu' => url('admin/menu'))),
);
$form['settings_links']['menu_primary_menu'] = array('#type' => 'select',
'#title' => t('Menu containing primary links'),
'#default_value' => variable_get('menu_primary_menu', 0),
'#options' => $primary_options,
);
$secondary_options = $root_menus;
$secondary_options[0] = t('No secondary links');
$form['settings_links']['menu_secondary_menu'] = array('#type' => 'select',
'#title' => t('Menu containing secondary links'),
'#default_value' => variable_get('menu_secondary_menu', 0),
'#options' => $secondary_options,
'#description' => t('If you select the same menu as primary links then secondary links will display the appropriate second level of your navigation hierarchy.'),
);
$form['settings_authoring'] = array('#type' => 'fieldset',
'#title' => t('Post authoring form settings'),
);
$form['settings_authoring']['intro'] = array('#type' => 'item',
'#value' => t('The menu module allows on-the-fly creation of menu links in the post authoring forms. The following option limits the menus in which a new link may be added. For e.g. this can be used to force new menu items to be created in the primary links menu or to hide admin menu items.'),
);
$authoring_options = $root_menus;
$authoring_options[0] = t('Show all menus');
$form['settings_authoring']['menu_parent_items'] = array('#type' => 'select',
'#title' => t('Restrict parent items to'),
'#default_value' => variable_get('menu_parent_items', 0),
'#options' => $authoring_options,
'#description' => t('Choose the menu to be made available in the post authoring form. Only this menu item and its children will be shown.'),
);
return system_settings_form('menu_configure', $form);
}
/**
......@@ -244,72 +314,6 @@ function menu_add_menu() {
return $output;
}
/**
* Menu callback; reset a single modified item.
*/
function menu_reset_item($mid) {
$op = isset($_POST['op']) ? $_POST['op'] : '';
switch ($op) {
case t('Reset'):
menu_delete_item($mid);
drupal_set_message(t("The menu item was reset to its default settings."));
drupal_goto('admin/menu');
break;
default:
$title = db_result(db_query('SELECT title FROM {menu} WHERE mid = %d', $mid));
return confirm_form('menu_item_confirm_reset', array(),
t('Are you sure you want to reset the item %item to its default values?', array('%item' => theme('placeholder', $title))),
'admin/menu', t('Any customizations will be lost. This action cannot be undone.'),
t('Reset'));
}
}
/**
* Menu callback; delete a single custom item.
*/
function menu_edit_item_delete($mid) {
$op = isset($_POST['op']) ? $_POST['op'] : '';
$result = db_query('SELECT type, title FROM {menu} WHERE mid = %d', $mid);
$menu = db_fetch_object($result);
if (!$menu) {
drupal_goto('admin/menu');
}
switch ($op) {
case t('Delete'):
menu_delete_item($mid);
if ($menu->type & MENU_IS_ROOT) {
drupal_set_message(t('The menu has been removed.'));
}
else {
drupal_set_message(t('The menu item has been removed.'));
}
drupal_goto('admin/menu');
break;
default:
if ($menu->type & MENU_IS_ROOT) {
$message = t('Are you sure you want to delete the menu %item?', array('%item' => theme('placeholder', $menu->title)));
}
else {
$message = t('Are you sure you want to delete the custom menu item %item?', array('%item' => theme('placeholder', $menu->title)));
}
return confirm_form('menu_confirm_delete', $form, $message, 'admin/menu', t('This action cannot be undone.'), t('Delete'));
}
}
/**
* Menu callback; hide a menu item.
*/
function menu_disable_item($mid) {
$item = menu_get_item($mid);
$type = $item['type'];
$type &= ~MENU_VISIBLE_IN_TREE;
$type &= ~MENU_VISIBLE_IN_BREADCRUMB;
$type |= MENU_MODIFIED_BY_ADMIN;
db_query('UPDATE {menu} SET type = %d WHERE mid = %d', $type, $mid);
drupal_set_message(t('The menu item has been disabled.'));
drupal_goto('admin/menu');
}
/**
* Menu callback; dispatch to the appropriate menu item edit function.
*/
......@@ -342,7 +346,7 @@ function menu_edit_item($mid = 0) {
}
else {
$edit['mid'] = 0; // In case a negative ID was passed in.
$edit['pid'] = 1; // default to "Navigation" menu.
$edit['pid'] = 1; // Default to "Navigation" menu.
$edit['type'] = MENU_CUSTOM_ITEM;
}
$output .= menu_edit_item_form($edit);
......@@ -357,32 +361,66 @@ function menu_edit_item($mid = 0) {
function menu_edit_item_form($edit) {
if ($edit['pid'] == 0) {
// Display a limited set of fields for menus (not items).
$form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $edit['title'], '#description' => t('The name of the menu.'), '#required' => TRUE);
$form['title'] = array('#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $edit['title'],
'#description' => t('The name of the menu.'),
'#required' => TRUE,
);
$form['path'] = array('#type' => 'hidden', '#value' => '');
$form['pid'] = array('#type' => 'hidden', '#value' => 0);
$form['weight'] = array('#type' => 'hidden', '#value' => 0);
}
else {
$form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $edit['title'], '#description' => t('The name of the menu item.'), '#required' => TRUE);
$form['description'] = array('#type' => 'textfield', '#title' => t('Description'), '#default_value' => $edit['description'], '#description' => t('The description displayed when hovering over a menu item.'));
$form['title'] = array('#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $edit['title'],
'#description' => t('The name of the menu item.'),
'#required' => TRUE,
);
$form['description'] = array('#type' => 'textfield',
'#title' => t('Description'),
'#default_value' => $edit['description'],
'#description' => t('The description displayed when hovering over a menu item.'),
);
$path_description = t('The Drupal path this menu item links to. Enter %front to link to the front page.', array('%front' => theme('placeholder', '<front>')));
if ($edit['type'] & MENU_CREATED_BY_ADMIN) {
$form['path'] = array('#type' => 'textfield', '#title' => t('Path'), '#default_value' => $edit['path'], '#description' => $path_description, '#required' => TRUE);
$form['path'] = array('#type' => 'textfield',
'#title' => t('Path'),
'#default_value' => $edit['path'],
'#description' => $path_description,
'#required' => TRUE,
);
}
else {
$form['_path'] = array('#type' => 'item', '#title' => t('Path'), '#description' => l($edit['path'], $edit['path']));
$form['_path'] = array('#type' => 'item',
'#title' => t('Path'),
'#description' => l($edit['path'], $edit['path']),
);
$form['path'] = array('#type' => 'hidden', '#value' => $edit['path']);
}
$expanded = $edit['type'] & MENU_EXPANDED ? 1 : 0;
$form['expanded'] = array('#type' => 'checkbox', '#title' => t('Expanded'), '#default_value' => $expanded, '#description' => t('If selected and this menu item has children, the menu will always appear expanded.'));
$form['expanded'] = array('#type' => 'checkbox',
'#title' => t('Expanded'),
'#default_value' => $expanded,
'#description' => t('If selected and this menu item has children, the menu will always appear expanded.'),
);
// Generate a list of possible parents (not including this item or descendants).
$options = menu_parent_options($edit['mid']);
$form['pid'] = array('#type' => 'select', '#title' => t('Parent item'), '#default_value' => $edit['pid'], '#options' => $options);
$form['pid'] = array('#type' => 'select',
'#title' => t('Parent item'),
'#default_value' => $edit['pid'],
'#options' => $options,
);
$form['weight'] = array('#type' => 'weight', '#title' => t('Weight'), '#default_value' => $edit['weight'], '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'));
$form['weight'] = array('#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $edit['weight'],
'#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
);
}
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
......@@ -440,6 +478,81 @@ function menu_edit_item_save($edit) {
return $edit['mid'];
}
/**
* Menu callback; reset a single modified item.
*/
function menu_reset_item($mid) {
$op = isset($_POST['op']) ? $_POST['op'] : '';
switch ($op) {
case t('Reset'):
menu_delete_item($mid);
drupal_set_message(t("The menu item was reset to its default settings."));
drupal_goto('admin/menu');
break;
default:
$title = db_result(db_query('SELECT title FROM {menu} WHERE mid = %d', $mid));
return confirm_form('menu_item_confirm_reset', array(),
t('Are you sure you want to reset the item %item to its default values?', array('%item' => theme('placeholder', $title))),
'admin/menu', t('Any customizations will be lost. This action cannot be undone.'),
t('Reset'));
}
}
/**
* Menu callback; delete a single custom item.
*/
function menu_edit_item_delete($mid) {
$op = isset($_POST['op']) ? $_POST['op'] : '';
$result = db_query('SELECT type, title FROM {menu} WHERE mid = %d', $mid);
$menu = db_fetch_object($result);
if (!$menu) {
drupal_goto('admin/menu');
}
switch ($op) {
case t('Delete'):
menu_delete_item($mid);
if ($menu->type & MENU_IS_ROOT) {
drupal_set_message(t('The menu has been removed.'));
}
else {
drupal_set_message(t('The menu item has been removed.'));
}
drupal_goto('admin/menu');
break;
default:
if ($menu->type & MENU_IS_ROOT) {
$message = t('Are you sure you want to delete the menu %item?', array('%item' => theme('placeholder', $menu->title)));
}
else {
$message = t('Are you sure you want to delete the custom menu item %item?', array('%item' => theme('placeholder', $menu->title)));
}
return confirm_form('menu_confirm_delete', $form, $message, 'admin/menu', t('This action cannot be undone.'), t('Delete'));
}
}
/**
* Menu callback; hide a menu item.
*/
function menu_disable_item($mid) {
$item = menu_get_item($mid);
$type = $item['type'];
$type &= ~MENU_VISIBLE_IN_TREE;
$type &= ~MENU_VISIBLE_IN_BREADCRUMB;
$type |= MENU_MODIFIED_BY_ADMIN;
db_query('UPDATE {menu} SET type = %d WHERE mid = %d', $type, $mid);
drupal_set_message(t('The menu item has been disabled.'));
drupal_goto('admin/menu');
}
/**
* Menu callback; present the main menu management page.
*/
function menu_overview() {
menu_rebuild();
return menu_overview_tree();
}
/**
* Save a menu item to the database.
*
......@@ -468,7 +581,7 @@ function menu_save_item(&$item) {
else {
$item['mid'] = db_next_id('{menu}_mid');
// Check explicitly for mid <= 2. If the database was improperly prefixed,
// this would cause a nasty infinite loop or duplicate mid errors.
// this would cause a nasty infinite loop or duplicate mid errors.
// TODO: have automatic prefixing through an installer to prevent this.
while ($item['mid'] <= 2) {
$item['mid'] = db_next_id('{menu}_mid');
......@@ -664,97 +777,9 @@ function menu_parent_options($mid, $pid = 0, $depth = 0) {
return $options;
}
/**
* Add menu item fields to the node form.
*/
function menu_form_alter($form_id, &$form) {
if (user_access('administer menu') && isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
$edit = isset($_POST['edit']) ? $_POST['edit'] : '';
$edit['nid'] = $form['nid']['#value'];
$item = array();
if ($edit['nid'] > 0) {
$item = db_fetch_array(db_query("SELECT * FROM {menu} WHERE path = 'node/%d'", $edit['nid']));
if (is_array($edit['menu'])) {
$item = !is_array($item) ? $edit['menu'] : (($_POST['op'] == t('Preview')) ? array_merge($item, $edit['menu']) : array_merge($edit['menu'], $item));
}
}
$form['menu'] = array(
'#type' => 'fieldset',
'#title' => t('Menu settings'),
'#collapsible' => TRUE,
'#collapsed' => empty($item['title']),
'#tree' => TRUE,
'#weight' => 30,
);
$form['menu']['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $item['title'],
'#description' => t('The name to display for this link.'),
);
$form['menu']['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#default_value' => $item['description'],
'#description' => t('The description displayed when hovering over a menu item.'),
);
// Generate a list of possible parents.
$options = menu_parent_options($item['mid'], variable_get('menu_parent_items', 0));