简体   繁体   English

Drupal 6动态菜单项

[英]Drupal 6 dynamic menu item

I need to create a menu item, which changes its title and link if a user has a certain condition, or not. 我需要创建一个菜单项,如果用户有特定条件,则可以更改其标题和链接。 Drupal caches all the menues, so i can't really think of a way to do that. Drupal会缓存所有菜单,所以我真的想不出一种方法。

For example, user has a node attached to his profile, menu item is "Create blabla" (link node/add/blabla) User doesn't have the node, menu item is "Create notblablabla" (link node/add/notblabla) 例如,用户的个人资料上附加了一个节点,菜单项为“创建blabla”(链接节点/添加/ blabla)用户没有节点,菜单项为“创建notblablabla”(链接节点/添加/ notblabla)

The best and easiest way is to use hook_menu with param title_callback . 最好和最简单的方法是将hook_menu与参数title_callback一起使用。

See thedrupalblog.com and drupal.org . 请参阅thedrupalblog.comdrupal.org

Drupal does not allow for dynamic menu items, but it can hide certain menu items if the user is not allowed to go there. Drupal不允许动态菜单项,但是如果不允许用户去那里,它可以隐藏某些菜单项。 Referring to your example, if you create both the links and use the permission system to restrict the creation of those node types to certain roles, Drupal will only show the menu items if the user has the required role. 以您的示例为例,如果您同时创建链接和使用权限系统将这些节点类型的创建限制为某些角色,则Drupal仅在用户具有所需角色时才显示菜单项。 Maybe that helps in your situation. 也许对您的情况有所帮助。

Other options are: 其他选项是:

  • write a simple module that shows a single link for all users and redirects to the appropriate page when clicked 编写一个简单的模块,为所有用户显示一个链接,并在单击时重定向到相应的页面
  • create a custom block which displays the correct link based on the current user (make sure the block is not cached) 创建一个基于当前用户显示正确链接的自定义块(确保未缓存该块)
  • use javascript like googletorp suggest (although I wouldn't recommend it for the reasons he mentions) 使用类似googletorp建议的JavaScript(尽管由于他提到的原因我不推荐使用)
  • A simple way to do this, would be to use JavaScript. 一种简单的方法是使用JavaScript。 You can alter HTML without much efford using jQuery. 您可以使用jQuery更改HTML,而无需花很多钱。 This will require that your users has JS enabled, so it's not a perfect solution. 这将要求您的用户启用JS,因此这不是一个完美的解决方案。

  • Another option would be to have a single menu item, that linked to a url you created in a module. 另一个选择是只有一个菜单项,该菜单项链接到您在模块中创建的URL。 He you could do the condition check a redirect the user to whichever url he should be redirected to. 您可以执行条件检查,将用户重定向到他应该重定向到的任何URL。 The only problem with this method would be that the changing the title of the menu item. 这种方法的唯一问题是更改菜单项的标题。 But you might be able to give a fitting description for both cases. 但是您可能可以针对这两种情况给出合适的描述。 You could also use JS to change the name of the link. 您也可以使用JS更改链接的名称。 That way you would keep the functionality intact without JS but improve the UI for users with it enabled. 这样,您可以在没有JS的情况下保持功能完整,但可以为启用了JS的用户改进UI。

I would probably create two menu items, and use the theme system to hide one or the other based on the condition. 我可能会创建两个菜单项,然后使用主题系统根据条件隐藏一个或另一个。

It's been a while, but I'd look at: 已经有一段时间了,但我会看看:

Since this menu item seems to be predicated on user profile information, I would suggest writing a simple module that implements hook_menu_alter() to alter the menu based on your condition. 由于此菜单项似乎取决于用户个人资料信息,因此我建议编写一个简单的模块,该模块实现hook_menu_alter()来根据您的情况更改菜单。 You can then call menu_cache_clear() inside hook_user() to renew the menu cache when the user profile changes. 然后,您可以在hook_user()调用menu_cache_clear()以在用户配置文件更改时续订菜单缓存。

Have you tried this (inserting your other conditions into the 'if' statement as well of course): 您是否尝试过此操作(当然也将其他条件插入到“ if”语句中):


function hook_translated_menu_link_alter(&$item, $map) {
  if ($item['href'] == 'node/add/blabla') {
    $item['href'] = 'node/add/notblabla';
  }
}

You'll probably also have to do this to mark the link as alterable: 您可能还必须这样做以将链接标记为可更改:


function hook_menu_link_alter(&$item, $menu) {
  if ($item['link_path'] == 'node/add/blabla') {
    $item['options']['alter'] = TRUE;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM