Drupal 6 动态菜单项

发布于 2024-08-19 19:27:37 字数 192 浏览 12 评论 0原文

我需要创建一个菜单项,如果用户有某种条件,该菜单项会更改其标题和链接。 Drupal 缓存了所有菜单,所以我实在想不出一种方法来做到这一点。

例如,用户有一个附加到他的个人资料的节点,菜单项是“创建 blabla”(链接节点/add/blabla) 用户没有节点,菜单项是“Create notblablabla”(链接节点/add/notblabla)

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.

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

嗫嚅 2024-08-26 19:27:37

最好、最简单的方法是将 hook_menu 与参数 title_callback 一起使用。

请参阅 thedrupalblog.comdrupal.org

The best and easiest way is to use hook_menu with param title_callback.

See thedrupalblog.com and drupal.org.

べ映画 2024-08-26 19:27:37

Drupal 不允许动态菜单项,但如果不允许用户进入某些菜单项,它可以隐藏某些菜单项。参考您的示例,如果您创建这两个链接并使用权限系统将这些节点类型的创建限制为某些角色,则 Drupal 将仅在用户具有所需角色时显示菜单项。也许这对你的情况有帮助。

其他选项包括:

  • 编写一个简单的模块,为所有用户显示单个链接,并在单击时重定向到相应的页面
  • 创建一个自定义块,根据当前用户显示正确的链接(确保该块未缓存)
  • 使用 javascript,例如googletorp 建议(尽管我不会因为他提到的原因推荐它)

Drupal does not allow for dynamic menu items, but it can hide certain menu items if the user is not allowed to go there. 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. 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)
无人问我粥可暖 2024-08-26 19:27:37
  • 实现此目的的一个简单方法是使用 JavaScript。您可以使用 jQuery 轻松更改 HTML。这将要求您的用户启用 JS,因此这不是一个完美的解决方案。

  • 另一个选择是有一个菜单项,链接到您在模块中创建的 URL。您可以进行条件检查,将用户重定向到他应该重定向到的任何网址。此方法的唯一问题是更改菜单项的标题。但您也许能够对这两种情况给出合适的描述。您还可以使用 JS 更改链接的名称。这样,您就可以在没有 JS 的情况下保持功能完整,但可以为启用该功能的用户改进 UI。

  • A simple way to do this, would be to use JavaScript. You can alter HTML without much efford using jQuery. This will require that your users has JS enabled, so it's not a perfect solution.

  • Another option would be to have a single menu item, that linked to a url you created in a module. He you could do the condition check a redirect the user to whichever url he should be redirected to. 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. That way you would keep the functionality intact without JS but improve the UI for users with it enabled.

烏雲後面有陽光 2024-08-26 19:27:37

我可能会创建两个菜单项,并使用主题系统根据条件隐藏其中一个或另一个。

已经有一段时间了,但我会看看:

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:

我很OK 2024-08-26 19:27:37

由于此菜单项似乎基于用户配置文件信息,因此我建议编写一个简单的模块来实现 hook_menu_alter() 来根据您的情况更改菜单。然后,您可以在 hook_user() 内调用 menu_cache_clear() 以在用户配置文件更改时更新菜单缓存。

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. You can then call menu_cache_clear() inside hook_user() to renew the menu cache when the user profile changes.

沧桑㈠ 2024-08-26 19:27:37

您是否尝试过此操作(当然,也将其他条件插入到“if”语句中):


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

您可能还需要这样做才能将链接标记为可更改:


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

Have you tried this (inserting your other conditions into the 'if' statement as well of course):


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;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文