I've created two content types, Father and Son, and I've written a simple hook_form_alter() which populate a field using the 4th URL argument:

function sons_form_alter(&$form, $form_state, $form_id) {

  if ($form_id == 'son_node_form') {
    $form['field_father'][0]['#default_value']['value'] = arg(3);
  }

}

In this way, if I go to node/add/son/123, field_father will be populated with the Father nid.

Now, I want to display the son_node_form form when I'm on a Father node (eg. node/123), with that field_father populated with the Father nid (123).

How can I do that?

I've used node_add('son_node_form'), but it displays the node/add/son form, not the node/add/son/123 one.
How can I pass the 123 argument to node_add() so that my hook will correctly populate the field?

Greetings, Giovanni

Comments

meet.h.thakkar’s picture

I am also having same problem i think this should be a fetaure

m4manas’s picture

AS i understand drupal breaks URI and make it arg. It must be available in the form as argument. How about making a hidden field in node_form_alter and assign the value of the arg.

m4manas’s picture

function MODULE_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'node_form':
    drupal_set_message(arg(3));
break;
}
}

Give a try you will find you argument right there.

Antinoo’s picture

@m4manas: my problem was to use the node_add() function in order to show the form appearing at node/add/son/123 (the 4th argument automatically populates a CCK field), and not the default one appearing at node/add/son.

danielb’s picture

The only way your arg(3) trick will work is if the current page you are on (the father node) has that arg(3).

Since you're embedding in the father node, in your hook_form_alter you could instead test to see if you are on a node page (arg(0) is 'node' and arg(1) is numeric) and the numeric arg is the father node id. Doesn't matter if you're using aliases either. But this idea won't work in teaser lists.

Another idea you could try is instead of using the node_add() function you will have to create you own function identical to node_add() and call that instead.
In this function you can add a value to $node before it gets sent to drupal_get_form() and pass it in like that.

shaneforsythe’s picture

I had the same issue where I had Hierarchical type data, and wanted to have the add/create form for the child node when viewing the parent.

>Now, I want to display the son_node_form form when I'm on a Father node (eg. node/123), with that field_father populated with the Father nid (123).

This is how I did it for code on a page with php filter
(you might not need the module_load_include , if drupal_get_form is being called from within a module hook_view type function )

<?php
module_load_include('inc', 'node', 'node.pages');  
$new_item_node = array('type' => 'mymodule' , 'parent' => '$nid');

print drupal_get_form('mymodule_node_form', $new_item_node);
?>

in the mymodule.module file

function mymodule_form(&$node, $form_state) {
  $type = node_get_types('type', $node);

  $pid = $node->parent;
.....