Auto populate a nodereference field using the URL?

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

I have a node type A and node type B. B-nodes have a nodereference field that point to an A-node so that one A-node has many child B-nodes.

I need a link placed somewhere in the A-node (could be on a block as well) that says "Create a child B-node". This link should open the form to create a new B-node with the nodereference field:
1) hidden
2) pre-populated with the nid of the calling A-node

What is the best way to do that in Drupal 6?

Comments

Prepopulate module

christefano's picture

Try the Prepopulate module: http://drupal.org/project/prepopulate

It won't hide fields but it will populate them from the URL.

rules

fago's picture

you could also do this by using the rules module - there is a populate a field action... Then you could hide the field by setting appropriate permissions.. Or just do it with php code for the default value of the field.. However it might be that this isn't executed when the field is hidden this way...

Or you could just code this

dwees's picture

Or you could just code this yourself, it's a 2 function module.

  1. hook_links - adds the link in question to your type A node which should be a path link "node/add/type-b/" where is the of the type A node with the link.

  2. hook_form_alter - modifies the CCK field on form B to #type = value and sets the #value of it to arg(3) (after checking that arg(3) is numeric AND a node of type A).

Dave

That sounds like not a lot

kirilius's picture

That sounds like not a lot of work, however I am not that good in PHP. I could probably do it but it will be far from robust. Do you have a link to samples or any materials/tutorials/examples of similar functionality?

rules

fago's picture

then consider using rules - you can easily create a rule that sets the value of the nodereference - then you need PHP just for getting the argument.

Thanks, I will give Rules

kirilius's picture

Thanks, I will give Rules and Prepopulate modules a try then!

Node Reference URL Widget

kswan's picture

I think Node Reference URL Widget (http://drupal.org/project/nodereference_url) is what you are looking for.

why bother with the node reference widget . . .

Hunabku's picture

If you want to hide the reference field you'll need to alter the form. And if you are altering the form just do all the args populating of your fields then - not just nodereference.

see this example: Populate CCK Node Reference Field and hide it

Pulling arguments using the field default

strellman's picture

This is what helped me:
http://drupal.org/node/126776#comment-3460726
Simple, if you don't care about hiding your field

Content Construction Kit (CCK)

Group organizers

Group notifications

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