how to programmatically create menu items while creating nodes?

Posted by sprugman on Stack Overflow See other posts from Stack Overflow or by sprugman
Published on 2010-04-08T20:38:59Z Indexed on 2010/04/09 16:33 UTC
Read the original article Hit count: 465

Filed under:
|
|
|

I'm creating some nodes programmatically, thus:

foreach ($titles as $t) {
    $n = new stdClass();
    $n->type = 'myType';
    $n->uid = 1;
    $n->title = $t;
    $menu = array();
    $menu['link_title'] = $t;
    $menu['menu_name'] = 'primary-links';
    // this attempt at placing the menu item in a particular place in the 
    // menu hierarchy didn't work:
    $menu['parent'] = 'primary-links:867';
    $menu['depth'] = 3;
    $menu['p1'] = '580';
    $menu['p2'] = '867';
    $n->menu = $menu;
    node_save($n);
}

I've got a menu structure like this:

primary-links
    Parent 1
        Child 1
        Child 2
    Parent 2
        Child 3

I want the new menu items to appear as children of Child 3. I was able to create menu items at the same time as the nodes, and they appeared in the correct menu, but not in the correct place in the hierarchy. What am I missing?

© Stack Overflow or respective owner

Related posts about drupal

Related posts about api