views:

26

answers:

2

I have a content type (A) that references a single node of a different content type (B). The node referenced (B) can be programmatically determined using the information for the user creating this new node (A)... Each user can only create a single node of the referenced content type (B), so this single node will always be referenced from nodes of content type B that the user creates.

Because the referenced node is always known, I don't want the user to have to enter the reference value, I want to set it for them behind the scenes. I've found a number of threads about doing this, but none seem to be clear or actually work for me.

Any help would be greatly appreciated.

Note: Drupal 6

+2  A: 

You can try:

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
   switch ($op) {
      case 'insert':
         if ($node->type == 'type_a') {
            $node->field_of_reference[0]['value'] = 'node reference value';
            node_save($node);
         } 
         break;
   }
}

This should add the value to the node and save it after it has been created.

http://api.drupal.org/api/function/hook_nodeapi

Note: You will need to create a module to facilitate this. You can also try the Rules module, though, I am not sure it will do what you ask without a custom rule. But I know the above method will work.

Kevin
I haven't tried it out yet... but this will override the default submit action correct? If the node reference field is required, will the form be validated before it gets to this point?
Chaulky
Don't make the form field required, and this supplements the submit action, not override.
Kevin
Very good point!
Chaulky
This solution worked for me and was rather quick. One note to make though, in the code above it should be field_of_reference[0]['nid']... note the 'nid' instead of 'value'
Chaulky
+1  A: 

Without any programming - use "Rules" modules, event - node update, action - set field to some value.

Nikit
I'd prolly still need some programming to get the value for the node reference field. Can you use PHP snippets with the Rules / Action module? I'm assuming you can. Thanks for the tip, I might give this one a try.
Chaulky

related questions