123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795 |
- <?php
- define('MENU_MAX_MENU_NAME_LENGTH_UI', 27);
- function menu_help($path, $arg) {
- switch ($path) {
- case 'admin/help#menu':
- $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/documentation/modules/menu/')) . '</p>';
- $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>';
- $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>';
- return $output;
- 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>';
- }
- 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>';
- }
- }
- function menu_permission() {
- return array(
- 'administer menu' => array(
- 'title' => t('Administer menus and menu items'),
- ),
- );
- }
- function menu_menu() {
- $items['admin/structure/menu'] = array(
- 'title' => 'Menus',
- 'description' => 'Add new menus to your site, edit existing menus, and rename and reorganize menu links.',
- 'page callback' => 'menu_overview_page',
- 'access callback' => 'user_access',
- 'access arguments' => array('administer menu'),
- 'file' => 'menu.admin.inc',
- );
- $items['admin/structure/menu/parents'] = array(
- 'title' => 'Parent menu items',
- 'page callback' => 'menu_parent_options_js',
- 'type' => MENU_CALLBACK,
- 'access arguments' => array('administer menu'),
- );
- $items['admin/structure/menu/list'] = array(
- 'title' => 'List menus',
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'weight' => -10,
- );
- $items['admin/structure/menu/add'] = array(
- 'title' => 'Add menu',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('menu_edit_menu', 'add'),
- 'access arguments' => array('administer menu'),
- 'type' => MENU_LOCAL_ACTION,
- 'file' => 'menu.admin.inc',
- );
- $items['admin/structure/menu/settings'] = array(
- 'title' => 'Settings',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('menu_configure'),
- 'access arguments' => array('administer menu'),
- 'type' => MENU_LOCAL_TASK,
- 'weight' => 5,
- 'file' => 'menu.admin.inc',
- );
- $items['admin/structure/menu/manage/%menu'] = array(
- 'title' => 'Customize menu',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('menu_overview_form', 4),
- 'title callback' => 'menu_overview_title',
- 'title arguments' => array(4),
- 'access arguments' => array('administer menu'),
- 'file' => 'menu.admin.inc',
- );
- $items['admin/structure/menu/manage/%menu/list'] = array(
- 'title' => 'List links',
- 'weight' => -10,
- 'type' => MENU_DEFAULT_LOCAL_TASK,
- 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
- );
- $items['admin/structure/menu/manage/%menu/add'] = array(
- 'title' => 'Add link',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('menu_edit_item', 'add', NULL, 4),
- 'access arguments' => array('administer menu'),
- 'type' => MENU_LOCAL_ACTION,
- 'file' => 'menu.admin.inc',
- );
- $items['admin/structure/menu/manage/%menu/edit'] = array(
- 'title' => 'Edit menu',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('menu_edit_menu', 'edit', 4),
- 'access arguments' => array('administer menu'),
- 'type' => MENU_LOCAL_TASK,
- 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
- 'file' => 'menu.admin.inc',
- );
- $items['admin/structure/menu/manage/%menu/delete'] = array(
- 'title' => 'Delete menu',
- 'page callback' => 'menu_delete_menu_page',
- 'page arguments' => array(4),
- 'access arguments' => array('administer menu'),
- 'file' => 'menu.admin.inc',
- );
- $items['admin/structure/menu/item/%menu_link/edit'] = array(
- 'title' => 'Edit menu link',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('menu_edit_item', 'edit', 4, NULL),
- 'access arguments' => array('administer menu'),
- 'file' => 'menu.admin.inc',
- );
- $items['admin/structure/menu/item/%menu_link/reset'] = array(
- 'title' => 'Reset menu link',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('menu_reset_item_confirm', 4),
- 'access arguments' => array('administer menu'),
- 'file' => 'menu.admin.inc',
- );
- $items['admin/structure/menu/item/%menu_link/delete'] = array(
- 'title' => 'Delete menu link',
- 'page callback' => 'menu_item_delete_page',
- 'page arguments' => array(4),
- 'access arguments' => array('administer menu'),
- 'file' => 'menu.admin.inc',
- );
- return $items;
- }
- function menu_theme() {
- return array(
- 'menu_overview_form' => array(
- 'file' => 'menu.admin.inc',
- 'render element' => 'form',
- ),
- 'menu_admin_overview' => array(
- 'file' => 'menu.admin.inc',
- 'variables' => array('title' => NULL, 'name' => NULL, 'description' => NULL),
- ),
- );
- }
- 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();
- $base_link['router_path'] = 'admin/structure/menu/manage/%';
- $base_link['module'] = 'menu';
- $result = db_query("SELECT * FROM {menu_custom}", array(), array('fetch' => PDO::FETCH_ASSOC));
- foreach ($result as $menu) {
-
- $link = $base_link;
- $link['mlid'] = 0;
- $link['link_title'] = $menu['title'];
- $link['link_path'] = 'admin/structure/menu/manage/' . $menu['menu_name'];
- $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) {
- menu_link_save($link);
- }
- }
- menu_cache_clear_all();
- }
- function menu_overview_title($menu) {
- return $menu['title'];
- }
- function menu_load($menu_name) {
- $all_menus = menu_load_all();
- return isset($all_menus[$menu_name]) ? $all_menus[$menu_name] : FALSE;
- }
- function menu_load_all() {
- $custom_menus = &drupal_static(__FUNCTION__);
- if (!isset($custom_menus)) {
- if ($cached = cache_get('menu_custom', 'cache_menu')) {
- $custom_menus = $cached->data;
- }
- else {
- $custom_menus = db_query('SELECT * FROM {menu_custom}')->fetchAllAssoc('menu_name', PDO::FETCH_ASSOC);
- cache_set('menu_custom', $custom_menus, 'cache_menu');
- }
- }
- return $custom_menus;
- }
- function menu_save($menu) {
- $status = db_merge('menu_custom')
- ->key(array('menu_name' => $menu['menu_name']))
- ->fields(array(
- 'title' => $menu['title'],
- 'description' => $menu['description'],
- ))
- ->execute();
- menu_cache_clear_all();
- switch ($status) {
- case SAVED_NEW:
-
-
-
- $active_menus = variable_get('menu_default_active_menus', array_keys(menu_get_menus()));
- if (!in_array($menu['menu_name'], $active_menus)) {
- $active_menus[] = $menu['menu_name'];
- variable_set('menu_default_active_menus', $active_menus);
- }
- module_invoke_all('menu_insert', $menu);
- break;
- case SAVED_UPDATED:
- module_invoke_all('menu_update', $menu);
- break;
- }
- }
- function menu_delete($menu) {
-
- menu_delete_links($menu['menu_name']);
-
- $active_menus = variable_get('menu_default_active_menus', array_keys(menu_get_menus()));
- foreach ($active_menus as $i => $menu_name) {
- if ($menu['menu_name'] == $menu_name) {
- unset($active_menus[$i]);
- variable_set('menu_default_active_menus', $active_menus);
- }
- }
-
- db_delete('menu_custom')
- ->condition('menu_name', $menu['menu_name'])
- ->execute();
- menu_cache_clear_all();
- module_invoke_all('menu_delete', $menu);
- }
- function menu_parent_options($menus, $item, $type = '') {
-
-
-
-
- if (variable_get('menu_override_parent_selector', FALSE)) {
- return array();
- }
- $available_menus = array();
- if (!is_array($item)) {
-
-
- $type = $item;
- $item = array('mlid' => 0);
- }
- if (empty($type)) {
-
- $available_menus = $menus;
- }
- else {
-
- $type_menus = variable_get('menu_options_' . $type, array('main-menu' => 'main-menu'));
- foreach ($type_menus as $menu) {
- $available_menus[$menu] = $menu;
- }
- }
- return _menu_get_options($menus, $available_menus, $item);
- }
- 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);
- }
- function _menu_get_options($menus, $available_menus, $item) {
-
- if (isset($item['parent_depth_limit'])) {
- $limit = $item['parent_depth_limit'];
- }
- else {
- $limit = _menu_parent_depth_limit($item);
- }
- $options = array();
- foreach ($menus as $menu_name => $title) {
- 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;
- }
- function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) {
- foreach ($tree as $data) {
- if ($data['link']['depth'] > $depth_limit) {
-
- 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']) {
- _menu_parents_recurse($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit);
- }
- }
- }
- }
- function menu_reset_item($link) {
-
-
-
-
-
- $menu = menu_get_router();
- $router_item = $menu[$link['router_path']];
- $new_link = _menu_link_build($router_item);
-
- foreach (array('mlid', 'has_children') as $key) {
- $new_link[$key] = $link[$key];
- }
- menu_link_save($new_link);
- return $new_link;
- }
- function menu_block_info() {
- $menus = menu_get_menus(FALSE);
- $blocks = array();
- foreach ($menus as $name => $title) {
- $blocks[$name]['info'] = check_plain($title);
-
-
- $blocks[$name]['cache'] = DRUPAL_NO_CACHE;
- }
- return $blocks;
- }
- function menu_block_view($delta = '') {
- $menus = menu_get_menus(FALSE);
- $data['subject'] = check_plain($menus[$delta]);
- $data['content'] = menu_tree($delta);
-
- if (!empty($data['content'])) {
- $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($delta));
- }
- return $data;
- }
- function menu_block_view_alter(&$data, $block) {
-
- if ($block->module == 'system' && !empty($data['content'])) {
- $system_menus = menu_list_system_menus();
- if (isset($system_menus[$block->delta])) {
- $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($block->delta));
- }
- }
- }
- function menu_node_insert($node) {
- menu_node_save($node);
- }
- function menu_node_update($node) {
- menu_node_save($node);
- }
- function menu_node_save($node) {
- if (isset($node->menu)) {
- $link = &$node->menu;
- if (empty($link['enabled'])) {
- if (!empty($link['mlid'])) {
- menu_link_delete($link['mlid']);
- }
- }
- elseif (trim($link['link_title'])) {
- $link['link_title'] = trim($link['link_title']);
- $link['link_path'] = "node/$node->nid";
- if (trim($link['description'])) {
- $link['options']['attributes']['title'] = trim($link['description']);
- }
- else {
-
-
- unset($link['options']['attributes']['title']);
- }
- if (!menu_link_save($link)) {
- drupal_set_message(t('There was an error saving the menu link.'), 'error');
- }
- }
- }
- }
- function menu_node_delete($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));
- foreach ($result as $m) {
- menu_link_delete($m['mlid']);
- }
- }
- function menu_node_prepare($node) {
- if (empty($node->menu)) {
-
- $menu_name = strtok(variable_get('menu_parent_' . $node->type, 'main-menu:0'), ':');
- $item = array();
- if (isset($node->nid)) {
- $mlid = FALSE;
-
- $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,
- ':menu_name' => $menu_name,
- ))->fetchField();
- }
-
- 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),
- ))->fetchField();
- }
- if ($mlid) {
- $item = menu_link_load($mlid);
- }
- }
-
- $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,
- );
- }
-
- if (!isset($node->menu['parent_depth_limit'])) {
- $node->menu['parent_depth_limit'] = _menu_parent_depth_limit($node->menu);
- }
- }
- function _menu_parent_depth_limit($item) {
- return MENU_MAX_DEPTH - 1 - (($item['mlid'] && $item['has_children']) ? menu_link_children_relative_depth($item) : 0);
- }
- function menu_form_node_form_alter(&$form, $form_state) {
-
-
- $link = $form['#node']->menu;
- $type = $form['#node']->type;
-
-
-
- $options = menu_parent_options(menu_get_menus(), $link['mlid'] ? $link : $type, $type);
-
- 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),
- ),
- ),
- );
-
- 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]);
- }
- $form['menu']['link']['link_title'] = array(
- '#type' => 'textfield',
- '#title' => t('Menu link title'),
- '#maxlength' => 255,
- '#default_value' => $link['link_title'],
- );
- $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.'),
- );
- $default = ($link['mlid'] ? $link['menu_name'] . ':' . $link['plid'] : variable_get('menu_parent_' . $type, 'main-menu:0'));
-
-
-
-
- if (!isset($options[$default])) {
- $array = array_keys($options);
- $default = reset($array);
- }
- $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.'),
- );
- }
- function menu_node_submit($node, $form, $form_state) {
-
-
- if (!empty($form_state['values']['menu']['parent'])) {
- list($node->menu['menu_name'], $node->menu['plid']) = explode(':', $form_state['values']['menu']['parent']);
- }
- }
- 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'),
- '#default_value' => variable_get('menu_options_' . $type->type, array('main-menu')),
- '#options' => $menu_options,
- '#description' => t('The menus available to place links in for this content type.'),
- );
-
-
-
-
- $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')),
- );
-
-
- drupal_add_js(
- '(function ($) { Drupal.menu_update_parent_list(); })(jQuery);',
- array('scope' => 'footer', 'type' => 'inline')
- );
- }
- function menu_get_menus($all = TRUE) {
- 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);
- }
- return $custom_menus;
- }
|