Drupal programmatically adds an item to the menu

I want to conditionally add an item to the menu. I have a custom module and a menu called links. How to add an item to a menu in my modular code?

+3
source share
5 answers

The menu system is cached, so you cannot add or remove menu items as you wish, based on the user, page view, user logic, etc. That is, you cannot do this without clearing the menu cache, which cause a serious defeat.

What you can do to create this effect is to create some custom logic to define an access control in a menu item. Since Drupal hides menu items that users do not have access to, under certain circumstances you can prevent the menu item from being hidden. This is a bit of a hacker solution.

Another solution I would prefer would be to use js or css to hide or show the menu. You can dynamically add / remove a class in the body to determine whether a menu item should be displayed or not. However, this would quickly become unmanageable if you needed several of these menu items.

+1
source

You need to implement hook_menu in your module. Example:

<?php
function mymodule_menu() {
  $items['mymodule/links'] = array(
    'title' => 'Links', 
    'page callback' => 'mymodule_links_page', 
    'access arguments' => array('access content'), 
    'type' => MENU_SUGGESTED_ITEM,
  );
  return $items;
}
?>

'type' => MENU_SUGGESTED_ITEM, , - , ""? , , "" .

+3

'type' => MENU_NORMAL_ITEM,, , . , , . . http://api.drupal.org/api/drupal/includes--menu.inc/group/menu/7.

, , , , , , " ". mymodule.install, :

<?php  
function mymodule_install() {  
  $menu = array(  
    'menu_name' => 'links',  
    'title' => 'My Custom Links',  
    'description' => 'Descriptive text.',  
  );  
  menu_save($menu);  
}  
?>

, , . , , !

+2

You can dynamically display or hide a menu item based on a condition (access callback).

Here is an example from https://drupal.org/project/examples :

<?php
function mymodule_menu() {
  $items = array();

  $items['my-menu-item'] = array(
    'title' => 'My Menu',
    'description' => 'My description',
    'page callback' => 'my_page_link_callback_function_name',
    'access callback' => 'can_the_user_see_this_item',
    'expanded' => TRUE,
    'weight' => -100,
    'menu_name' => 'primary-links',
  ); 

  return $items;
}

// Here we determine if the user can or can not see the item.
function can_the_user_see_this_item(){
  if (MY_CONDITION){
    return TRUE;
  }
  else {
    return FALSE;
  }
}
+2
source

Use the menu_link_save () function

Saves a menu link.

After calling this function, rebuild the menu cache using menu_cache_clear_all().

Parameters

$item: An associative array representing a menu link item, with elements:

link_path: (required) The path of the menu item, which should be normalized first by calling drupal_get_normal_path() on it.
link_title: (required) Title to appear in menu for the link.
menu_name: (optional) The machine name of the menu for the link. Defaults to 'navigation'.
weight: (optional) Integer to determine position in menu. Default is 0.
expanded: (optional) Boolean that determines if the item is expanded.
options: (optional) An array of options, see l() for more.
mlid: (optional) Menu link identifier, the primary integer key for each menu link. Can be set to an existing value, or to 0 or NULL to insert a new link.
plid: (optional) The mlid of the parent.
router_path: (optional) The path of the relevant router item.
$existing_item: Optional, the current record from the {menu_links} table as an array.

$parent_candidates: Optional array of menu links keyed by mlid. Used by _menu_navigation_links_rebuild() only.

Return value

The mlid of the saved menu link, or FALSE if the menu link could not be saved.
+1
source

Source: https://habr.com/ru/post/1780664/


All Articles