如何在 Drupal 中仅显示已填充的菜单选项卡?

发布于 2024-10-16 20:28:21 字数 499 浏览 6 评论 0 原文

在评估邀请模块,我们意识到默认的选项卡导航并不是最用户友好的。特别是,每个模块都有一个选项卡页面,显示邀请或支持票证的各种类别(待处理、已取消等)。对于开发人员来说,预定义所有选项卡是最简单的,但从用户的角度来看,只提供包含内容的选项卡更有意义。

我假设可以运行查询来检查应为特定用户显示哪些选项卡,并使用 hook_menu_alter.然而,这是最好的方法还是会导致 Drupal 菜单缓存出现问题?有更好的办法吗?

In evaluating the Invite and Support modules for Drupal recently, we realized the default tab navigation is not the most user friendly. In particular, each module has a page of tabs that show the various categories of invitations or support tickets (pending, cancelled, etc). For developers, it's easiest to pre-define all the tabs, but from a user standpoint, it makes more sense to only be offered the tabs that contain content.

I assume it's possible to run queries to check which tabs should be displayed for a particular user and change the menus using hook_menu_alter. However, is that the best way to do it or will that cause problems with Drupal's menu caching? Is there a better way?

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

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

发布评论

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

评论(2

同尘 2024-10-23 20:28:21

jhedstrom 的答案是正确的,但我不太相信动态显示/隐藏本地任务会带来更好的用户体验,这对我来说有点令人困惑。

相反,我的建议是使用标题回调(可以使用相同的 hook_menu_alter() 添加并显示该特定选项卡内的内容数量。这就是我用于 Privatemsg 的示例,以显示未读消息的数量。

有关示例,请参阅 privatemsg_title_callback() 和 < a href="http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_menu/7" rel="nofollow">hook_menu 了解更多信息关于一般标题回调。

The answer by jhedstrom is correct, but I'm not that convinced that dynamically showing/hiding local tasks results in better UX, that sounds kinda confusing to me.

Instead, my suggestion would be to use a title callback (which can be added with the same hook_menu_alter() and show the number of things inside that specific tab. That is what I for example use for Privatemsg to show the number of unread messages.

See privatemsg_title_callback() for an example and hook_menu for more information about title callbacks in general.

情话已封尘 2024-10-23 20:28:21

如果您想以动态方式有选择地删除选项卡(例如,一个节点获得选项卡,而另一个节点则没有),您将无法使用 hook_menu_alter() 因为这仅在构建菜单缓存时有效。但是,您可以通过覆盖菜单访问回调来完成此操作。如果对选项卡的访问被拒绝,则不会显示该选项卡。

例如,要覆盖节点选项卡,您可以执行以下操作:

function mymodule_menu_alter(&$items) {
  $item['node/%node/foo']['access callback'] = 'mymodule_override_access';
}

function mymodule_override_access($node) {
  // Perform queries, logic, etc to determine if content exists at node/nid/foo.
  // Return false if there is no content, otherwise fall through to the original
  // access callback function.
}

If you want to selectively remove tabs in a dynamic way (eg, one node gets a tab, while another does not), you won't be able to use hook_menu_alter() because that only has an effect when the menu cache is being built. However, you can do this by overriding the menu access callback. If access to a tab is denied, it won't be displayed.

For example, to override a node tab, you might do something like this:

function mymodule_menu_alter(&$items) {
  $item['node/%node/foo']['access callback'] = 'mymodule_override_access';
}

function mymodule_override_access($node) {
  // Perform queries, logic, etc to determine if content exists at node/nid/foo.
  // Return false if there is no content, otherwise fall through to the original
  // access callback function.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文