views:

479

answers:

3

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?

+1  A: 

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.

mingos
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! :/
Shadi Almosri
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
A: 

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.

tdous
A: 

http://drupal.org/node/584984

Jason Smith

related questions