1

I try to enable the menu link programmatically, here is how I do it:

function create_menu_link() {
    $node = new stdClass();
    $node->type = 'mytype';
    node_object_prepare($node);

    $node->menu['link-title'] = $menu_title;

    node_save($node);
}

But setting only the title is not working. In $node->menu there is some other variables like mlid and plid which are set to 0 it has to do with this ? The menu_name is set to main-menu:0, module is set to menu.

2 Answers 2

5

You'll need to create the menu item separately, using menu_link_save

$item = array(
  'menu_name' => 'main-menu',
  'link_title' => $node->title,
  'link_path' => 'node/'. $node->nid,
);
menu_link_save($item);
1

You don't have to create the link manually. The menu_node_save() hook will take care of link creation automatically when saving the node, if you provide the following variables:

  $node->menu['enabled'] = TRUE;
  $node->menu['link_title'] = t('My title');
  $node->menu['description'] = ''; // Needed even if empty to avoid notices.
1
  • This should have been the correct answer because it use the base principle of Drupal : hook!
    – Steve S.
    Dec 22, 2015 at 15:58

Your Answer

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

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