A drupal 6.x question,

What I need to do is incredibly simple:
Get the $mid of the current/active menu item.
I've searched for hours but cannot find anything.

Guys, is there a way?

Comments

francort’s picture

I haven't seen any mid field on any menu table
what do you mean with mid?

jaypan’s picture

What don't you explain why you want to do this, and maybe we can give you an alternate solution.

Contact me to contract me for D7 -> D10/11 migrations.

antoine.lucas’s picture

menu_get_active_trail() do the job for you :

$trail=menu_get_active_trail();
$mlid=$trail[1]['mlid']

:)

welly’s picture

mlid doesn't appear in the array returned by menu_get_active_trail.

otx’s picture

Are you sure about that? I just checked the output of menu_get_active_trail() in D7 and it does contain both mlid and plid. So I believe this is the right way to go. antoine.lucas, your suggestion has been very helpful. Thank you!

ti2m’s picture

I love Drupal, but I really really hate the menu system, its a pain to work with. Maybe there is a nicer way to do this but I don't just need the mlid, but the translated title of the menu (yeah, thats fun). Here is what I came up with:

  $item = menu_get_item();
//copied from menu_link_load
  $item = db_fetch_array(db_query("SELECT m.*, ml.* FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path WHERE ml.link_path = '%s'",$item['href']));
  _menu_link_translate($item);
print $item['title'];

Basically you could change that to

  $item = menu_get_item();
  $mlid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s'",$item['href']));
  print $mlid;

to get the mlid.

I'm not sure though if link_path = $item['href'] will always work, like on view paths an stuff, but if the menu item just links to a normal page it should do the trick. Let me know if you have anything better....

otx’s picture

I hate the Drupal's menu system too. I'm trying to do this very similar way as you do, but I don't get that menu_get_item() thing. I thought it's enough to use $_GET['q'] to get the current node's path. But there's one problem with this method: I believe it won't work as expected in the case one node appears in your menu multiple times. This is the case of site I'm currently developing and I didn't find the workaround yet.

ti2m’s picture

The problem I see with $_GET['q'] is that the link_path column in the menu_link table contains raw paths like node/3. $_GET['q'] could be an url alias and therefore you wouldn't find a match. I used menu_get_item() for the translation of the url alias to the raw url.
I'm not sure what you want to do, but if you have multiple items the query should find the entries and you could iterate over the mids, load the full entires and match those against additional conditions. That way you should be able to find the entry you want.

jaypan’s picture

This gives the unaliased path:
$path = drupal_get_normal_path($_GET['q']);

Contact me to contract me for D7 -> D10/11 migrations.