0

While creating a new node, I'd like to pass a parameter in the URL: node/add/page?plid=123 in order to put the newly created node in this menu item as its child.

I tried to hook_form_alter() the node form but I couldn't populate the menu part, also I'd like to hide this part to the end user as they shouldn't alter it.

Should this be done in a hook_node_insert() with something like this answer: How to enable menu link when creating a new node ?

1

hook_node_insert() is probably the best place to do this. You'll want to call menu_link_save() to create the menu item, using the $_GET superglobal to retrieve the plid parameter:

mymodule_node_insert($node) {
  $item = array(
    'link_path' => 'node/' . $node->nid,
    'link_title' => $node->title,
    'plid' => $_GET['plid'],
  );
  if (!menu_link_save($item)) {
    // menu_link_save() returns the mlid of the new item. Handle a failure here,
    // which will occur if the plid supplied is invalid.
  }
}
  • Hi Mark, thanks for your answer, it works great but now I have a follow-up question if you'd like to chime in ;) – tostinni Aug 25 '11 at 18:09
  • I'm pretty sure that plid isn't checked to be a valid mlid but it may be good to ensure this. I finally move my code to hook_node_presave() to avoid problems with pathauto. – tostinni Aug 28 '11 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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