1

I've a node type Person and want to attach a new submit handler. Will it be fine if I call drupal_goto to another URL from within this new submit handler?

What I want to do is to ask for some confirmation message and then save the node using drupal_execute.

In the submit handler I'll save $form_values in session using key form #build_id and then in it I'll redirect it to another url with this build_it in the url as GET parameter. In the redirected form url I'll be doing some checks on the node data( if same title node already present etc) and asking for confirmation. If yes then I'll create/save the node myself using drupal_execute.

Will it work this way?

7

Can you call drupal_goto in a submit handler: Yes
Should you call drupal_goto in a submit handler: No

Why?

The reason is that potentially other submit handlers might need to be called in order for things to work properly. If you call drupal_goto you would stop the execution flow and make it impossible for the other submit handlers to do their thing.

What you should do instead is to use $form_state['redirect'], which when set will be the url that is redirected to when the submission process is completed.

In your specific use case, however it could work since you don't want the node to be saved, but stopping the process in this way could give you strange bugs/side effects.

But you really should't be doing it this way.

Instead you should attach a validate handler to the node form. If the validation fails the node is not saved and you can display any message to the user via drupal_set_messsage or form_set_error. You could save in $form_state that the message has been displayed to the user and allow him to save the node anyway if he resubmits the form. This is what the validate handlers are built for, after all.

1
  • Thanks, got it. What I want to is in this question: drupal.stackexchange.com/questions/21709/… . I want to save the $form_values in permanent storage while in the submit handler and use it later to create the same node. I want to handle it in submit handler because it'll ensure that all validate handler have run. Also I want to abort saving the node data in submit handler as I want to do it later.
    – AgA
    Feb 6 '12 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.