如何在Drupal中获取单击的菜单项的$mlid?

发布于 2024-08-20 03:19:04 字数 262 浏览 1 评论 0原文

我正在尝试构建一个基于 $mlid 的子菜单。我找到了一个似乎可以完成这项工作的函数,但我似乎无法弄清楚如何获取刚刚单击的菜单项的 $mlid

我在 SO (http://drupal.org/node/249257< /a>),但我只能找到手动设置 $mlid 的示例。有什么建议吗?

I'm trying to build a submenu based on $mlid. I've found a function that appears to do the job, but I just can't seem to figure out how to get the $mlid of the menu item that was just clicked.

I found the function in a link on similar post here on SO (http://drupal.org/node/249257), but I can only find examples where the $mlid is set manually. Any suggestions?

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

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

发布评论

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

评论(5

顾北清歌寒 2024-08-27 03:19:04

您可以使用 menu_get_item() 函数获取有关当前页面的信息作为菜单项,然后查询数据库以获取 mlid。

$item = menu_get_item(); //Gets menu_router information for current page
$mlid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path='%s'", $item['path']));

注意 - 这适用于 Drupal 6。

You can use the menu_get_item() function to get information about the current page as a menu item, then make a query to the database to get the mlid.

$item = menu_get_item(); //Gets menu_router information for current page
$mlid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path='%s'", $item['path']));

Note - this applies to Drupal 6.

提赋 2024-08-27 03:19:04

emmychan 解决方案背后的想法很棒,但包含错误。所以我为 Drupal 7 的数据库 api 重写了它:

$mlid = db_select('menu_links' , 'ml')
  ->condition('ml.link_path' , $_GET['q'])
  ->fields('ml' , array('mlid'))
  ->execute()
  ->fetchField();

编辑:为了使代码片段更加通用,就像 Ambidex 建议我更新代码一样,它使用 $_GET['q'] 来尝试获取 MLID当前页面。

The idea behind emmychan's solution is great, but contains errors. So I rewrote it for Drupal 7's database api:

$mlid = db_select('menu_links' , 'ml')
  ->condition('ml.link_path' , $_GET['q'])
  ->fields('ml' , array('mlid'))
  ->execute()
  ->fetchField();

EDIT: To make the snippet more versatile like Ambidex suggests I updated the code so it uses $_GET['q'] to try to get the MLID of the current page.

假装爱人 2024-08-27 03:19:04

我建议您使用: menu_get_active_trail()< /code>

您将获得当前的$mlid

适用于 Drupal 7。

I advice you to use: menu_get_active_trail()

You'll get the current $mlid.

It's for Drupal 7.

羅雙樹 2024-08-27 03:19:04

这是基于 Drupal 7 的 Imeurs 代码。如果您不知道 $nid,或者将在面板页面中使用它,您可以使用以下代码获取当前项目:

$item =  menu_get_item();    
$mlid = db_select('menu_links' , 'ml')
  ->condition('ml.link_path' , $item['href'])
  ->fields('ml' , array('mlid'))
  ->execute()
  ->fetchField();

This is based off of Imeurs code for Drupal 7. If you don't know the $nid, or will be using this with panels pages, you can get the current item using the following code:

$item =  menu_get_item();    
$mlid = db_select('menu_links' , 'ml')
  ->condition('ml.link_path' , $item['href'])
  ->fields('ml' , array('mlid'))
  ->execute()
  ->fetchField();
吲‖鸣 2024-08-27 03:19:04

您真的不是在寻找 菜单块 模块吗?

Aren't you really looking for the Menu Block module?

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