0

I need to navigate the user through different pages of a multistep webform. I have added additional submit button to the webform using following code.

function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if($form_id == 'mywebform_id') {
    $form['actions']['submit_button_step_1'] = array(
      '#type' => 'submit',
      '#value' => t('Go to Step 1'),
      '#submit' => array('custom_navigation'),
    );
    $form['actions']['submit_button_step_2'] = array(
      '#type' => 'submit',
      '#value' => t('Go to Step 2'),
      '#submit' => array('custom_navigation'),
    );
}
}

But now How could I navigate the user to the pages based on user clicks on which button?

I found this in community website. But not able to understand where should I put this code in my custom module.

How to programmatically skip pages in wizard forms

0

I used another more durty way. I just hide pages which must be skipped

function my_module_webform_submission_form_alter(array &$form, FormStateInterface $form_state, $form_id)
{
  if (!isset($form['#webform_id']) || $form['#webform_id'] != 'my_form') {
    return;
  }

  $pages = &$form_state->get('pages');

  if (some_statement) {
    $pages['skipped_page_name']['#access'] = FALSE;
  }
}

but it works only in forward direction. And if user clicks "previous page" it may cause unexpected behaviour. So you also need "show" pages again on each step:

$pages = &$form_state->get('pages');
foreach ($pages as &$page) {
  $page['#access'] = TRUE;
}

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.