Auto populate a nodereference field using the URL?

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by kirilius on December 6, 2008 at 3:53am

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?

Categories:

Comments

Prepopulate module

Posted by christefano on December 6, 2008 at 5:40am

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

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

rules

Posted by fago on December 6, 2008 at 9:46am

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

Posted by dwees on December 6, 2008 at 8:13pm

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

Posted by kirilius on December 8, 2008 at 9:16pm

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

Posted by fago on December 10, 2008 at 12:29pm

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

Posted by kirilius on December 11, 2008 at 4:41pm

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

Node Reference URL Widget

Posted by kswan on March 9, 2009 at 7:29pm

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 . . .

Posted by Hunabku on September 4, 2009 at 11:43pm

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

Posted by strellman on September 17, 2010 at 7:11pm

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:

AltStyle によって変換されたページ (->オリジナル) /