By akolahi on
From the node you would like referenced, or from a block that displays on that node do something like this:
<p><a href="/node/add/node-type-to-be-created/<?php $id = arg(1); print $id;?>">Click here to create a new node that will have an autocomplete that will reference this node</a></p>
This will add the node id to the url so it can be captured by the new node.
Then using cck node reference field of autocomplete type, place the following in the "default value" section to provide the default value, which will be the node that referenced the creation of this node
$id2 = arg(3);
$mynode=node_load($id2);
return array(
0 => array('nid' => $mynode->nid)
);
This will capture the node id from the url and feed it into the autocomplete field.
If you don't want the autocomplete field to display, you can easily hide it using css.
Comments
Subscribing, since I would
Subscribing, since I would like this inherit feature too.
Thanks
Thanks for this great tidbit. It'll help on a number of projects.
array
When I do this, the words "array" get inserted as the default value, and I never see the id in the url, here's the url I get
http://www.iwasborntowin.com/node/add/application/%3C?php%20$id%20=%20arg(1);%20print%20$id;?%3E
It it because of space? hence the %20?
solved
I had the code in a block and needed to change input value to accept the php code. Works like a charm. Unless I'm missing something this has got to be the easiest, and simplest way I've seen to link one node to another? Although I may learn later that I would have been better actually linking tables together but this seems to bridge the links between 2 different nodes via the content_node_field_instance table where the node reference data is stored.
where in the content type do
where in the content type do i need to put this code? Im new at this but almost done withe the site that i am creating i dont know any coding so a little more detail would help please
I'd like to know too
the PHP box in default value of the link to the content type with the node reference field didn't like that code.....
Hélène
Brilliant. Thanks for this...
Brilliant. Thanks for this... been trying to work this out for a while.