Drupal以编程方式将项目添加到菜单中

时间:2010-12-16 12:04:48

标签: drupal drupal-6 drupal-modules

我希望有条件地将一个项目添加到菜单中。我有一个自定义模块和一个名为“链接”的菜单。如何将项目添加到模块代码中的菜单中?

5 个答案:

答案 0 :(得分:3)

您需要在模块中实施hook_menu。例如:

<?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,部分使其成为可选部分,因此可以由最终用户启用 - 这是“有条件”的意思吗?如果没有,请解释您正在寻找什么样的“有条件”。

答案 1 :(得分:2)

或者您可以使用'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 :(得分:2)

您可以根据条件(访问回调)动态显示或隐藏菜单项。

以下是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;
  }
}

答案 3 :(得分:1)

菜单系统已缓存,因此您无法根据用户,页面查看,自定义逻辑等随意添加或删除菜单项。即使不清除菜单缓存,您也无法执行此操作。导致严重的性能损失。

要创建此效果,您可以创建一些自定义逻辑来定义菜单项上的访问控制。由于Drupal隐藏了用户无权访问的菜单项,因此在某些情况下您可以拒绝隐藏菜单项的权限。这是一个有点hackish的解决方案。

我更喜欢的另一个解决方案是使用js或css来隐藏或显示菜单。您可以在主体上动态添加/删除类,以确定是否应显示菜单项。但是,如果你需要这些菜单中的几种,这很快就会变得无法控制。

答案 4 :(得分:1)

使用menu_link_save()功能

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.