Attempting to load an intermediate page of a multistep Webform with early page values pre-filled

Created on 16 August 2018, over 6 years ago
Updated 25 June 2024, 6 months ago

Hello! We're looking for more guidance on how to link directly into an intermediate page of a multi-step form. For context, what we're trying to do is use query parameters to indicate the values of elements in the early pages of the form (which is fine, not having problems with setting values) and then jumping to page 2 or 3 with those values pre-filled.

We have tried reading the value of current_page from a query parameter, setting that on form state, and submitting and rebuilding the form (basically the steps in WebformSubmissionForm::wizardSubmit). We've tried using states on earlier wizard pages that respond to becoming hidden based on values in query parameters to hide the initial pages on load, which also doesn't work (though using values on elements in earlier pages to skip intermediate pages through use of states does work fine and we are doing that).

We've also tried unsetting the pages we want to skip in $form['pages'], setting $form['#attributes']['data-webform-wizard-current-page'] to our desired destination, and altering all attributes of the pages we want to skip in the ways they appear to be on a "valid" intermediate submission (code snippet follows).

Populating fields from query parameters is enabled and wizard page tracking is also enabled.

If you have any further guidance on where to look to try and make this happen, we'd appreciate it. We're very happy to write custom code to make it happen. We've tried a few avenues and we're not having luck thus far.

Basically, we're trying to understand how the form makes the decision to render an intermediate wizard page, and replicate that programmatically so that it can be done on first load.

We've attempted calling this from several steps. It's kind of a jumble of different techniques, but represents some of what we've tried.

function pin_help_webforms_help_center_contact_preload_page(array &$form, FormStateInterface $form_state) {
	$submission_form = $form_state->getFormObject();
	$pages = $form_state->get('pages');
	// Set the page we want to be current page.
    $destination = 'about_you_page';
    $form_state->set('current_page', $destination);
    $form['#attributes']['data-webform-wizard-current-page'] = $destination;
    // Remove the first 2 pages from array.
   	unset($pages['overview_page_1']);
   	unset($pages['overview_page_2']);
   	$elements["overview_page_1"]["#access"] = FALSE;
   	$elements["overview_page_2"]["#access"] = FALSE;
    $form_state->set('pages', $pages);
    $form['pages'] = $pages;
    // Set next link to for page after about you page.
    $form["actions"]["wizard_next"]["#attributes"]["data-webform-wizard-page"] = 'the_problem_page_1';

    // Try setting the first 2 steps as completed.
    $formStateCompleted = $form_state->get('complete_form');
    $formStateCompleted["elements"]["overview_page_1"]["#access"] = FALSE;
    $formStateCompleted["elements"]["overview_page_1"]["#defaults_loaded"] = TRUE;
    $formStateCompleted["elements"]["overview_page_1"]["#processed"] = TRUE;
    $formStateCompleted["elements"]["overview_page_1"]["#after_build_done"] = TRUE;
    $formStateCompleted["elements"]["overview_page_1"]["#validated"] = TRUE;

    $formStateCompleted["elements"]["overview_page_2"]["#access"] = FALSE;
    $formStateCompleted["elements"]["overview_page_2"]["#defaults_loaded"] = TRUE;
    $formStateCompleted["elements"]["overview_page_2"]["#processed"] = TRUE;
    $formStateCompleted["elements"]["overview_page_2"]["#after_build_done"] = TRUE;
    $formStateCompleted["elements"]["overview_page_2"]["#validated"] = TRUE;
    $form_state->set('complete_form', $formStateCompleted);

    // Try submitting form and rebuilding state.
    $submission_form->submitForm($form, $form_state);
	$form_state->setRebuild();
}

Any advice much appreciated. Thank you~

πŸ’¬ Support request
Status

Fixed

Version

5.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States timcosgrove

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¦πŸ‡ΊAustralia imclean Tasmania

    For others coming across this issue, a different hook is required when loading a previous submission by token. This also works when creating a new submission so may be preferrable to hook_ENTITY_TYPE_create().

    /**
     * Implements hook_ENTITY_TYPE_prepare_form().
     */
    function CUSTOM_MODULE_webform_submission_prepare_form(\Drupal\webform\WebformSubmissionInterface $webform_submission, $operation, \Drupal\Core\Form\FormStateInterface $form_state) {
      $current_page = \Drupal::request()->query->get('page');
      if (empty($current_page)) {
        return;
      }
    
      $pages = $webform_submission->getWebform()->getPages();
      if (isset($pages[$current_page])) {
        $form_state->set('current_page', $current_page);
      }
    }
    

    Example request path:

    /form/example-wizard?current_page=contact&token=PYNaCklH_mocS8aru_pdPJhE9Iicr9ibEKHNmxVU4UU

Production build 0.71.5 2024