3

I am trying to add additional submit buttons to the webform to navigate between different steps. I have added button like below:

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'),
    );
  }
}

function custom_navigation(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {

  $value = $form_state->getValues();
  //echo '<pre>';print_r($form['actions']);echo '</pre>';exit;
}

But, I'm using same custom submit handler both the custom buttons. How I identify which button has clicked by user? And based on user click I need to redirect user to corresponding wizard page.

2 Answers 2

3

Below codes work for two submit buttons given at webform for identify which button is click.

function modulename_form_submit(&$form, FormStateInterface $form_state) {

  $myurl = FALSE;
  $triggerElement = $form_state->getTriggeringElement();

  if (isset($triggerElement['#id']) && $triggerElement['#id'] == 'COMPARE BUTTON ID') {
    $myurl = Url::fromUri('internal:/SETYOURFIRSTACTIONPATH');
  }

  if (isset($triggerElement['#id']) && $triggerElement['#id'] == 'COMPARE ANOTHER BUTTON ID') {
    $myurl = Url::fromUri('internal:SETYOURSECONDACTIONPATH');
  }

  if ($myurl) {
    $form_state->setRedirectUrl($myurl);
  }
}
1
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',
      '#name' => 'step_1',
      '#value' => t('Go to Step 1'),
      '#submit' => array('custom_navigation'),
    );
    $form['actions']['submit_button_step_2'] = array(
      '#type' => 'submit',
      '#name' => 'step_2',
      '#value' => t('Go to Step 2'),
      '#submit' => array('custom_navigation'),
    );
  }
}

function custom_navigation(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  print $form_state->getTriggeringElement()['#name'];
}

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.