creating multiple nodes with one submit
chrisabbato - November 15, 2008 - 21:45
I am attempting to create multiple nodes in one form with one submit button, however, when I press submit, it only creates the second node type that I added to the form. I've been looking around at all the different available functions available for form manipulation, but I cannot figure out how to create both types of content from a single submit button.
Please help! I've been working on this for hours upon hours with hardly any progress.

You can easily create a node
You can easily create a node by calling node_save($node) with a suitably constructed node object. If you are using a standard node edit form then I don't think Drupal is able to parse it into 2 nodes without your creating one of them in this way.
gpk
----
www.alexoria.co.uk
Thank you for your reply. At
Thank you for your reply.
At one point I had tried using node_save, but I could not seem to get it to work.
I am almost not sure I'm bringing the form in correctly. Right now I am using form_alter on node A and then using retrieve_form to get node B and I am making node B a sub-element of node A.
Is there a different way to do this that I have missed?
I have tried so many different things in so many different ways I have confused the hell out of myself at this point.
I'd just use form alter on
I'd just use form alter on the relevant node edit form to provide additional custom form fields, then extract them on submit to populate the custom $node object -> node_save() it.
gpk
----
www.alexoria.co.uk
excellent! I was able to do
excellent!
I was able to do just this, thank you for the help.
I thought I would save time by using the pre-defined forms for nodes, but I think it just caused more collision than anything.
Now I am able to create a page node with a sub node inside of it.
Would you be able to point me in the right direction to make it more thorough? Such as being able to edit the forms later on and have the values filled in and updated correctly.
>I thought I would save time
>I thought I would save time by using the pre-defined forms for nodes, but I think it just caused more collision than anything.
Yes, a node form is a form for "a" (single) node ... at the end of the day there's nothing in Drupal form/node handling to allow multiple forms to be handled from a single submit/POST.
To handle the subsequent edit I'd do something very similar - form_alter on the node edit form, use node_load() to get the node attributes you need to populate the form fields with, and handle the submission as before.
You know, there *might* be away of getting multiple node forms to work on a single page, but it's probably not worth the effort it would take ..!
[update] Actually, thinking about it, I have a feeling there's a CCK module that lets you define a sub-node or something. Might we worth a look. http://drupal.org/project/add_n_reference http://drupal.org/project/nodereferrer_create http://drupal.org/project/related_subform - this last one might be best but not available for 6.x yet AFAICS. Maybe rolling your own solution easiest after all.
gpk
----
www.alexoria.co.uk
Hi, I have the same problem.
Hi,
I have the same problem. For example:
- I have 2 content types, Content Type A and Content Type B.
- Content Type B contains a node reference that references Content Type A.
I would like to have a form where I can fill in the fields of Content Type A and Content Type B on a single page. The node reference field will also be automatically filled up upon submit.
I do not have a good solution for this.
What I'm thinking is that I have a 3rd content type, Content Type C. Content Type C has all the fields of Content Type A and Content Type B. Upon submission of content Type C, I will set a action and trigger that problematically generates Content Type A and Content Type B.
I think that this is not the best solution that I can have. Do you have any other suggestions?
Thanks!
Did you look at
Did you look at nodereferrer_create and related_subform modules mentioned above? Your method should also work, or look at the approch described above using hook_form_alter().
gpk
----
www.alexoria.co.uk
Hi, If I'm not mistaken
Hi,
If I'm not mistaken related_subform module (http://drupal.org/project/related_subform) is still in beta and it deals more with relativity than node reference. Furthermore, it is only available in Drupal 5.
For the nodereferrer_create module (http://drupal.org/project/nodereferrer_create), I think it is meant for creating the nodereferrer seperately. Whereas, in my case I wanted to create them simultaneously in the same form.
For my method, what custom module features can I write to make the entire process cleaner? Or is my method not a very good start? There will be duplication of data because the same data will be stored both in table of Content Type C but also A and B.
Thanks!
TBH for those reasons it
TBH for those reasons it might be easier to use the approach described by chrisabbato, i.e. form_alter the node create/edit form to add the fields for the 2nd node type and then intercept on submit to do the clever stuff. Storing the same stuff in 2 places is a recipe for headaches!
gpk
----
www.alexoria.co.uk