Newer
Older
<?php
Dries Buytaert
committed
* Allows administrators to customize the site's navigation menus.
*
* A menu (in this context) is a hierarchical collection of links, generally
* used for navigation. This is not to be confused with the
* @link menu Menu system @endlink of menu.inc and hook_menu(), which defines
* page routing requests for Drupal, and also allows the defined page routing
* URLs to be added to the main site navigation menu.
/**
* Maximum length of menu name as entered by the user. Database length is 32
* and we add a menu- prefix.
*/
Gábor Hojtsy
committed
define('MENU_MAX_MENU_NAME_LENGTH_UI', 27);
/**
Dries Buytaert
committed
* Implements hook_help().
*/
Gábor Hojtsy
committed
function menu_help($path, $arg) {
switch ($path) {
Dries Buytaert
committed
case 'admin/help#menu':
Angie Byron
committed
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Menu module provides an interface for managing menus. A menu is a hierarchical collection of links, which can be within or external to the site, generally used for navigation. Each menu is rendered in a block that can be enabled and positioned through the <a href="@blocks">Blocks administration page</a>. You can view and manage menus on the <a href="@menus">Menus administration page</a>. For more information, see the online handbook entry for the <a href="@menu">Menu module</a>.', array('@blocks' => url('admin/structure/block'), '@menus' => url('admin/structure/menu'), '@menu' => 'http://drupal.org/handbook/modules/menu/')) . '</p>';
Angie Byron
committed
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Managing menus') . '</dt>';
$output .= '<dd>' . t('Users with the <em>Administer menus and menu items</em> permission can add, edit and delete custom menus on the <a href="@menu">Menus administration page</a>. Custom menus can be special site menus, menus of external links, or any combination of internal and external links. You may create an unlimited number of additional menus, each of which will automatically have an associated block. By selecting <em>list links</em>, you can add, edit, or delete links for a given menu. The links listing page provides a drag-and-drop interface for controlling the order of links, and creating a hierarchy within the menu.', array('@menu' => url('admin/structure/menu'), '@add-menu' => url('admin/structure/menu/add'))) . '</dd>';
Angie Byron
committed
$output .= '<dt>' . t('Displaying menus') . '</dt>';
$output .= '<dd>' . t('After you have created a menu, you must enable and position the associated block on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</dd>';
$output .= '</dl>';
Dries Buytaert
committed
return $output;
Dries Buytaert
committed
case 'admin/structure/menu/add':
return '<p>' . t('You can enable the newly-created block for this menu on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</p>';
}
Dries Buytaert
committed
if ($path == 'admin/structure/menu' && module_exists('block')) {
return '<p>' . t('Each menu has a corresponding block that is managed on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</p>';
}
Dries Buytaert
committed
* Implements hook_permission().
function menu_permission() {
return array(
Angie Byron
committed
'administer menu' => array(
'title' => t('Administer menus and menu items'),
Angie Byron
committed
),
);
/**
Dries Buytaert
committed
* Implements hook_menu().
*/
Dries Buytaert
committed
function menu_menu() {
Dries Buytaert
committed
$items['admin/structure/menu'] = array(
'title' => 'Menus',
'description' => 'Add new menus to your site, edit existing menus, and rename and reorganize menu links.',
Dries Buytaert
committed
'page callback' => 'menu_overview_page',
'access callback' => 'user_access',
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
Dries Buytaert
committed
$items['admin/structure/menu/parents'] = array(
'title' => 'Parent menu items',
'page callback' => 'menu_parent_options_js',
'type' => MENU_CALLBACK,
'access arguments' => array(TRUE),
);
Dries Buytaert
committed
$items['admin/structure/menu/list'] = array(
'title' => 'List menus',
Gábor Hojtsy
committed
'weight' => -10,
);
Dries Buytaert
committed
$items['admin/structure/menu/add'] = array(
'title' => 'Add menu',
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_edit_menu', 'add'),
Dries Buytaert
committed
'access arguments' => array('administer menu'),
Angie Byron
committed
'type' => MENU_LOCAL_ACTION,
'file' => 'menu.admin.inc',
Gábor Hojtsy
committed
);
Dries Buytaert
committed
$items['admin/structure/menu/settings'] = array(
Dries Buytaert
committed
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_configure'),
Dries Buytaert
committed
'access arguments' => array('administer menu'),
Dries Buytaert
committed
'type' => MENU_LOCAL_TASK,
Gábor Hojtsy
committed
'weight' => 5,
'file' => 'menu.admin.inc',
Gábor Hojtsy
committed
);
Dries Buytaert
committed
$items['admin/structure/menu/manage/%menu'] = array(
Dries Buytaert
committed
'title' => 'Customize menu',
Gábor Hojtsy
committed
'page callback' => 'drupal_get_form',
Dries Buytaert
committed
'page arguments' => array('menu_overview_form', 4),
'title callback' => 'menu_overview_title',
Dries Buytaert
committed
'title arguments' => array(4),
Dries Buytaert
committed
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
Gábor Hojtsy
committed
);
Dries Buytaert
committed
$items['admin/structure/menu/manage/%menu/list'] = array(
Dries Buytaert
committed
'title' => 'List links',
Dries Buytaert
committed
'weight' => -10,
Gábor Hojtsy
committed
'type' => MENU_DEFAULT_LOCAL_TASK,
Angie Byron
committed
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
Gábor Hojtsy
committed
);
Dries Buytaert
committed
$items['admin/structure/menu/manage/%menu/add'] = array(
Dries Buytaert
committed
'title' => 'Add link',
Dries Buytaert
committed
'page callback' => 'drupal_get_form',
Dries Buytaert
committed
'page arguments' => array('menu_edit_item', 'add', NULL, 4),
Dries Buytaert
committed
'access arguments' => array('administer menu'),
Angie Byron
committed
'type' => MENU_LOCAL_ACTION,
'file' => 'menu.admin.inc',
Gábor Hojtsy
committed
);
Dries Buytaert
committed
$items['admin/structure/menu/manage/%menu/edit'] = array(
Dries Buytaert
committed
'title' => 'Edit menu',
'page callback' => 'drupal_get_form',
Dries Buytaert
committed
'page arguments' => array('menu_edit_menu', 'edit', 4),
Dries Buytaert
committed
'access arguments' => array('administer menu'),
Gábor Hojtsy
committed
'type' => MENU_LOCAL_TASK,
Angie Byron
committed
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'menu.admin.inc',
Gábor Hojtsy
committed
);
Dries Buytaert
committed
$items['admin/structure/menu/manage/%menu/delete'] = array(
'title' => 'Delete menu',
'page callback' => 'menu_delete_menu_page',
Dries Buytaert
committed
'page arguments' => array(4),
Dries Buytaert
committed
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
Gábor Hojtsy
committed
);
Dries Buytaert
committed
$items['admin/structure/menu/item/%menu_link/edit'] = array(
Dries Buytaert
committed
'title' => 'Edit menu link',
'page callback' => 'drupal_get_form',
Dries Buytaert
committed
'page arguments' => array('menu_edit_item', 'edit', 4, NULL),
Dries Buytaert
committed
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
Gábor Hojtsy
committed
);
Dries Buytaert
committed
$items['admin/structure/menu/item/%menu_link/reset'] = array(
Dries Buytaert
committed
'title' => 'Reset menu link',
'page callback' => 'drupal_get_form',
'page arguments' => array('menu_reset_item_confirm', 4),
Dries Buytaert
committed
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
Gábor Hojtsy
committed
);
Dries Buytaert
committed
$items['admin/structure/menu/item/%menu_link/delete'] = array(
Dries Buytaert
committed
'title' => 'Delete menu link',
'page callback' => 'menu_item_delete_page',
'page arguments' => array(4),
Dries Buytaert
committed
'access arguments' => array('administer menu'),
'file' => 'menu.admin.inc',
Gábor Hojtsy
committed
);
Dries Buytaert
committed
}
Gábor Hojtsy
committed
/**
Dries Buytaert
committed
* Implements hook_theme().
Gábor Hojtsy
committed
*/
function menu_theme() {
return array(
'menu_overview_form' => array(
'file' => 'menu.admin.inc',
'render element' => 'form',
Gábor Hojtsy
committed
),
Dries Buytaert
committed
'menu_admin_overview' => array(
'file' => 'menu.admin.inc',
'variables' => array('title' => NULL, 'name' => NULL, 'description' => NULL),
Dries Buytaert
committed
),
Gábor Hojtsy
committed
);
}
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implements hook_enable().
Dries Buytaert
committed
*
* Add a link for each custom menu.
Dries Buytaert
committed
*/
function menu_enable() {
menu_rebuild();
$base_link = db_query("SELECT mlid AS plid, menu_name FROM {menu_links} WHERE link_path = 'admin/structure/menu' AND module = 'system'")->fetchAssoc();
Dries Buytaert
committed
$base_link['router_path'] = 'admin/structure/menu/manage/%';
$base_link['module'] = 'menu';
Dries Buytaert
committed
$result = db_query("SELECT * FROM {menu_custom}", array(), array('fetch' => PDO::FETCH_ASSOC));
foreach ($result as $menu) {
// $link is passed by reference to menu_link_save(), so we make a copy of $base_link.
$link = $base_link;
Dries Buytaert
committed
$link['mlid'] = 0;
$link['link_title'] = $menu['title'];
Dries Buytaert
committed
$link['link_path'] = 'admin/structure/menu/manage/' . $menu['menu_name'];
Dries Buytaert
committed
$menu_link = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND plid = :plid", array(
':path' => $link['link_path'],
':plid' => $link['plid']
))
->fetchField();
if (!$menu_link) {
Dries Buytaert
committed
}
menu_cache_clear_all();
}
/**
* Title callback for the menu overview page and links.
*/
function menu_overview_title($menu) {
}
Dries Buytaert
committed
/**
* Load the data for a single custom menu.
*
* @param $menu_name
* The unique name of a custom menu to load.
Dries Buytaert
committed
* @return
* Array defining the custom menu, or FALSE if the menu doesn't exist.
Dries Buytaert
committed
*/
function menu_load($menu_name) {
Dries Buytaert
committed
$all_menus = menu_load_all();
return isset($all_menus[$menu_name]) ? $all_menus[$menu_name] : FALSE;
}
/**
* Load all custom menu data.
*
* @return
* Array of custom menu data.
*/
function menu_load_all() {
$custom_menus = &drupal_static(__FUNCTION__);
if (!isset($custom_menus)) {
Dries Buytaert
committed
if ($cached = cache('menu')->get('menu_custom')) {
Dries Buytaert
committed
$custom_menus = $cached->data;
}
else {
$custom_menus = db_query('SELECT * FROM {menu_custom}')->fetchAllAssoc('menu_name', PDO::FETCH_ASSOC);
Dries Buytaert
committed
cache('menu')->set('menu_custom', $custom_menus);
Dries Buytaert
committed
}
}
return $custom_menus;
Dries Buytaert
committed
}
/**
* Save a custom menu.
*
* @param $menu
* An array representing a custom menu:
Angie Byron
committed
* - menu_name: The unique name of the custom menu (composed of lowercase
* letters, numbers, and hyphens).
* - title: The human readable menu title.
* - description: The custom menu description.
*
* Modules should always pass a fully populated $menu when saving a custom
* menu, so other modules are able to output proper status or watchdog messages.
*
* @see menu_load()
*/
function menu_save($menu) {
Angie Byron
committed
$status = db_merge('menu_custom')
->key(array('menu_name' => $menu['menu_name']))
->fields(array(
'title' => $menu['title'],
'description' => $menu['description'],
))
->execute();
Dries Buytaert
committed
menu_cache_clear_all();
Angie Byron
committed
switch ($status) {
case SAVED_NEW:
module_invoke_all('menu_insert', $menu);
break;
case SAVED_UPDATED:
module_invoke_all('menu_update', $menu);
break;
}
}
/**
* Delete a custom menu and all contained links.
*
* Note that this function deletes all menu links in a custom menu. While menu
* links derived from router paths may be restored by rebuilding the menu, all
* customized and custom links will be irreversibly gone. Therefore, this
* function should usually be called from a user interface (form submit) handler
* only, which allows the user to confirm the action.
*
* @param $menu
* An array representing a custom menu:
* - menu_name: The unique name of the custom menu.
* - title: The human readable menu title.
* - description: The custom menu description.
*
* Modules should always pass a fully populated $menu when deleting a custom
* menu, so other modules are able to output proper status or watchdog messages.
*
* @see menu_load()
*
Dries Buytaert
committed
* menu_delete_links() will take care of clearing the page cache. Other modules
* should take care of their menu-related data by implementing
* hook_menu_delete().
*/
function menu_delete($menu) {
// Delete all links from the menu.
Dries Buytaert
committed
menu_delete_links($menu['menu_name']);
// Delete the custom menu.
db_delete('menu_custom')
->condition('menu_name', $menu['menu_name'])
->execute();
Dries Buytaert
committed
menu_cache_clear_all();
module_invoke_all('menu_delete', $menu);
}
/**
* 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().
Dries Buytaert
committed
* @param $item
Dries Buytaert
committed
* The menu item or the node type for which to generate a list of parents.
* If $item['mlid'] == 0 then the complete tree is returned.
Dries Buytaert
committed
* @param $type
* The node type for which to generate a list of parents.
* If $item itself is a node type then $type is ignored.
* 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.
*
* @todo This has to be turned into a #process form element callback. The
* 'menu_override_parent_selector' variable is entirely superfluous.
Dries Buytaert
committed
function menu_parent_options($menus, $item, $type = '') {
Gábor Hojtsy
committed
// The menu_links table can be practically any size and we need a way to
// allow contrib modules to provide more scalable pattern choosers.
// hook_form_alter is too late in itself because all the possible parents are
// retrieved here, unless menu_override_parent_selector is set to TRUE.
if (variable_get('menu_override_parent_selector', FALSE)) {
return array();
}
Dries Buytaert
committed
$available_menus = array();
Dries Buytaert
committed
if (!is_array($item)) {
// If $item is not an array then it is a node type.
// Use it as $type and prepare a dummy menu item for _menu_get_options().
$type = $item;
$item = array('mlid' => 0);
}
if (empty($type)) {
// If no node type is set, use all menus given to this function.
Dries Buytaert
committed
$available_menus = $menus;
}
else {
Dries Buytaert
committed
// If a node type is set, use all available menus for this type.
$type_menus = variable_get('menu_options_' . $type, array('main-menu' => 'main-menu'));
Dries Buytaert
committed
foreach ($type_menus as $menu) {
$available_menus[$menu] = $menu;
}
}
return _menu_get_options($menus, $available_menus, $item);
}
/**
* Page callback.
* Get all the available menus and menu items as a JavaScript array.
Dries Buytaert
committed
*/
function menu_parent_options_js() {
$available_menus = array();
if (isset($_POST['menus']) && count($_POST['menus'])) {
foreach ($_POST['menus'] as $menu) {
$available_menus[$menu] = $menu;
}
}
$options = _menu_get_options(menu_get_menus(), $available_menus, array('mlid' => 0));
drupal_json_output($options);
Dries Buytaert
committed
}
/**
* Helper function to get the items of the given menu.
*/
function _menu_get_options($menus, $available_menus, $item) {
Gábor Hojtsy
committed
// If the item has children, there is an added limit to the depth of valid parents.
Gábor Hojtsy
committed
if (isset($item['parent_depth_limit'])) {
$limit = $item['parent_depth_limit'];
}
else {
$limit = _menu_parent_depth_limit($item);
}
Dries Buytaert
committed
$options = array();
foreach ($menus as $menu_name => $title) {
Dries Buytaert
committed
if (isset($available_menus[$menu_name])) {
$tree = menu_tree_all_data($menu_name, NULL);
$options[$menu_name . ':0'] = '<' . $title . '>';
_menu_parents_recurse($tree, $menu_name, '--', $options, $item['mlid'], $limit);
}
}
return $options;
Dries Buytaert
committed
* Recursive helper function for menu_parent_options().
Gábor Hojtsy
committed
function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) {
Dries Buytaert
committed
foreach ($tree as $data) {
Gábor Hojtsy
committed
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);
Dries Buytaert
committed
if ($data['link']['hidden']) {
$title .= ' (' . t('disabled') . ')';
Dries Buytaert
committed
}
$options[$menu_name . ':' . $data['link']['mlid']] = $title;
Gábor Hojtsy
committed
if ($data['below']) {
_menu_parents_recurse($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit);
Dries Buytaert
committed
}
}
}
* Reset a system-defined menu link.
function menu_reset_item($link) {
// To reset the link to its original values, we need to retrieve its
// definition from hook_menu(). Otherwise, for example, the link's menu would
// not be reset, because properties like the original 'menu_name' are not
// stored anywhere else. Since resetting a link happens rarely and this is a
// one-time operation, retrieving the full menu router does no harm.
$menu = menu_get_router();
$router_item = $menu[$link['router_path']];
$new_link = _menu_link_build($router_item);
// Merge existing menu link's ID and 'has_children' property.
Dries Buytaert
committed
foreach (array('mlid', 'has_children') as $key) {
$new_link[$key] = $link[$key];
Dries Buytaert
committed
}
menu_link_save($new_link);
return $new_link;
Dries Buytaert
committed
* Implements hook_block_info().
Dries Buytaert
committed
function menu_block_info() {
Dries Buytaert
committed
$menus = menu_get_menus(FALSE);
Dries Buytaert
committed
$blocks = array();
foreach ($menus as $name => $title) {
// Default "Navigation" block is handled by user.module.
$blocks[$name]['info'] = check_plain($title);
// Menu blocks can't be cached because each menu item can have
// a custom access callback. menu.inc manages its own caching.
$blocks[$name]['cache'] = DRUPAL_NO_CACHE;
Dries Buytaert
committed
return $blocks;
}
/**
Dries Buytaert
committed
* Implements hook_block_view().
Dries Buytaert
committed
*/
function menu_block_view($delta = '') {
Dries Buytaert
committed
$menus = menu_get_menus(FALSE);
Dries Buytaert
committed
$data['subject'] = check_plain($menus[$delta]);
$data['content'] = menu_tree($delta);
Angie Byron
committed
// Add contextual links for this block.
if (!empty($data['content'])) {
Dries Buytaert
committed
$data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($delta));
Angie Byron
committed
}
Dries Buytaert
committed
return $data;
Angie Byron
committed
/**
Dries Buytaert
committed
* Implements hook_block_view_alter().
Angie Byron
committed
*/
function menu_block_view_alter(&$data, $block) {
// Add contextual links for system menu blocks.
if ($block->module == 'system' && !empty($data['content'])) {
$system_menus = menu_list_system_menus();
if (isset($system_menus[$block->delta])) {
Dries Buytaert
committed
$data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($block->delta));
Angie Byron
committed
}
}
}
Dries Buytaert
committed
* Implements hook_node_insert().
function menu_node_insert($node) {
menu_node_save($node);
Dries Buytaert
committed
}
/**
Dries Buytaert
committed
* Implements hook_node_update().
Dries Buytaert
committed
*/
function menu_node_update($node) {
menu_node_save($node);
}
/**
* Helper for hook_node_insert() and hook_node_update().
*/
function menu_node_save($node) {
Dries Buytaert
committed
if (isset($node->menu)) {
$link = &$node->menu;
if (empty($link['enabled'])) {
if (!empty($link['mlid'])) {
menu_link_delete($link['mlid']);
}
Dries Buytaert
committed
}
elseif (trim($link['link_title'])) {
$link['link_title'] = trim($link['link_title']);
$link['link_path'] = "node/$node->nid";
Dries Buytaert
committed
if (trim($link['description'])) {
$link['options']['attributes']['title'] = trim($link['description']);
}
else {
// If the description field was left empty, remove the title attribute
// from the menu link.
unset($link['options']['attributes']['title']);
Dries Buytaert
committed
}
if (!menu_link_save($link)) {
Dries Buytaert
committed
drupal_set_message(t('There was an error saving the menu link.'), 'error');
Gábor Hojtsy
committed
}
Dries Buytaert
committed
}
}
}
/**
Dries Buytaert
committed
* Implements hook_node_delete().
Dries Buytaert
committed
*/
function menu_node_delete($node) {
Dries Buytaert
committed
// Delete all menu module links that point to this node.
$result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu'", array(':path' => 'node/' . $node->nid), array('fetch' => PDO::FETCH_ASSOC));
Dries Buytaert
committed
foreach ($result as $m) {
Dries Buytaert
committed
menu_link_delete($m['mlid']);
}
}
/**
Dries Buytaert
committed
* Implements hook_node_prepare().
Dries Buytaert
committed
*/
function menu_node_prepare($node) {
Dries Buytaert
committed
if (empty($node->menu)) {
// Prepare the node for the edit form so that $node->menu always exists.
Dries Buytaert
committed
$menu_name = strtok(variable_get('menu_parent_' . $node->type, 'main-menu:0'), ':');
Dries Buytaert
committed
$item = array();
if (isset($node->nid)) {
Dries Buytaert
committed
$mlid = FALSE;
Dries Buytaert
committed
// Give priority to the default menu
Dries Buytaert
committed
$type_menus = variable_get('menu_options_' . $node->type, array('main-menu' => 'main-menu'));
if (in_array($menu_name, $type_menus)) {
$mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND menu_name = :menu_name AND module = 'menu' ORDER BY mlid ASC", 0, 1, array(
':path' => 'node/' . $node->nid,
Dries Buytaert
committed
':menu_name' => $menu_name,
))->fetchField();
}
// Check all allowed menus if a link does not exist in the default menu.
if (!$mlid && !empty($type_menus)) {
$mlid = db_query_range("SELECT mlid FROM {menu_links} WHERE link_path = :path AND module = 'menu' AND menu_name IN (:type_menus) ORDER BY mlid ASC", 0, 1, array(
':path' => 'node/' . $node->nid,
':type_menus' => array_values($type_menus),
Angie Byron
committed
))->fetchField();
Dries Buytaert
committed
}
if ($mlid) {
$item = menu_link_load($mlid);
}
}
// Set default values.
$node->menu = $item + array(
'link_title' => '',
'mlid' => 0,
'plid' => 0,
'menu_name' => $menu_name,
'weight' => 0,
'options' => array(),
'module' => 'menu',
'expanded' => 0,
'hidden' => 0,
'has_children' => 0,
'customized' => 0,
);
Dries Buytaert
committed
}
// Find the depth limit for the parent select.
if (!isset($node->menu['parent_depth_limit'])) {
$node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu);
}
}
Gábor Hojtsy
committed
/**
* Find the depth limit for items in the parent select.
*/
function _menu_parent_depth_limit($item) {
return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0);
}
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implements hook_form_BASE_FORM_ID_alter().
*
* Adds menu item fields to the node form.
*
* @see menu_node_submit()
Dries Buytaert
committed
*/
Dries Buytaert
committed
function menu_form_node_form_alter(&$form, $form_state) {
Dries Buytaert
committed
// Generate a list of possible parents (not including this link or descendants).
Dries Buytaert
committed
// @todo This must be handled in a #process handler.
Dries Buytaert
committed
$link = $form['#node']->menu;
Dries Buytaert
committed
$type = $form['#node']->type;
Dries Buytaert
committed
// menu_parent_options() is goofy and can actually handle either a menu link
// or a node type both as second argument. Pick based on whether there is
// a link already (menu_node_prepare() sets mlid default to 0).
$options = menu_parent_options(menu_get_menus(), $link['mlid'] ? $link : $type);
Dries Buytaert
committed
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
// If no possible parent menu items were found, there is nothing to display.
if (empty($options)) {
return;
}
$form['menu'] = array(
'#type' => 'fieldset',
'#title' => t('Menu settings'),
'#access' => user_access('administer menu'),
'#collapsible' => TRUE,
'#collapsed' => !$link['link_title'],
'#group' => 'additional_settings',
'#attached' => array(
'js' => array(drupal_get_path('module', 'menu') . '/menu.js'),
),
'#tree' => TRUE,
'#weight' => -2,
'#attributes' => array('class' => array('menu-link-form')),
);
$form['menu']['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Provide a menu link'),
'#default_value' => (int) (bool) $link['mlid'],
);
$form['menu']['link'] = array(
'#type' => 'container',
'#parents' => array('menu'),
'#states' => array(
'invisible' => array(
'input[name="menu[enabled]"]' => array('checked' => FALSE),
),
Dries Buytaert
committed
),
);
Dries Buytaert
committed
Dries Buytaert
committed
// Populate the element with the link data.
foreach (array('mlid', 'module', 'hidden', 'has_children', 'customized', 'options', 'expanded', 'hidden', 'parent_depth_limit') as $key) {
$form['menu']['link'][$key] = array('#type' => 'value', '#value' => $link[$key]);
}
Dries Buytaert
committed
Dries Buytaert
committed
$form['menu']['link']['link_title'] = array(
'#type' => 'textfield',
'#title' => t('Menu link title'),
'#default_value' => $link['link_title'],
);
Dries Buytaert
committed
Dries Buytaert
committed
$form['menu']['link']['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => isset($link['options']['attributes']['title']) ? $link['options']['attributes']['title'] : '',
'#rows' => 1,
'#description' => t('Shown when hovering over the menu link.'),
);
Dries Buytaert
committed
Dries Buytaert
committed
$default = ($link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0'));
Dries Buytaert
committed
// If the current parent menu item is not present in options, use the first
// available option as default value.
// @todo User should not be allowed to access menu link settings in such a
// case.
Dries Buytaert
committed
if (!isset($options[$default])) {
Dries Buytaert
committed
$array = array_keys($options);
$default = reset($array);
Dries Buytaert
committed
}
Dries Buytaert
committed
$form['menu']['link']['parent'] = array(
'#type' => 'select',
'#title' => t('Parent item'),
'#default_value' => $default,
'#options' => $options,
'#attributes' => array('class' => array('menu-parent-select')),
);
$form['menu']['link']['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
'#delta' => 50,
'#default_value' => $link['weight'],
'#description' => t('Menu links with smaller weights are displayed before links with larger weights.'),
);
Dries Buytaert
committed
}
/**
Dries Buytaert
committed
* Implements hook_node_submit().
Dries Buytaert
committed
*
* @see menu_form_node_form_alter()
*/
Dries Buytaert
committed
function menu_node_submit($node, $form, $form_state) {
// Decompose the selected menu parent option into 'menu_name' and 'plid', if
// the form used the default parent selection widget.
if (!empty($form_state['values']['menu']['parent'])) {
Dries Buytaert
committed
list($node->menu['menu_name'], $node->menu['plid']) = explode(':', $form_state['values']['menu']['parent']);
}
}
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implements hook_form_FORM_ID_alter().
*
Dries Buytaert
committed
* Adds menu options to the node type form.
*/
function menu_form_node_type_form_alter(&$form, $form_state) {
$menu_options = menu_get_menus();
$type = $form['#node_type'];
$form['menu'] = array(
'#type' => 'fieldset',
'#title' => t('Menu settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attached' => array(
'js' => array(drupal_get_path('module', 'menu') . '/menu.admin.js'),
),
'#group' => 'additional_settings',
);
$form['menu']['menu_options'] = array(
'#type' => 'checkboxes',
'#title' => t('Available menus'),
Dries Buytaert
committed
'#default_value' => variable_get('menu_options_' . $type->type, array('main-menu')),
Dries Buytaert
committed
'#options' => $menu_options,
'#description' => t('The menus available to place links in for this content type.'),
);
// To avoid an 'illegal option' error after saving the form we have to load
// all available menu items.
// Otherwise it is not possible to dynamically add options to the list.
// @todo Convert menu_parent_options() into a #process callback.
Dries Buytaert
committed
$options = menu_parent_options(menu_get_menus(), array('mlid' => 0));
$form['menu']['menu_parent'] = array(
'#type' => 'select',
'#title' => t('Default parent item'),
'#default_value' => variable_get('menu_parent_' . $type->type, 'main-menu:0'),
'#options' => $options,
'#description' => t('Choose the menu item to be the default parent for a new link in the content authoring form.'),
'#attributes' => array('class' => array('menu-title-select')),
);
// Call Drupal.menu_update_parent_list() to filter the list of
// available default parent menu items based on the selected menus.
drupal_add_js(
'(function ($) { Drupal.menu_update_parent_list(); })(jQuery);',
array('scope' => 'footer', 'type' => 'inline')
);
}
Dries Buytaert
committed
/**
* Return an associative array of the custom menus names.
*
* @param $all
* If FALSE return only user-added menus, or if TRUE also include
* the menus defined by the system.
* @return
* An array with the machine-readable names as the keys, and human-readable
* titles as the values.
*/
function menu_get_menus($all = TRUE) {
Dries Buytaert
committed
if ($custom_menus = menu_load_all()) {
if (!$all) {
$custom_menus = array_diff_key($custom_menus, menu_list_system_menus());
}
foreach ($custom_menus as $menu_name => $menu) {
$custom_menus[$menu_name] = t($menu['title']);
}
asort($custom_menus);
Dries Buytaert
committed
return $custom_menus;