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?
Comments
Prepopulate module
Try the Prepopulate module: http://drupal.org/project/prepopulate
It won't hide fields but it will populate them from the URL.
rules
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
Or you could just code this yourself, it's a 2 function module.
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.
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
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
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
Thanks, I will give Rules and Prepopulate modules a try then!
Node Reference URL Widget
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 . . .
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
This is what helped me:
http://drupal.org/node/126776#comment-3460726
Simple, if you don't care about hiding your field
http://2tell.org/doubt