4

I currently have nodes setup on my site, and each node belongs to a particular menu (not primary or secondary prebuilt menues).

How can i find out which menu a node belongs to?

4 Answers 4

10

Maybe this is what you mean:

$trail        = menu_get_active_trail();
$lastInTrail  = end($trail);
$menu_name    = $lastInTrail['menu_name'];

menu_get_active_trail() returns a breadcrumbs like array, the last breadcrumb represents the current node.

Cheers, Laurens Meurs, Rotterdam

0
3

I'm a noob, so don't bash me if what I'm going to write is worthless babbling.

I think you can't do that directly, unless there's some smart module out there that would do all the nasty SQL queries necessary to check this.

Node info is stored in the SQL table "node", and is identified merely by NID (node ID, which is the node number that appears after /?q=node/ in the address). Their aliases, if any, are stored in "url_alias" table, where you can find columns "src" and "dst", identifying the original and the aliased path (for instance, src='node/123', dst='my/url/alias'). The menu links can be found in the table "menu_links", where you can find the columns "menu_name" (the machine-radable name of a menu) and "link_path" (either the node/... or the alias).

So, what you'd need to do is the following:

  1. get the current node's NID
  2. query "url_alias" if there's an alias for node/NID and retrieve it, otherwise leave node/NID
  3. query the "menu_links" table for the path you've determined and retrieve "none" or the menu's machine-readable name

You could then also query the table "menu_custom" to check what's the human-readable name of the menu you've determined.

Anyway, that's a complicated query (several queries?) and I'm a MySQL ignorant, so I can't help you with the actual code you'll need to use to check all that :P.

2
  • 1
    an easier way is to simply look at $node->path and query the menu_links table. So a single query, was hoping there would be a smarter/easier built in way, i can't belive that the $node object doesn't hold the info for what menu it belongs! :/ Feb 6, 2010 at 0:55
  • Oh, yes, you're right. I've been doing manual DB tweaking today (Worpress import messed some info up) and was thinking only about it :D. the table "node" contains information line NID, VID, content type (page, story...), timestamps for creation and last modification, creator's UID and a few more. Sadly, nothing about menus. It works the other way round in Drupal: it's the menu that leads the user to a node, not the other way round... :(
    – mingos
    Feb 6, 2010 at 1:09
0

This isn't a direct solution and I see from your reply to a previous answer that you didn't wanted the simplest solution possible, but I thought I'd mention this option. The Menu Node API module maintains a table which Drupal lacks, a node-menu relationship table.

The module does nothing on its own, but there seems to be contributed modules which build on this, so depending on how complex your problem is it might be a starting point.

1
  • There is a direct way of doing this and it is offered by lmeurs on this same page.
    – asiby
    Apr 5, 2013 at 19:16
-1

http://drupal.org/node/584984

Updated: Sorry guys, didn't even realize I had posted this link. I think I intended it as a draft and simply posted it when closing tabs. That said, mingos (above) is right on. My link is to a function menu_get_active_menu_name() that appears to provide you with an array containing the active menu for the current page. As I presume that is what you are using it for, it would be a nice way to abstract yourself away from the database calls that might cause problems down the line. I myself have never tried it, which is probably why I didn't elaborate and post. well... at least didn't post on purpose.

2
  • I assume you were referring to the example on the referenced page, but it referrs to a function that will "Get the active menu for the current page - determines the active trail.", you should also try to expand on your posts to explain why you're linking to something.
    – electblake
    Jan 5, 2011 at 17:18
  • Ouch... don't even remember linking to this. Must have done it as a draft and submitted. I'll edit the post. Feb 9, 2011 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.