Node reference creation

Events happening in the community are now at Drupal community events on www.drupal.org.
Catalyst5's picture

I know node reference has been covered elsewhere, but I cannot find out how to do this: Any help would be appreciated.

I have a node that contains 2 fields:
field_a contains text equal to a node title (not this node)
field_b is a node reference field (CCK) but is currently empty.

I think the best way to automatically insert a node-reference (without user input) is through Rules (Awesome module I think)

I want to set field_b = the nid of the node refered to in field_a

I don't know what to set field_b = to???? The closest I can get is
return array( 0 => array('value' => [node:nid];), );

but this only returns the nid of the current node.

I'm pulling my hair out (what's left of it anyway) Please help

Comments

I was thinking...

Catalyst5's picture

I tried the following, but this doesn't work either, as it doesn't reference the field_a:

$zonk =[node:nid];
return array( 0 => array('value' => $zonk), );

Maybe if I rephrase the question someone will be able to help:

How do I get the nid of a node represented by one of the fields in this node? Eg. There is a node called "Two". It has a nid of 1465.
The node I am in currently is "One" - It has a nid of 1464. It also has a field called field_a. This field (field_a) contains text of "two" which is the same as the name of the other node. I want to set field_b = value 1465 (which is the nid of "two")

Any ideas?

nid, not value

itangalo's picture

The structure of node reference fields are not the same as text fields – they store the main value under the key nid, not value. You will thus have to use something like this:

return array(
  0 => array(
    'nid' => [node:nid],
  ),
);

Knowing which array structure to use can be tricky, but there are two useful ways of finding out. The first one is to read the fine print under most PHP text areas, since they usually give an example of what should be returned. The other is to use Devel and have a look at the node structure directly – then you'll see the structure of each CCK field as well.

Good luck!
//Johan Falk, NodeOne, Sweden

But different node...

Catalyst5's picture

Hi Itangalo

Thanks for your help with the syntax. The issue is that I need the nid of another node that has the title of the value stored in a field in this node.

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: