2004-04-15 20:49:42 +00:00
<?php
// $Id$
2004-08-21 06:42:38 +00:00
/**
* @file
* Allows administrators to customize the site navigation menu.
*/
2004-04-15 20:49:42 +00:00
/**
* Implementation of hook_help().
*/
function menu_help($section) {
switch ($section) {
2005-11-01 10:17:34 +00:00
case 'admin/help#menu':
2006-08-18 12:17:00 +00:00
$output = t('<p>Menus are a collection of links (menu items) used to navigate a website. The menu module provides an interface to control and customize the powerful menu system that comes with Drupal. Menus are primarily displayed as a hierarchical list of links using Drupal\'s highly flexible <a href="@admin-block">blocks</a> feature. Each menu automatically creates a block of the same name. By default, new menu items are placed inside a built-in menu labelled %navigation, but administrators can also create custom menus.</p>
<p>Drupal themes generally provide out-of-the-box support for two menus commonly labelled %primary-links and %secondary-links. These are sets of links which are usually displayed in the header or footer of each page (depending on the currently active theme). Any menu can be designated as the primary or secondary links menu via the <a href="@menu-settings">menu settings page</a>.</p>
2006-04-07 13:10:17 +00:00
Menu administration tabs:
2005-11-01 10:17:34 +00:00
<ul>
2006-04-07 13:10:17 +00:00
<li>On the administer menu page, administrators can "edit" to change the title, description, parent or weight of a menu item. Under the "operations" column, click on "enable/disable" to toggle a menu item on or off. Only menu items which are enabled are displayed in the corresponding menu block. Note that the default menu items generated by the menu module cannot be deleted, only disabled.</li>
<li>Use the "add menu" tab to submit a title for a new custom menu. Once submitted, the menu will appear in a list toward the bottom of the administer menu page underneath the main navigation menu. Under the menu name there will be links to edit or delete the menu, and a link to add new items to the menu.</li>
<li>Use the "add menu item" tab to create new links in either the navigation or a custom menu (such as a primary/secondary links 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>
2006-08-18 12:17:00 +00:00
</ul>', array('%navigation' => 'Navigation', '%primary-links' => 'primary links', '%secondary-links' => 'secondary links', '@admin-block' => url('admin/build/block'), '@menu-settings' => url('admin/build/menu/settings')));
$output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@menu">Menu page</a>.', array('@menu' => 'http://drupal.org/handbook/modules/menu/')) .'</p>';
2005-11-01 10:17:34 +00:00
return $output;
2006-07-31 11:25:55 +00:00
case 'admin/build/menu':
2007-01-31 16:38:34 +00:00
return '<p>'. t('Menus are a collection of links (menu items) used to navigate a website. The list(s) below display the currently available menus along with their menu items. Select an operation from the list to manage each menu or menu item.', array('@admin-settings-menus' => url('admin/build/menu/settings'), '@admin-block' => url('admin/build/block'))) .'</p>';
2006-07-31 11:25:55 +00:00
case 'admin/build/menu/menu/add':
2006-08-18 12:17:00 +00:00
return '<p>'. t('Enter the name for your new menu. Remember to enable the newly created block in the <a href="@blocks">blocks administration page</a>.', array('@blocks' => url('admin/build/block'))) .'</p>';
2006-07-31 11:25:55 +00:00
case 'admin/build/menu/item/add':
2006-04-07 13:10:17 +00:00
return '<p>'. t('Enter the title, path, position and the weight for your new menu item.') .'</p>';
2005-11-03 19:33:37 +00:00
}
}
2007-04-06 04:39:51 +00:00
/**
* Implementation of hook_perm().
*/
function menu_perm() {
return array('administer menu');
}
2005-11-03 19:33:37 +00:00
/**
2006-03-23 09:34:14 +00:00
* Implementation of hook_menu().
2005-11-03 19:33:37 +00:00
*/
2007-01-24 14:48:36 +00:00
function menu_menu() {
2007-04-06 04:39:51 +00:00
$items['admin/build/menu'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Menus',
'description' => "Control your site's navigation menu, primary links and secondary links. as well as rename and reorganize menu items.",
2007-06-05 09:15:02 +00:00
'page callback' => 'system_admin_menu_block_page',
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
2007-04-06 04:39:51 +00:00
'access callback' => 'user_access',
'access arguments' => array('administer menu'),
);
2007-06-05 09:15:02 +00:00
$result = db_query('SELECT * FROM {menu_custom} ORDER BY title');
while ($menu = db_fetch_object($result)) {
$items['admin/build/menu/'. $menu->menu_name] = array(
'title' => $menu->title,
'page callback' => 'menu_overview',
'page arguments' => array($menu->menu_name),
'description' => $menu->description,
);
$items['admin/build/menu/'. $menu->menu_name .'/list'] = array(
'title' => 'List items',
'weight' => -10,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/build/menu/'. $menu->menu_name .'/add'] = array(
'title' => 'Add item',
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_edit_item', 'add', $menu->menu_name),
'type' => MENU_LOCAL_TASK);
$items['admin/build/menu/'. $menu->menu_name .'/edit'] = array(
'title' => 'Edit menu',
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_edit_menu', 'edit', $menu->menu_name),
'type' => MENU_LOCAL_TASK);
}
2007-04-06 04:39:51 +00:00
$items['admin/build/menu/list'] = array(
2007-06-05 09:15:02 +00:00
'title' => 'List menus',
2007-04-06 04:39:51 +00:00
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
2007-06-05 09:15:02 +00:00
$items['admin/build/menu/menu/add'] = array(
'title' => 'Add menu',
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_edit_menu', 'add'),
'type' => MENU_LOCAL_TASK);
2007-04-06 04:39:51 +00:00
$items['admin/build/menu/item/disable'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Disable menu item',
2007-04-06 04:39:51 +00:00
'page callback' => 'menu_flip_item',
2007-06-05 09:15:02 +00:00
'page arguments' => array(TRUE),
2007-04-06 04:39:51 +00:00
'type' => MENU_CALLBACK,
);
$items['admin/build/menu/item/enable'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Enable menu item',
2007-04-06 04:39:51 +00:00
'page callback' => 'menu_flip_item',
2007-06-05 09:15:02 +00:00
'page arguments' => array(FALSE),
2007-04-06 04:39:51 +00:00
'type' => MENU_CALLBACK,
);
$items['admin/build/menu/item/edit'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Edit menu item',
2007-04-06 04:39:51 +00:00
'page callback' => 'drupal_get_form',
2007-06-05 09:15:02 +00:00
'page arguments' => array('menu_edit_item', 'edit', 5),
2007-04-06 04:39:51 +00:00
'type' => MENU_CALLBACK);
$items['admin/build/menu/item/reset'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Reset menu item',
2007-04-06 04:39:51 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_reset_item'),
'type' => MENU_CALLBACK);
$items['admin/build/menu/item/delete'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Delete menu item',
2007-04-06 04:39:51 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_item_delete_form'),
'type' => MENU_CALLBACK);
2007-06-05 09:15:02 +00:00
$items['admin/build/menu/menu/edit'] = array(
'title' => 'Edit menu',
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_edit_menu', 'edit'),
'type' => MENU_CALLBACK);
$items['admin/build/menu/menu/delete'] = array(
'title' => 'Delete menu',
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_item_delete_form'),
'type' => MENU_CALLBACK);
$items['admin/build/menu/settings'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_configure'),
'type' => MENU_LOCAL_TASK,
'weight' => 5,
);
2007-04-15 14:38:16 +00:00
2007-04-06 04:39:51 +00:00
return $items;
2007-06-05 09:15:02 +00:00
2004-04-15 20:49:42 +00:00
}
2006-08-31 21:58:36 +00:00
/**
2007-04-06 04:39:51 +00:00
* Menu callback which displays every menu element accessible to the current
* user and the relevant operations.
2006-08-31 21:58:36 +00:00
*/
2007-06-05 09:15:02 +00:00
function menu_overview($menu_name) {
2007-04-06 04:39:51 +00:00
$header = array(t('Menu item'), t('Expanded'), array('data' => t('Operations'), 'colspan' => '3'));
2007-06-05 09:15:02 +00:00
$sql ="
SELECT *, ml.weight + 50000 AS weight FROM {menu_links} ml
LEFT JOIN {menu_router} m ON m.path = ml.router_path
WHERE menu_name = '%s' AND hidden >= 0
ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC";
$sql_count = "SELECT COUNT(*) FROM {menu_links} ml WHERE menu_name = '%s' AND hidden >= 0";
$result = pager_query($sql, 200, 0, $sql_count, $menu_name);
list(, $tree) = _menu_tree_data($result);
$rows = _menu_overview_tree($tree);
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 200, 0);
return $output;
}
2007-05-16 13:45:17 +00:00
2007-06-05 09:15:02 +00:00
function _menu_overview_tree($tree) {
static $rows = array();
foreach ($tree as $data) {
$title = '';
if ($item = $data['link']) {
_menu_link_translate($item);
if (!$item['access']) {
continue;
}
$title = str_repeat(' ', $item['depth'] - 1) . ($item['depth'] > 1 ? '- ' : '');
$title .= l($item['link_title'], $item['href'], $item['options']);
// Populate the operations field.
$operations = array();
// Set the edit column.
$operations[] = array('data' => l(t('edit'), 'admin/build/menu/item/edit/'. $item['mlid']));
if ($item['hidden']) {
$title .= ' ('. t('disabled') .')';
$class = 'menu-disabled';
$operations[] = array('data' => l(t('enable'), 'admin/build/menu/item/enable/'. $item['mlid']));
}
else {
$class = 'menu-enabled';
$operations[] = array('data' => l(t('disable'), 'admin/build/menu/item/disable/'. $item['mlid']));
}
// Only items created by the menu module can be deleted.
if ($item['module'] == 'menu') {
$operations[] = array('data' => l(t('delete'), 'admin/build/menu/item/delete/'. $item['mlid']));
}
// Set the reset column.
else if ($item['module'] == 'system') {
$operations[] = array('data' => l(t('reset'), 'admin/build/menu/item/reset/'. $item['mlid']));
}
else {
$operations[] = array('data' => '');
}
$row = array(
array('data' => $title, 'class' => $class),
array('data' => ($item['has_children'] ? (($item['expanded']) ? t('Yes') : t('No')) : ''), 'class' => $class),
);
foreach ($operations as $operation) {
$operation['class'] = $class;
$row[] = $operation;
}
$rows[] = $row;
2007-04-06 04:39:51 +00:00
}
2007-06-05 09:15:02 +00:00
if ($data['below']) {
_menu_overview_tree($data['below']);
2006-03-23 09:34:14 +00:00
}
}
2007-06-05 09:15:02 +00:00
return $rows;
2006-03-23 09:34:14 +00:00
}
2007-04-06 13:27:23 +00:00
2004-04-15 20:49:42 +00:00
/**
2007-04-06 04:39:51 +00:00
* Menu callback; enable/disable a menu item.
*
2007-06-05 09:15:02 +00:00
* @param $hide
* TRUE to not show in the menu tree. FALSE to make the item and its children
2007-04-06 04:39:51 +00:00
* reappear in menu tree.
2007-06-05 09:15:02 +00:00
* @param $mlid
* mlid of the menu item.
2004-04-15 20:49:42 +00:00
*/
2007-06-05 09:15:02 +00:00
function menu_flip_item($hide, $mlid) {
if (!($item = menu_link_load($mlid))) {
drupal_not_found();
return;
2004-04-15 20:49:42 +00:00
}
2007-06-05 09:15:02 +00:00
$item['hidden'] = (bool)$hide;
menu_link_save($item);
drupal_set_message($hide ? t('The menu item has been disabled.') : t('The menu item has been enabled.'));
drupal_goto('admin/build/menu/'. $item['menu_name']);
2004-04-15 20:49:42 +00:00
}
/**
2007-06-05 09:15:02 +00:00
* Menu callback; present the menu item editing form.
2004-04-15 20:49:42 +00:00
*/
2007-06-05 09:15:02 +00:00
function menu_edit_item(&$form_state, $type, $id = 0) {
2006-07-31 11:25:55 +00:00
if ($type == 'edit') {
2007-06-05 09:15:02 +00:00
if (!($item = menu_link_load($id))) {
2006-03-23 21:48:36 +00:00
drupal_not_found();
return;
}
2004-04-15 20:49:42 +00:00
}
else {
2006-03-23 21:48:36 +00:00
// This is an add form.
2007-06-05 09:15:02 +00:00
// The mlid argument (if set) will be the default pid to use.
$item = array('mlid' => 0, 'plid' => 0, 'menu_name' => $id, 'weight' => 0, 'link_title' => '', 'link_path' => '', 'options' => array(), 'module' => 'menu', 'expanded' => 0, 'hidden' => 0, 'has_children' => 0);
}
foreach (array('link_path', 'mlid', 'module', 'hidden', 'menu_name', 'has_children') as $key) {
$form[$key] = array('#type' => 'value', '#value' => $item[$key]);
2006-03-23 21:48:36 +00:00
}
2004-08-15 16:42:59 +00:00
2007-06-05 09:15:02 +00:00
$form['link_title'] = array('#type' => 'textfield',
2006-03-23 21:48:36 +00:00
'#title' => t('Title'),
2007-06-05 09:15:02 +00:00
'#default_value' => $item['link_title'],
2006-03-23 21:48:36 +00:00
'#description' => t('The name of the menu item.'),
'#required' => TRUE,
);
2007-04-06 04:39:51 +00:00
$form['description'] = array(
2007-06-05 09:15:02 +00:00
'#type' => 'textarea',
2006-03-23 21:48:36 +00:00
'#title' => t('Description'),
2007-06-05 09:15:02 +00:00
'#default_value' => isset($item['options']['attributes']['title']) ? $item['options']['attributes']['title'] : '',
2006-03-23 21:48:36 +00:00
);
2007-06-05 09:15:02 +00:00
if ($item['module'] == 'menu') {
$form['link_path'] = array(
2007-04-06 04:39:51 +00:00
'#type' => 'textfield',
2006-03-23 21:48:36 +00:00
'#title' => t('Path'),
2007-06-05 09:15:02 +00:00
'#default_value' => $item['link_path'],
2006-08-18 12:17:00 +00:00
'#description' => t('The path this menu item links to. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '<front>', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')),
2006-03-23 21:48:36 +00:00
'#required' => TRUE,
2006-03-23 09:34:14 +00:00
);
2006-03-23 21:48:36 +00:00
}
else {
2007-04-06 04:39:51 +00:00
$form['_path'] = array(
'#type' => 'item',
2006-03-23 21:48:36 +00:00
'#title' => t('Path'),
2007-06-05 09:15:02 +00:00
'#description' => l($item['link_title'], $item['href'], $item['options']),
2006-03-23 09:34:14 +00:00
);
2004-04-15 20:49:42 +00:00
}
2007-06-05 09:15:02 +00:00
$form['original_item'] = array('#type' => 'value', '#value' => $item);
2004-04-15 20:49:42 +00:00
2007-04-06 04:39:51 +00:00
$form['expanded'] = array(
'#type' => 'checkbox',
2006-03-23 21:48:36 +00:00
'#title' => t('Expanded'),
2007-06-05 09:15:02 +00:00
'#default_value' => $item['expanded'],
2006-03-23 21:48:36 +00:00
'#description' => t('If selected and this menu item has children, the menu will always appear expanded.'),
);
2004-04-15 20:49:42 +00:00
2006-03-23 21:48:36 +00:00
// Generate a list of possible parents (not including this item or descendants).
2007-06-05 09:15:02 +00:00
$options = menu_parent_options($item['mlid'], $item['menu_name']);
$form['plid'] = array(
2007-04-06 04:39:51 +00:00
'#type' => 'select',
2006-03-23 21:48:36 +00:00
'#title' => t('Parent item'),
2007-06-05 09:15:02 +00:00
'#default_value' => $item['plid'],
2006-03-23 21:48:36 +00:00
'#options' => $options,
);
2007-04-06 04:39:51 +00:00
$form['weight'] = array(
'#type' => 'weight',
2006-03-23 21:48:36 +00:00
'#title' => t('Weight'),
2007-06-05 09:15:02 +00:00
'#default_value' => isset($item['weight']) ? $item['weight'] : 0,
2006-03-23 21:48:36 +00:00
'#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'));
2004-04-15 20:49:42 +00:00
2006-08-18 18:58:47 +00:00
return $form;
2004-04-15 20:49:42 +00:00
}
2007-06-05 09:15:02 +00:00
function menu_edit_item_validate($form, &$form_state) {
$item = $form_state['values'];
2007-06-06 06:18:47 +00:00
if (isset($item['link_path']) && !menu_path_is_external($item['link_path'])) {
$path = $item['link_path'];
2007-04-06 04:39:51 +00:00
$item = menu_get_item($path);
2007-06-05 09:15:02 +00:00
if (!$item || !$item['access']) {
2007-04-06 04:39:51 +00:00
form_set_error('path', t('This path is either invalid or you do not have access to it'));
}
}
}
2007-06-05 09:15:02 +00:00
2004-08-15 16:42:59 +00:00
/**
2006-03-23 21:48:36 +00:00
* Process menu and menu item add/edit form submissions.
2004-08-15 16:42:59 +00:00
*/
2007-06-05 09:15:02 +00:00
function menu_edit_item_submit($form, &$form_state) {
menu_link_save($form_state['values']);
$form_state['redirect'] = 'admin/build/menu/'. $form_state['values']['menu_name'];
2004-08-15 16:42:59 +00:00
}
2004-04-15 20:49:42 +00:00
/**
2007-04-06 04:39:51 +00:00
* Return a list of menu items that are valid possible parents for the
* given menu item. The list excludes the given item and its children.
*
2007-06-05 09:15:02 +00:00
* @param $mlid
2007-04-06 04:39:51 +00:00
* The menu item id for which to generate a list of parents.
2007-06-05 09:15:02 +00:00
* If $mlid == 0 then the complete tree is returned.
* @param $menu_name
* The name of the menu.
* @param $plid
* The menu link item id of the menu item at which to start the tree.
2007-04-06 04:39:51 +00:00
* If $pid > 0 then this item will be included in the tree.
* @param $depth
* The current depth in the tree - used when recursing to indent the tree.
* @return
2007-06-05 09:15:02 +00:00
* An array of menu titles keyed on the mlid.
2007-04-06 04:39:51 +00:00
*/
2007-06-05 09:15:02 +00:00
function menu_parent_options($mlid, $menu_name, $plid = 0, $depth = 0) {
2007-04-06 04:39:51 +00:00
$options = array(0 => t('Root'));
2007-06-05 09:15:02 +00:00
// Exclude $mlid and its children from the list unless $mlid is 0.
if ($mlid && $mlid == $plid) {
2007-04-06 04:39:51 +00:00
return $options;
}
2007-06-05 09:15:02 +00:00
$sql = "SELECT * FROM {menu_links} ml LEFT JOIN {menu_router} mr ON ml.router_path = mr.path WHERE menu_name = '%s' AND hidden >= 0";
$params = array($menu_name);
if ($mlid && ($item = menu_link_load($mlid))) {
$parents = array();
for ($i = 1; $i <= 6; $i++) {
$key = "p$i";
$value = $item[$key];
if ($value) {
$parents[]= "$key != %d";
$params[] = $value;
}
else {
break;
}
2007-04-06 04:39:51 +00:00
}
2007-06-05 09:15:02 +00:00
$sql .= ' AND (' . implode(' OR ', $parents) .')';
2007-04-06 04:39:51 +00:00
}
2007-06-05 09:15:02 +00:00
$sql .= ' ORDER BY p1, p2, p3, p4, p5';
$result = db_query($sql, $params);
while ($item = db_fetch_array($result)) {
_menu_link_translate($item);
if (!$item['access']) {
continue;
2007-04-06 04:39:51 +00:00
}
2007-06-05 09:15:02 +00:00
$title = str_repeat('--', $item['depth']) .' '. $item['link_title'];
if ($item['hidden']) {
$title .= ' ('. t('disabled') .')';
2007-04-06 04:39:51 +00:00
}
2007-06-05 09:15:02 +00:00
$options[$item['mlid']] = $title;
2007-04-06 04:39:51 +00:00
}
2007-06-05 09:15:02 +00:00
return $options;
2007-04-06 04:39:51 +00:00
}
/**
* Remove the menu item.
*/
function menu_node_form_delete($node) {
2007-06-05 09:15:02 +00:00
menu_link_delete(NULL, 'node/'. $node->nid);
2007-04-06 04:39:51 +00:00
}
/**
* Menu callback; handle the adding/editing of a new menu.
*/
2007-06-05 09:15:02 +00:00
function menu_edit_menu(&$form_state, $type, $menu_name = '') {
2007-04-06 04:39:51 +00:00
if ($type == 'edit') {
2007-06-05 09:15:02 +00:00
$menu = db_fetch_array(db_query("SELECT * FROM {menu_custom} WHERE menu_name = '%s'", $menu_name));
$form['menu_name'] = array('#type' => 'value', '#value' => $menu_name);
$form['#insert'] = FALSE;
2007-04-06 04:39:51 +00:00
}
else {
2007-06-05 09:15:02 +00:00
$menu = array('menu_name' => '', 'title' => '', 'description' => '');
$form['menu_name'] = array(
'#type' => 'textfield',
'#title' => t('Menu name'),
'#description' => t('The machine-readable name of this menu. This text will be used for constructing the URL of the <em>menu overwrite</em> page for this menu. This name may consist of only of lowercase letters, numbers and hypens and must be unique.'),
'#required' => TRUE,
);
$form['#insert'] = TRUE;
2007-04-06 04:39:51 +00:00
}
2007-06-05 09:15:02 +00:00
$form['#title'] = $menu['title'];
$form['title'] = array(
'#type' => 'textfield',
2007-04-06 04:39:51 +00:00
'#title' => t('Title'),
2007-06-05 09:15:02 +00:00
'#default_value' => $menu['title'],
2007-04-06 04:39:51 +00:00
'#required' => TRUE,
2007-06-05 09:15:02 +00:00
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => $menu['description'],
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
2007-04-06 04:39:51 +00:00
);
return $form;
}
2007-06-05 09:15:02 +00:00
function menu_edit_menu_validate($form, &$form_state) {
$item = $form_state['values'];
if (preg_match('/[^a-z0-9-]/', $item['menu_name'])) {
form_set_error('menu_name', t('Menu name may consist of only of lowercase letters, numbers and hypens.'));
}
if ($form['#insert'] &&
(db_result(db_query("SELECT menu_name FROM {menu_custom} WHERE menu_name = '%s'", $item['menu_name'])) ||
db_result(db_query("SELECT menu_name FROM {menu_links} WHERE menu_name = '%s'", $item['menu_name'])))) {
form_set_error('menu_name', t('Menu already exists'));
}
}
function menu_edit_menu_submit($form, &$form_state) {
$menu = $form_state['values'];
$redirect = 'admin/build/menu/'. $menu['menu_name'];
if ($form['#insert']) {
db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '%s')", $menu['menu_name'], $menu['title'], $menu['description']);
}
else {
db_query("UPDATE {menu_custom} SET title = '%s', description = '%s' WHERE menu_name = '%s'", $menu['title'], $menu['description'], $menu['menu_name']);
db_query("UPDATE {menu_links} SET link_title = '%s' WHERE link_title = '%s' AND router_path = '%s'", $menu['title'], $form['#title'], $redirect);
}
menu_rebuild();
$form_state['redirect'] = $redirect;
}
2007-04-06 04:39:51 +00:00
/**
* Menu callback; delete a single custom item.
*/
2007-06-05 09:15:02 +00:00
function menu_item_delete_form(&$form_state, $mlid) {
if (!$item = menu_link_load($mlid)) {
2006-03-23 21:48:36 +00:00
drupal_not_found();
return;
}
2007-06-05 09:15:02 +00:00
$form['#item'] = $item;
2006-03-23 21:48:36 +00:00
2007-06-22 08:32:28 +00:00
return confirm_form($form, t('Are you sure you want to delete the custom menu item %item?', array('%item' => $item['link_title'])), 'admin/build/menu/'. $item['menu_name']);
2006-03-23 21:48:36 +00:00
}
2006-01-19 08:58:00 +00:00
2006-03-23 21:48:36 +00:00
/**
* Process menu delete form submissions.
*/
2007-06-04 07:22:23 +00:00
function menu_item_delete_form_submit($form, &$form_state) {
2007-06-05 09:15:02 +00:00
$item = $form['#item'];
menu_link_delete($item['mlid']);
$t_args = array('%title' => $item['link_title']);
drupal_set_message(t('The menu item %title has been deleted.', $t_args));
watchdog('menu', 'Deleted menu item %title.', $t_args, WATCHDOG_NOTICE);
$form_state['redirect'] = 'admin/build/menu/'. $item['menu_name'];
2004-04-15 20:49:42 +00:00
}
2006-03-23 09:34:14 +00:00
/**
* Menu callback; reset a single modified item.
*/
2007-06-05 09:15:02 +00:00
function menu_reset_item(&$form_state, $mlid) {
if (isset($mlid) && $item = db_fetch_array(db_query('SELECT router_path, link_title FROM {menu_links} WHERE mlid = %d', $mlid))) {
$form['#router_path'] = $item['router_path'];
2007-06-22 08:32:28 +00:00
$options = array(
'description' => t('Any customizations will be lost. This action cannot be undone.'),
2007-06-24 10:09:53 +00:00
'yes' => t('Reset'),
'cancel' => 'admin/build/menu',
2007-06-22 08:32:28 +00:00
);
2007-06-24 10:09:53 +00:00
return confirm_form($form, t('Are you sure you want to reset the item %item to its default values?', array('%item' => $item['link_title'])), 'admin/build/menu/navigation', $options);
2006-03-23 21:48:36 +00:00
}
else {
drupal_not_found();
2006-03-23 09:34:14 +00:00
}
}
/**
2006-03-23 21:48:36 +00:00
* Process menu reset item form submissions.
2006-03-23 09:34:14 +00:00
*/
2007-06-04 07:22:23 +00:00
function menu_reset_item_submit($form, &$form_state) {
2007-06-05 09:15:02 +00:00
$new_item = db_fetch_array(db_query("SELECT * FROM {menu_router} WHERE path = '%s'", $form['#router_path']));
menu_link_save(_menu_link_build($new_item));
2006-03-23 21:48:36 +00:00
drupal_set_message(t('The menu item was reset to its default settings.'));
2007-06-05 09:15:02 +00:00
$form_state['redirect'] = 'admin/build/menu/navigation';
2006-03-23 09:34:14 +00:00
}
2007-04-06 04:39:51 +00:00
// Conversion ends here.
2006-03-23 21:48:36 +00:00
/**
2007-04-06 04:39:51 +00:00
* Implementation of hook_block().
2006-03-23 21:48:36 +00:00
*/
2007-06-05 09:15:02 +00:00
function menu_block($op = 'list', $delta = 0) {
$custom_menus = menu_get_menus();
2007-04-06 04:39:51 +00:00
if ($op == 'list') {
$blocks = array();
2007-06-05 09:15:02 +00:00
foreach ($custom_menus as $name => $title) {
2007-04-06 04:39:51 +00:00
// Default "Navigation" block is handled by user.module.
2007-06-05 09:15:02 +00:00
$blocks[$name]['info'] = check_plain($title);
2006-11-07 05:46:31 +00:00
}
2007-04-06 04:39:51 +00:00
return $blocks;
2006-03-23 21:48:36 +00:00
}
2007-04-06 04:39:51 +00:00
else if ($op == 'view') {
2007-06-05 09:15:02 +00:00
$data['subject'] = check_plain($custom_menus[$delta]);
$data['content'] = menu_tree($delta);
2007-04-06 04:39:51 +00:00
return $data;
}
}
2006-03-23 21:48:36 +00:00
2007-04-06 04:39:51 +00:00
/**
* Implementation of hook_nodeapi().
*/
2007-06-05 09:15:02 +00:00
function menu_nodeapi($node, $op) {
if (user_access('administer menu') && isset($node->menu)) {
$item = $node->menu;
2007-04-06 04:39:51 +00:00
switch ($op) {
2007-06-05 09:15:02 +00:00
case 'delete':
$item['delete'] = 1;
// Deliberate no break.
2007-04-06 04:39:51 +00:00
case 'insert':
case 'update':
2007-06-05 09:15:02 +00:00
if ($item['delete']) {
_menu_delete_item($item);
2007-04-06 04:39:51 +00:00
}
2007-06-05 09:15:02 +00:00
else {
$item['link_path'] = "node/$node->nid";
menu_link_save($item);
2007-04-06 04:39:51 +00:00
}
break;
}
2006-03-23 21:48:36 +00:00
}
}
2006-01-19 08:58:00 +00:00
/**
2007-04-06 04:39:51 +00:00
* Implementation of hook_form_alter().
* Add menu item fields to the node form.
2006-01-19 08:58:00 +00:00
*/
2007-06-05 09:15:02 +00:00
function menu_form_alter(&$form, $form_state, $form_id) {
2007-04-06 04:39:51 +00:00
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
2007-06-05 09:15:02 +00:00
$form['menu'] = array(
'#type' => 'fieldset',
2007-04-06 04:39:51 +00:00
'#title' => t('Menu settings'),
'#access' => user_access('administer menu'),
'#collapsible' => TRUE,
2007-06-05 09:15:02 +00:00
'#collapsed' => FALSE,
2007-04-06 04:39:51 +00:00
'#tree' => TRUE,
'#weight' => 30,
);
2007-06-05 09:15:02 +00:00
$form_state = array();
if ($mlid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = 'node/%d' AND menu_name = '%s'", $form['nid']['#value'], variable_get('menu_parent_items', 'navigation')))) {
$menu_form = drupal_retrieve_form('menu_edit_item', $form_state, 'edit', $mlid);
$menu_form['delete'] = array(
'#type' => 'checkbox',
2007-04-06 04:39:51 +00:00
'#title' => t('Check to delete this menu item.'),
);
2006-01-19 08:58:00 +00:00
}
2007-06-05 09:15:02 +00:00
else {
$menu_form = drupal_retrieve_form('menu_edit_item', $form_state, 'add', variable_get('menu_parent_items', 'navigation'));
unset($menu_form['link_path']);
$menu_form['link_title']['#required'] = FALSE;
$form['menu']['#collapsed'] = TRUE;
}
unset($menu_form['submit']);
$form['menu'] += $menu_form;
2006-01-19 08:58:00 +00:00
}
}
2007-06-05 09:15:02 +00:00
function menu_get_menus($all = FALSE) {
$sql = 'SELECT * FROM {menu_custom}'. ($all ? '' : " WHERE menu_name NOT IN ('navigation', 'primary-links', 'secondary-links')") . ' ORDER BY title';
$result = db_query($sql);
$rows = array();
while ($r = db_fetch_array($result)) {
$rows[$r['menu_name']] = $r['title'];
}
return $rows;
2007-05-16 13:45:17 +00:00
}
2006-01-19 08:58:00 +00:00
/**
2007-04-06 04:39:51 +00:00
* Menu callback; presents menu configuration options.
2006-01-19 08:58:00 +00:00
*/
2007-04-06 04:39:51 +00:00
function menu_configure() {
2007-06-05 09:15:02 +00:00
$form['intro'] = array(
'#type' => 'item',
2007-04-06 04:39:51 +00:00
'#value' => t('The menu module allows on-the-fly creation of menu links in the content authoring forms. The following option limits the menus in which a new link may be added. 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.'),
);
2007-06-05 09:15:02 +00:00
$authoring_options = menu_get_menus(TRUE);
2007-04-06 04:39:51 +00:00
2007-06-05 09:15:02 +00:00
$form['menu_parent_items'] = array('#type' => 'select',
2007-04-06 04:39:51 +00:00
'#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 content authoring form. Only this menu item and its children will be shown.'),
2007-06-05 09:15:02 +00:00
);
2007-04-06 04:39:51 +00:00
return system_settings_form($form);
2006-01-19 08:58:00 +00:00
}