How to get the clicked menu item's $mlid in Drupal?
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?
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.
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.
I advice you to use: menu_get_active_trail()
You'll get the current $mlid
.
It's for Drupal 7.
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();
Aren't you really looking for the Menu Block module?
精彩评论