Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Drupal以编程方式向菜单添加项_Drupal_Drupal 6_Drupal Modules - Fatal编程技术网

Drupal以编程方式向菜单添加项

Drupal以编程方式向菜单添加项,drupal,drupal-6,drupal-modules,Drupal,Drupal 6,Drupal Modules,我希望有条件地在菜单中添加一个项目。我有一个自定义模块和一个名为“链接”的菜单。如何将项目添加到模块代码中的菜单中?您需要在模块中实现。例如: <?php function mymodule_menu() { $items['mymodule/links'] = array( 'title' => 'Links', 'page callback' => 'mymodule_links_page', 'access arguments' =>

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

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

<?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'=>菜单\u建议\u项,
部分将其设置为可选项,因此最终用户可以启用它-这就是您所说的“有条件”吗?如果没有,请解释您正在寻找的是哪种“有条件的”。

菜单系统是缓存的,因此您不能根据用户、查看的页面、自定义逻辑等随意添加或删除菜单项。也就是说,如果不清除会导致严重性能下降的菜单缓存,您就不能这样做

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


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

或者您可以使用
'type'=>菜单项,因为默认情况下它是启用的,但可以随时禁用。这当然取决于你的喜好。有关更多参考,请参阅

在自定义菜单中使用模块定义的菜单项时,要知道的另一件好事可能是如何以编程方式创建要使用的菜单,以便“开箱即用”地创建所有内容。只需添加一个mymodule.install-file,其中包含以下代码:

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


如果您有卸载功能,不要忘记不仅要停用模块,还要卸载它。重新启动模块,刷新缓存,菜单项应该在那里

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

以下是一个例子:

使用函数


为什么你认为使用Access系统隐藏菜单项是不礼貌的?Drupal core也可以通过登录/注销链接做到这一点,我看不出有什么问题(除非你隐藏了用户通过另一个链接确实可以访问的内容)。@marcvangend:这取决于你如何使用它,如果你在某些其他页面上编写自定义访问函数来拒绝访问某个页面,我会称之为hackish。这是有道理的,你不能登录时,你没有登录,但不是你不能访问一个页面时,你在另一个页面。这个检查本身没有任何意义,只是用来隐藏一个菜单项。难道不能根据html标记中已经存在的特定css简单地使用(是的,尽管再次使用hack-ish,但使用了很多)css“隐藏”吗?一点.js也可以。有时(甚至我)忘记了我们有太多的媒介来操纵DOM:)。
<?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;
  }
}
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.