How to find a menu item pointing to the specific node?

On node creation I want to create a main_menu link for the node. With menu_rules it is relatively easy. The problem arises when the parent element can differ.



In the above case adding a new group D is easy - I simply add a menu item to 'Groups', but what if I would like to add a3? I know the name of A and I know what it is linking to (a node representing a group) but I don't know its randomly generated menu ID (for example [menu-1082]).

How do I find that Id?

3 Answers 3


I'm almost new user of Drupal, but I encountered the same problem because I write myself my modules. My need is to create a node and to insert it as submenu of a main menu Item. I solved my problem in this way:

I wrote this function

function mymodule_get_plid($menu_name , $name) {
    $menu = menu_tree_all_data($menu_name);
    foreach ($menu as $item => $menuitem) {
        if ($name == $menuitem['link']['link_title']) {
            return $menuitem['link']['mlid'];
    return false;

where $menuname is a string like 'main-menu', $name is the menu item for which I'm searching for it's Id. Function return Id if it find $name, false otherwise.

Now to complete node creation i wrote this.

$node = new stdClass();
$node->title = "YOUR TITLE";
$node->type = "page";  // Creating a node of Basic page type
node_object_prepare($node); // Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled
$node->uid = $account->uid;
$node->status = 1; //(1 or 0): published or not
$node->promote = 0; //(1 or 0): promoted to front page
$node->comment = 0; // 0 = comments disabled, 1 = read only, 2 = read/write

$node->menu['enabled'] = TRUE;
$node->menu['menu_name'] = 'main-menu';
$node->menu['plid'] = mymodule_get_plid('main-menu' , 'Inquiries');
$node->menu['link_title'] = 'MY SUBMENU NAME';
$node->menu['description'] = ''; // Needed even if empty to avoid notices.  /*  

$node = node_submit($node); // Prepare node for saving

Now all behaves well, "MY SUBMENU NAME" is created as a new node and appended as sub menuitem of the 'Inquiries' menu Item of the main menu.


Edit that menu item in your menu admin page, the id is in the URL of the menu item edit page. In most of cases you'll also find the ID of your menu item as a class of the <a> link.


You can either use menu_get_item if you know the path of A, or a db_select on the menu_links table filtering on the path.

  • I know I can do that. The problem is that I am trying to create a rule that will do that automatically on the node creation.
    – Pawel G
    Jan 29, 2013 at 10:14
  • By checking manually and hard-coding the ID I can create a rule that will add menu links to groups, but not for the group content that will be a sub menu of groups.
    – Pawel G
    Jan 29, 2013 at 10:17
  • I edited my answer, now I understand better your problem. Jan 29, 2013 at 10:25
  • menu_get_item returns everything but the id, menu_links seems to be the way to do it. I will post the code once I have it
    – Pawel G
    Jan 29, 2013 at 10:48

To get the ID I query the db using the bellow function. As an argument it takes node id of the content and it returns id of the menu item for the parent group.

function mymodule_get_fathers_menu_item($nid){
    $node = node_load($nid);
    $fathers_gid = mymodule_get_father($node);
    $fathers_nid = mymodule_get_nid_from_gid($fathers_gid);
    $father_node_link = "node/".$fathers_nid;

    $query = db_select('menu_links');
    $query->condition('link_path', $father_node_link,'=');

    $results = $query->execute();
    $result = $results->fetchAssoc();


    return ("main-menu:".$result['mlid']) ;

I then use "Create a menu item for node" action from menu_rules module with the parent parameter as PHP code.

return windbench_get_fathers_menu_item( $value );

and data selector set for node:nid.

My code does not work on node creation (probably something to do with when the og data are saved) so I trigger it on node edit (after publishing).

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.