0

I know how to setup a multi-page webforms (basically add page breaks' between each page). I also know how to use the conditionals to skip certain questions based on the values or state of previous questions.

However, I would like to do this dynamically. So instead of using conditionals to change the workflow, I would like to use my own logic (code) to decide what the next question should be. The reason is I have a webforms with 42 questions (pages) and the actual workflow can change significantly.

All I am really asking is how do I tell webforms what the next question is that should be shown.

I guess it is similar to this question (How to jump to a specific page in a multipage Webform) but the solution quoted (Webform steps Module) does not solve my problem.

1 Answer 1

0

Solved.

I added hook_form_alter and checked if I am working with webforms (I suppose you can check if you are working with a specific webform), and then added a custom validation call-back:

  if (strstr($form_id, 'webform_client_form') !== FALSE) {
    if (empty ($form['#validate']['_set_next_page'])) {
      $form['#validate'][] = '_set_next_page';
    }
  }

In the _set_next_page function I get the value of the submitted component. In my case there is only one question per page, but you can easily adapt that

$submitted = array_keys($form_state['values']['submitted']);

Remember if this is the first page (question), the array looks slightly different, so:

  if ($form_state['webform']['page_num'] == 1) {
    $Q = $submitted[0];
  }
  else {
    $Q = $submitted[1];
  }
  // Get the value that was submitted
  $value = $form_state['values']['submitted'][$Q];

Assuming $destination holds the 'form_key' of the question you want to jump too:

   if ($destination) {
    foreach ($node->webform['components'] as $component) {
      if ($component['form_key'] == $destination) {
        $nextPage = $component['page_num'] - 1;
        if (($nextPage > 1)
          && ($nextPage < $form_state['webform']['page_count'])) 
        {
          $form_state['webform']['page_num'] = $nextPage;
          break;
        }
      }
    }
  }

I set the page_num to the page_number for the destination component to one less, because webforms will increment this value once the submission is processed.

I am sure the code needs some re-finement (make sure that I do not crash and burn while accessing array elements / objects that does not exist) - but you get the idea.

Hope this helps someone else.

Your Answer

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

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