1

I have a form which has a pagination, where you can navigate between steps. The steps are looped in and the callback contains a trigger check and changes the step number.

Working scenario: On step one, i can manoeuvre by clicking on submit to navigate to second step, or use the pagination to navigate to step 2 or 3.

Non working scenario: When on step 2 (however i get there), i cant seem to navigate back or forward using the pagination. All i can do is click submit to get to the last stage.

I am not getting any error whatsoever, Not sure whether this is the best way or if someone has another.

<?php

namespace Drupal\example\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\user\PrivateTempStoreFactory;
use Drupal\user\Entity;

class ExampleForm extends FormBase {

  /**
   * @var Storage of the max number of steps.
   */
  protected $maxSteps = 3;

  /**
   * @var Storage of the multiform steps.
   */
  protected $step = 1;



  public function getFormID() {
    return 'form_id';
  }

  /**
   * {@inheritdoc}.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {


    $form['#prefix'] = '<div id="wizard-form-wrapper">';
    $form['#suffix'] = '</div>';

    $form['actions']['#type'] = 'actions';


    if($this->step != 3){

      $form['pagination_fieldset'] = [
        '#title' => t('Pagination'),
        '#type' => 'fieldset'
      ];

      for ($i=1; $i <= $this->maxSteps; $i++) {
        if($i != $this->step){
          $form['pagination_fieldset']['pagination_' . $i] = array(
            '#type' => 'submit',
            '#value' => $this->t('Page ' . $i),
            '#button_type' => 'primary',
            '#weight' => 20,
            '#submit' => array(array($this, 'backSubmitForm')),
            '#ajax' => array(
              'wrapper' => 'wizard-form-wrapper',
              'callback' => array($this, 'submitNextStep'),
              'event' => 'click',
              'progress' => array(
                'type' => 'throbber',
              ),
            )
          );
        }
        else {
          $form['pagination_fieldset']['pagination_' . $i] = array(
            '#type' => 'submit',
            '#value' => $this->t('Page ' . $i),
            '#button_type' => 'primary',
            '#weight' => 20,
            '#disabled' => TRUE,
          );
        }
      }
    }

    if($this->step == 1) {

      $form['full_name'] = array(
        '#type' => 'textfield',
        '#title' => $this->t('Full Name'),
        '#default_value' => $this->store->get('full_name') ? $this->store->get('full_name') : '',
      );
    }

    if($this->step == 2) {

      $form['age'] = array(
        '#type' => 'textfield',
        '#title' => $this->t('age'),
        '#default_value' => $this->store->get('age') ? $this->store->get('age') : '',
      );

    }

    if($this->step != 3) {

      $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => $this->t('Submit'),
        '#button_type' => 'primary',
        '#weight' => 20,
        '#ajax' => array(
          'wrapper' => 'wizard-form-wrapper',
          'callback' => array($this, 'submitNextStep'),
          'event' => 'click',
          'progress' => array(
            'type' => 'throbber',
          ),
        )
      );
    }

    return $form;
  }


  public function submitForm(array &$form, FormStateInterface $form_state) {


    switch ($this->step) {
      case 1:
        $this->store->set('full_name', $form_state->getValue('full_name'));
      break;

      case 2:
        $this->store->set('age', $form_state->getValue('age'));
      break;
    }


    if($this->step < $this->maxSteps) {
      $this->step++;
      $form_state->setRebuild(TRUE);
    }
    else {
      drupal_set_message('Your request has been submitted', 'success');
      $this->step = $this->maxSteps + 1;
      // $this->deleteStore();
      $form_state->setRebuild(TRUE);

    }

  }


  public function backSubmitForm(array &$form, FormStateInterface $form_state){
    $str = $form_state->getTriggeringElement()['#value'];
    $int = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
    $this->step = (int) $int;
    $form_state->setRebuild();
  }


  public function submitNextStep(array &$form, FormStateInterface $form_state) {
    return $form;
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.