views:

366

answers:

4

I'm building a Drupal based site that requires the communication of a node ID to a seperate web service. This web service handles the uploading of files to a seperate server (from the one Drupal is on).

This creates a problem where in if I create a new node, the Node ID is not generated until the form is submitted - meaning I can't attach the files until I save the node and open it back up to edit it. I'd like to remove that step.

Is it possible to create a two step node creation process where the basics of the node are submitted and saved, and then the form re-directs to step two where I can attach the files?

I'd also consider an AJAX enabled node submission form - but that seems to add even more complexity to the situation.

Any advice, examples will be appreciated!

A: 

I ran into exactly this same issue and did it the wrong way. I added the hook myself.

http://drupal.org/node/313389

Daren Schwenke
@Daren: How does your 'postsave' nodeapi action differ from the standard 'insert' and 'update' actions of `hook_nodeapi`? Both occur *after* the node has been persisted, so e.g. with the insert operation, you already have the node id.
Henrik Opel
This was at least a year ago when I wrote that, and I could never get the result from insert to give me the node_id for what I was doing. I know it had something to do with file attachments. Sorry I couldn't be more help.
Daren Schwenke
+4  A: 

you could do this with a multi-step form. see http://pingv.com/blog/ben-jeavons/2009/multi-step-forms-drupal-6-using-variable-functions for the canonical way to do this (besides the code, also check the comments).

you could also do it by adding a second submit handler to the form. the first, default one (node_form_submit) saves your node (including the attached file) the standard Drupal way. the second handler could upload the file to the separate server, do upload error checking, delete the file from the Drupal DB, etc. you can add an additional submit handler to a Drupal 6 form by adding it to the form's #submit property, either in the form definition or via hook_form_alter / hook_form_FORM_ID_alter.

ax
+1 for adding a second submit callback
Henrik Opel
Since the second submit callback would already have access to the nid in `$form_state['nid']`, it could also just change `$form_state['redirect']` to 'node/[nid]/edit to immediately open the nodes edit form again after submission (would need check for new node to prevent redirection after second form submission).
Henrik Opel
Thanks! I've built the multi-step form (hooked into the the standard node add/edit form). Still struggling with getting the $form_state variable not being passed to my validator function - none of the examples I've found seems to show how this variable is passed around.
PrairieHippo
+2  A: 

Depending on what exactly you want to do, you might use hook_nodeapi on its 'insert' operation. It is fired after successful node creation, so the node object will contain the newly assigned nid there already.

NOTE: The wording of the API documentation is a bit ambiguous concerning the 'insert' and 'update' operations:

"insert": The node is being created (inserted in the database).

This sounds like it is right in the middle of the process, whereas the node has already been created at this point.

Henrik Opel
+1  A: 

I guess the node_save function can help you.

Geshan

related questions