Let's say my module defines a new node type. We'll call it TypeA.

The form that lets site admins reate a new node of this type contains a bunch of custom information, and is split into two sections. One of these sections allows the user to create an single, individual node. The other allows them to uplaod a .csv file that contains all the information for an indeterminate number of nodes. Each of these sections has its own submit button, because each is used for a distinct reason. When a user decides to upload a .csv file, I want to intercept and stop the normal node creation process. From there, I want to parse the data in the .csv file, and manually start the node creation process, handing it the correct information from each line of the file, one line at a time, creating a bulk node process.

Two things I'm wondering, then:

1. Is this even possible and, if so, how would I go about it? I've been looking throguh API docs for most of today, checking the current hooks and the example modules, looking for something to help, but I'm at a loss; and

2. Since the trigger for this mechanism is going to be the hook_validate and hook_submit implementations in my module (atleast, I assume it will be) is there any way inside one of those two I can detect which of my two submit buttons was pressed, so as to send it down the correct processing chain?

Thanks a lot!

Comments

dwees’s picture

I have thought of the same process, and it is definitely possible. You would handle the bulk import of the nodes in your submit function, and you would have to carefully 'craft' the nodes ahead of time, and use the node_save function. I actually do exactly this in a module where people can upload 5 images at a time and have each image attached a taxonomy term and a location chosen by the user.

For the CVS import, you want to look at the TableManager module because they have a CVS to Table import function for that module.

The second part about the forms is an API question. Basically each form on a page has a unique id, given either at the time of the form creation in Drupal 4.7.x, or equal to the specialized builder function that creates the form in 5.x. Suppose we had the form with the id of 'mymodule_form_one', then we can do any custom validation for this form in 'mymodule_form_one_validate' and any submission of the form to 'mymodule_form_one_submit'. So that means that even if you have two forms on the same page, they each have separate submit functions, and you can separate your logic that way.

If you are not using the forms API (which I suggest you do because it makes your life much easier, and your forms are much more secure), then I suggest you add a form_token that is unique for each form, and check for its value to split the logic using the availability of either token.

Dave