' . t('About') . ''; $output .= '

' . 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 Blocks administration page. You can view and manage menus on the Menus administration page. For more information, see the online handbook entry for the Menu module.', array('@blocks' => url('admin/structure/block'), '@menus' => url('admin/structure/menu'), '@menu' => 'http://drupal.org/documentation/modules/menu/')) . '

'; $output .= '

' . t('Uses') . '

'; $output .= '
'; $output .= '
' . t('Managing menus') . '
'; $output .= '
' . t('Users with the Administer menus and menu items permission can add, edit and delete custom menus on the Menus administration page. 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 list links, 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'))) . '
'; $output .= '
' . t('Displaying menus') . '
'; $output .= '
' . t('After you have created a menu, you must enable and position the associated block on the Blocks administration page.', array('@blocks' => url('admin/structure/block'))) . '
'; $output .= '
'; return $output; case 'admin/structure/menu/add': return '

' . t('You can enable the newly-created block for this menu on the Blocks administration page.', array('@blocks' => url('admin/structure/block'))) . '

'; } if ($path == 'admin/structure/menu' && module_exists('block')) { return '

' . t('Each menu has a corresponding block that is managed on the Blocks administration page.', array('@blocks' => url('admin/structure/block'))) . '

'; } } /** * Implements hook_permission(). */ function menu_permission() { return array( 'administer menu' => array( 'title' => t('Administer menus and menu items'), ), ); } /** * Implements hook_menu(). */ 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(TRUE), ); $items['admin/structure/menu/list'] = array( 'title' => 'List menus', 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items['admin/structure/menu/add'] = array( 'title' => 'Add menu', 'page callback' => 'menu_menu_add', 'access arguments' => array('administer menu'), 'type' => MENU_LOCAL_ACTION, 'file' => 'menu.admin.inc', ); $items['admin/structure/menu/settings'] = array( 'title' => 'Settings', 'route_name' => 'menu_settings', 'access arguments' => array('administer menu'), 'type' => MENU_LOCAL_TASK, 'weight' => 100, ); $items['admin/structure/menu/manage/%menu'] = array( 'title' => 'Edit menu', 'page callback' => 'menu_menu_edit', 'page arguments' => array(4), 'title callback' => 'entity_page_label', 'title arguments' => array(4), 'access arguments' => array('administer menu'), 'file' => 'menu.admin.inc', ); // Not officially a local action, but displayed as such in // menu_overview_form(). $items['admin/structure/menu/manage/%menu/add'] = array( 'title' => 'Add menu link', 'page callback' => 'menu_link_add', 'page arguments' => array(4), 'access arguments' => array('administer menu'), 'file' => 'menu.admin.inc', ); $items['admin/structure/menu/manage/%menu/edit'] = array( 'title' => 'Edit menu', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE, ); $items['admin/structure/menu/manage/%menu/delete'] = array( 'title' => 'Delete menu', 'route_name' => 'menu_delete_menu', ); $items['admin/structure/menu/item/%menu_link/edit'] = array( 'title' => 'Edit menu link', 'page callback' => 'entity_get_form', 'page arguments' => array(4), 'access arguments' => array('administer menu'), ); $items['admin/structure/menu/item/%menu_link/reset'] = array( 'title' => 'Reset menu link', 'route_name' => 'menu_link_reset', ); $items['admin/structure/menu/item/%menu_link/delete'] = array( 'title' => 'Delete menu link', 'route_name' => 'menu_link_delete', ); return $items; } /** * Implements hook_entity_info(). */ function menu_entity_info(&$entity_info) { $entity_info['menu']['controllers']['list'] = 'Drupal\menu\MenuListController'; $entity_info['menu']['controllers']['access'] = 'Drupal\menu\MenuAccessController'; $entity_info['menu']['uri_callback'] = 'menu_uri'; $entity_info['menu']['controllers']['form'] = array( 'default' => 'Drupal\menu\MenuFormController', 'delete' => 'Drupal\menu\Form\MenuDeleteForm', ); $entity_info['menu_link']['controllers']['form']['delete'] = 'Drupal\menu\Form\MenuLinkDeleteForm'; $entity_info['menu_link']['controllers']['form']['reset'] = 'Drupal\menu\Form\MenuLinkResetForm'; } /** * Implements hook_entity_bundle_info(). */ function menu_entity_bundle_info() { $bundles = array(); $config_names = config_get_storage_names_with_prefix('menu.menu.'); foreach ($config_names as $config_name) { $config = config($config_name); $bundles['menu_link'][$config->get('id')] = array( 'label' => $config->get('label'), ); } return $bundles; } /** * Entity URI callback. * * @param \Drupal\system\Plugin\Core\Entity\Menu $menu * A Menu entity. */ function menu_uri(Menu $menu) { return array( 'path' => 'admin/structure/menu/manage/' . $menu->id(), ); } /** * Implements hook_theme(). */ function menu_theme() { return array( 'menu_overview_form' => array( 'file' => 'menu.admin.inc', 'render element' => 'form', ), ); } /** * Implements hook_enable(). * * Add a link for each custom menu. */ function menu_enable() { Drupal::service('router.builder')->rebuild(); menu_router_rebuild(); $system_link = entity_load_multiple_by_properties('menu_link', array('link_path' => 'admin/structure/menu', 'module' => 'system')); $system_link = reset($system_link); $base_link = entity_create('menu_link', array( 'menu_name' => $system_link->menu_name, 'router_path' => 'admin/structure/menu/manage/%', 'module' => 'menu', )); $menus = entity_load_multiple('menu'); foreach ($menus as $menu) { $link = $base_link->createDuplicate(); $link->plid = $system_link->id(); $link->link_title = $menu->label(); $link->link_path = 'admin/structure/menu/manage/' . $menu->id(); $query = Drupal::entityQuery('menu_link') ->condition('link_path', $link->link_path) ->condition('plid', $link->plid); $result = $query->execute(); if (empty($result)) { $link->save(); } } menu_cache_clear_all(); } /** * Load the data for a single custom menu. * * @param $menu_name * The unique name of a custom menu to load. * @return * Array defining the custom menu, or NULL if the menu doesn't exist. */ function menu_load($menu_name) { return entity_load('menu', $menu_name); } /** * Implements hook_menu_insert() */ function menu_menu_insert(Menu $menu) { menu_cache_clear_all(); // Invalidate the block cache to update menu-based derivatives. if (module_exists('block')) { Drupal::service('plugin.manager.block')->clearCachedDefinitions(); } // Make sure the menu is present in the active menus variable so that its // items may appear in the menu active trail. // See menu_set_active_menu_names(). $config = config('system.menu'); $active_menus = $config->get('active_menus_default') ?: array_keys(menu_get_menus()); if (!in_array($menu->id(), $active_menus)) { $active_menus[] = $menu->id(); $config ->set('active_menus_default', $active_menus) ->save(); } } /** * Implements hook_menu_update(). */ function menu_menu_update(Menu $menu) { menu_cache_clear_all(); // Invalidate the block cache to update menu-based derivatives. if (module_exists('block')) { Drupal::service('plugin.manager.block')->clearCachedDefinitions(); } } /** * Implements hook_menu_predelete(). */ function menu_menu_predelete(Menu $menu) { // Delete all links from the menu. menu_delete_links($menu->id()); // Remove menu from active menus variable. $active_menus = variable_get('menu_default_active_menus', array_keys(menu_get_menus())); foreach ($active_menus as $i => $menu_name) { if ($menu->id() == $menu_name) { unset($active_menus[$i]); variable_set('menu_default_active_menus', $active_menus); } } } /** * Implements hook_menu_delete(). */ function menu_menu_delete(Menu $menu) { menu_cache_clear_all(); // Invalidate the block cache to update menu-based derivatives. if (module_exists('block')) { Drupal::service('plugin.manager.block')->clearCachedDefinitions(); } } /** * Returns a list of menu links that are valid possible parents for the given * menu link. * * @param array $menus * An array of menu names and titles, such as from menu_get_menus(). * @param \Drupal\menu_link\Plugin\Core\Entity\MenuLink $menu_link * The menu link for which to generate a list of parents. * If $menu_link->id() == 0 then the complete tree is returned. * @param string $type * The node type for which to generate a list of parents. * If $item itself is a node type then $type is ignored. * * @return array * An array of menu link titles keyed by 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 * 'override_parent_selector' variable is entirely superfluous. */ function menu_parent_options(array $menus, MenuLink $menu_link = NULL, $type = NULL) { // 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 override_parent_selector is set to TRUE. if (config('menu.settings')->get('override_parent_selector')) { return array(); } if (!$menu_link) { $menu_link = entity_create('menu_link', array('mlid' => 0)); } $available_menus = array(); if (!$type) { // If no node type is set, use all menus given to this function. $available_menus = $menus; } else { // If a node type is set, use all available menus for this type. $type_menus = variable_get('menu_options_' . $type, array('main' => 'main')); foreach ($type_menus as $menu) { $available_menus[$menu] = $menu; } } return _menu_get_options($menus, $available_menus, $menu_link); } /** * Page callback. * Get all the available menus and menu items as a JavaScript array. */ function menu_parent_options_js() { $available_menus = array(); $menus = Drupal::request()->request->get('menus'); if (count($menus)) { foreach ($menus as $menu) { $available_menus[$menu] = $menu; } } $options = _menu_get_options(menu_get_menus(), $available_menus, array('mlid' => 0)); return new JsonResponse($options); } /** * Helper function to get the items of the given menu. */ function _menu_get_options($menus, $available_menus, $item) { // If the item has children, there is an added limit to the depth of valid parents. 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; } /** * Recursive helper function for menu_parent_options(). */ function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude, $depth_limit) { foreach ($tree as $data) { if ($data['link']['depth'] > $depth_limit) { // Don't iterate through any links on this level. break; } if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) { $title = $indent . ' ' . truncate_utf8($data['link']['title'], 30, TRUE, FALSE); if ($data['link']['hidden']) { $title .= ' (' . t('disabled') . ')'; } $options[$menu_name . ':' . $data['link']['mlid']] = $title; if ($data['below']) { _menu_parents_recurse($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit); } } } } /** * Implements hook_block_view_BASE_BLOCK_ID_alter() for 'system_menu_block'. */ function menu_block_view_system_menu_block_alter(array &$build, BlockPluginInterface $block) { // Add contextual links for system menu blocks. if (isset($build['content'])) { foreach (element_children($build['content']) as $key) { $build['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($build['content'][$key]['#original_link']['menu_name'])); } } } /** * Implements hook_node_insert(). */ function menu_node_insert(EntityInterface $node) { menu_node_save($node); } /** * Implements hook_node_update(). */ function menu_node_update(EntityInterface $node) { menu_node_save($node); } /** * Helper for hook_node_insert() and hook_node_update(). */ function menu_node_save(EntityInterface $node) { if (isset($node->menu)) { $link = &$node->menu; if (empty($link['enabled'])) { if (!$link->isNew()) { 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 { // If the description field was left empty, remove the title attribute // from the menu link. unset($link['options']['attributes']['title']); } if (!menu_link_save($link)) { drupal_set_message(t('There was an error saving the menu link.'), 'error'); } } } } /** * Implements hook_node_predelete(). */ function menu_node_predelete(EntityInterface $node) { // Delete all menu module links that point to this node. $query = Drupal::entityQuery('menu_link') ->condition('link_path', 'node/' . $node->nid) ->condition('module', 'menu'); $result = $query->execute(); if (!empty($result)) { menu_link_delete_multiple($result); } } /** * Implements hook_node_prepare_form(). */ function menu_node_prepare_form(NodeInterface $node, $form_display, $operation, array &$form_state) { if (empty($node->menu)) { // Prepare the node for the edit form so that $node->menu always exists. $menu_name = strtok(variable_get('menu_parent_' . $node->type, 'main:0'), ':'); $menu_link = FALSE; if (isset($node->nid)) { $mlid = FALSE; // Give priority to the default menu $type_menus = variable_get('menu_options_' . $node->type, array('main' => 'main')); if (in_array($menu_name, $type_menus)) { $query = Drupal::entityQuery('menu_link') ->condition('link_path', 'node/' . $node->nid) ->condition('menu_name', $menu_name) ->condition('module', 'menu') ->sort('mlid', 'ASC') ->range(0, 1); $result = $query->execute(); $mlid = (!empty($result)) ? reset($result) : FALSE; } // Check all allowed menus if a link does not exist in the default menu. if (!$mlid && !empty($type_menus)) { $query = Drupal::entityQuery('menu_link') ->condition('link_path', 'node/' . $node->nid) ->condition('menu_name', array_values($type_menus), 'IN') ->condition('module', 'menu') ->sort('mlid', 'ASC') ->range(0, 1); $result = $query->execute(); $mlid = (!empty($result)) ? reset($result) : FALSE; } if ($mlid) { $menu_link = menu_link_load($mlid); } } if (!$menu_link) { $menu_link = entity_create('menu_link', array( 'mlid' => 0, 'plid' => 0, 'menu_name' => $menu_name, )); } // Set default values. $node->menu = $menu_link; } // 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); } } /** * 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']) ? entity_get_controller('menu_link')->findChildrenRelativeDepth($item) : 0); } /** * Implements hook_form_BASE_FORM_ID_alter(). * * Adds menu item fields to the node form. * * @see menu_node_submit() */ function menu_form_node_form_alter(&$form, $form_state) { // Generate a list of possible parents (not including this link or descendants). // @todo This must be handled in a #process handler. $node = $form_state['controller']->getEntity(); $link = $node->menu; $type = $node->type; $options = menu_parent_options(menu_get_menus(), $link, $type); // If no possible parent menu items were found, there is nothing to display. if (empty($options)) { return; } $form['menu'] = array( '#type' => 'details', '#title' => t('Menu settings'), '#access' => user_access('administer menu'), '#collapsed' => !$link['link_title'], '#group' => 'advanced', '#attached' => array( 'library' => array(array('menu', 'drupal.menu')), ), '#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), ), ), ); // 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]); } $form['menu']['link']['link_title'] = array( '#type' => 'textfield', '#title' => t('Menu link title'), '#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:0')); // 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. 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')), ); // Get number of items in menu so the weight selector is sized appropriately. $delta = entity_get_controller('menu_link')->countMenuLinks($link->menu_name); if ($delta < 50) { // Old hardcoded value $delta = 50; } $form['menu']['link']['weight'] = array( '#type' => 'weight', '#title' => t('Weight'), '#delta' => $delta, '#default_value' => $link['weight'], '#description' => t('Menu links with smaller weights are displayed before links with larger weights.'), ); } /** * Implements hook_node_submit(). * * @see menu_form_node_form_alter() */ function menu_node_submit(EntityInterface $node, $form, $form_state) { $node->menu = entity_create('menu_link', $form_state['values']['menu']); // 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'])) { list($node->menu['menu_name'], $node->menu['plid']) = explode(':', $form_state['values']['menu']['parent']); } } /** * Implements hook_form_FORM_ID_alter(). * * 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_state['controller']->getEntity(); $form['menu'] = array( '#type' => 'details', '#title' => t('Menu settings'), '#collapsed' => TRUE, '#attached' => array( 'library' => array(array('menu', 'drupal.menu.admin')), ), '#group' => 'additional_settings', ); $form['menu']['menu_options'] = array( '#type' => 'checkboxes', '#title' => t('Available menus'), '#default_value' => variable_get('menu_options_' . $type->id(), array('main')), '#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. $menu_link = entity_create('menu_link', array('mlid' => 0)); $options = menu_parent_options(menu_get_menus(), $menu_link); $form['menu']['menu_parent'] = array( '#type' => 'select', '#title' => t('Default parent item'), '#default_value' => variable_get('menu_parent_' . $type->id(), 'main: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.menuUpdateParentList() to filter the list of // available default parent menu items based on the selected menus. drupal_add_js( '(function ($) { Drupal.menuUpdateParentList(); })(jQuery);', array('scope' => 'footer', 'type' => 'inline') ); } /** * 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) { if ($custom_menus = entity_load_multiple('menu')) { if (!$all) { $custom_menus = array_diff_key($custom_menus, menu_list_system_menus()); } foreach ($custom_menus as $menu_name => $menu) { $custom_menus[$menu_name] = $menu->label(); } asort($custom_menus); } return $custom_menus; } /** * Implements hook_preprocess_HOOK() for block.html.twig. */ function menu_preprocess_block(&$variables) { if ($variables['configuration']['module'] == 'menu') { $variables['attributes']['role'] = 'navigation'; } } /** * Implements hook_library_info(). */ function menu_library_info() { $libraries['drupal.menu'] = array( 'title' => 'Menu', 'version' => VERSION, 'js' => array( drupal_get_path('module', 'menu') . '/menu.js' => array(), ), 'dependencies' => array( array('system', 'jquery'), array('system', 'drupal'), array('system', 'drupal.form'), ), ); $libraries['drupal.menu.admin'] = array( 'title' => 'Menu admin', 'version' => VERSION, 'js' => array( drupal_get_path('module', 'menu') . '/menu.admin.js' => array(), ), 'dependencies' => array( array('system', 'jquery'), array('system', 'drupal'), ), ); return $libraries; }