How to find which menu a node belongs to in drupal

10,175

Solution 1

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.

Solution 2

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

Solution 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.

Share:
10,175

Related videos on Youtube

Shadi Almosri
Author by

Shadi Almosri

Eat code, drink code and live code, well not exactly I love to indulge in a steak sandwich and gulp down on a freshly squeezed banana and strawberry smoothly. If I’m not enjoying a short break I’m tackling the next big technical challenge whether its building a huge site or supplying technical support for clients I’m always focusing on how I can better myself professionally. All though my position isn’t just about ones and zeros, I take my skill and translate that to a commercial sense. If it sounds like a lot of work it is but this my passion, my hobby my work, my life, I just need to remind myself I do need to sleep sometimes.

Updated on April 15, 2022

Comments

  • Shadi Almosri
    Shadi Almosri about 2 years

    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?

  • Shadi Almosri
    Shadi Almosri over 14 years
    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! :/
  • mingos
    mingos over 14 years
    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... :(
  • electblake
    electblake over 13 years
    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.
  • Jason Smith
    Jason Smith about 13 years
    Ouch... don't even remember linking to this. Must have done it as a draft and submitted. I'll edit the post.
  • asiby
    asiby about 11 years
    There is a direct way of doing this and it is offered by lmeurs on this same page.