1

I am connecting to an API that returns some data based on a user's zipcode. This data needs to be included in the payload of a second call to the API along with sone other form values.

I wanted to build the webform as a multistep, the first page collects the zip code and after the Next Page is clicked, do an API call to send the zipcode and save the response from that call, so that on the second page the Submit sends via another API call that response data plus all of the form values.

This Get the draft data in the webform hook_form_FORM_ID_alter() seems to be the answer but not quite.

2

You should write a custom WebformHandler that adds the return API data to $form_state and then alters the form elements as needed. There are some blog post available about writing webform handlers.

Here is how to get the current page from the $form_state.

/** @var \Drupal\webform\WebformSubmissionForm $form_object */
$form_object = $form_state->getFormObject();
$current_page = $form_state->get('current_page');
  • This is helpful. The examples I've found all seem to want to alter the final values right before the submission. The latest version I'm running seems to be full of examples of custom handlers handling things at the end, any pointers for per page would be helpful. – StevenPatz Jan 18 '18 at 15:03
  • This ticket includes an example module which shows you how to get the current page from $form_state. drupal.org/project/webform/issues/2936805 – jrockowitz Jan 19 '18 at 16:25

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.