0

I have an initial webform (based on 3.x of webform module/Drupal 7) that needs to redirect to a second form if yes is specified in a yes/no choice. Otherwise if No, the first form should submit normally (with a webform result).

Also, if yes is entered on the first form, on submitting the form, the webform result is not to be stored for this form, but the webform result will be stored on the second webform. Values entered in the first form are carried through to the second, to autofill, I use the PHP _SESSION variable to do this.

I'm having difficulty getting this to work completely. It always seems that I can get one part of the above working, but not the other. I'm using the standard approach of hook_form_alter and within this trying various combinations of setting the validate array and submit array and the form redirect property to do the redirect from the first and second form.

My approach now is to have a validate handler function to change the submit handler if the yes is chosen (for proceeding to the second form). Submitted values on the first form are available to the validate handler. The submit handler can do the redirect to the 2nd form.

But when I change the submit handler from within the validate handler, I see that this change gets overwritten by Drupal itself and I don't know why.

I have also found that the submit handler - which I use to set the form redirect, for the 'yes' choice - will not work if other submit handlers are present in the submit handler array. I have to empty the array and have only my function in it.

I had wanted to deal with the yes/no choicepoint at this stage in my own submit handler, but if I had to 'throw away' the other submit handlers, then the 'No' path won't work - the webform result won't get submitted.

Another approach I tried, to counteract my deleting of the other existing default submit handlers, was to programmatically submit a form for the the first form for the 'No' path so that it would get saved. However doing this I got an error about the number of submissions possible, even though it was set to unlimited (-1) in the config. From that I concluded that it was due to that I had already started the first form as a visitor and somehow the webform system 'notes' that, so programmatically submitted form would exceed that supposed maximum.

Note: I can't upgrade to webform 4.x as there are other forms on the site which may be impacted unexpectedly.

1 Answer 1

0

You don't need to rewrite form submit handler. You can implement drupal_goto() for your first form with FORM_ID_submit() depending on user input. Pass $form_state['values'] as url options and fetch them from $_GET array as #default_values in second form.

2
  • Thanks but a couple of issues with this answer: drupal_goto is not considered best practice I believe, don't know the details but I could understand why as I know the concept of goto is not well regarded in other programming areas when more refined language constructs such as functions, while, for, etc are available. drupal_goto exists to be an absolute last resort, and I'm not at that place at the moment. Secondly I don't think FORM_ID_submit is available in Drupal 7, there was hook_submit in Drupal 4.7 - was that what you meant? If so, it's not in Drupal 7. Jul 21, 2014 at 8:01
  • Instead of drupal_goto you can override $form['#action'] in hook_form_alter(). By FORM_ID_submit I mean custom submit function defined with $form['#submit'].
    – Artreaktor
    Jul 21, 2014 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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