How to set value for CCK Node-reference field automatically when creating a form

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

Since the reference node is always known, I do not want the user to enter a control value, I want to set it for them behind the scenes. I found several threads about this, but none of them seem clear or really work for me.

Any help would be greatly appreciated.

Note: Drupal 6

+3
source share
2 answers

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]['nid'] = 'node reference value';
            node_save($node);
         } 
         break;
   }
}

This should add a value to the node and save it after it is created.

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

Note. To do this, you need to create a module. You can also try the rule module, although I'm not sure that it will do what you ask for without a special rule. But I know that this method will work.

+6
source

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

+2
source

Source: https://habr.com/ru/post/1765888/


All Articles