You are here

class MultistepController in Simple multi step form 8

Same name and namespace in other branches
  1. 8.x src/MultistepController.php \Drupal\simple_multistep\MultistepController

Class MultistepController.

@package Drupal\simple_multistep

Hierarchy

Expanded class hierarchy of MultistepController

1 file declares its use of MultistepController
simple_multistep.module in ./simple_multistep.module
Contains simple_multistep.module.

File

src/MultistepController.php, line 12

Namespace

Drupal\simple_multistep
View source
class MultistepController extends FormStep {

  /**
   * Steps indicator.
   *
   * @var StepIndicator
   */
  public $stepIndicator;

  /**
   * Form button.
   *
   * @var FormButton
   */
  public $formButton;

  /**
   * Stored values from $form_state.
   *
   * @var array
   */
  protected $storedValues;

  /**
   * Input values from $form_state.
   *
   * @var array
   */
  protected $inputValues;

  /**
   * MultistepController constructor.
   *
   * @param array $form
   *   Form settings.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state object.
   */
  public function __construct(array &$form, FormStateInterface $form_state) {
    parent::__construct($form, $form_state);

    // Initialize empty storage.
    $this->inputValues = [];
    $this->storedValues = [];
  }

  /**
   * Save input values from current step.
   */
  public function saveInputValues() {
    $stored_input = $this->inputValues;
    $stored_input[$this->currentStep] = $this->formState
      ->getUserInput();
    $this->inputValues = $stored_input;
  }

  /**
   * Get input values.
   */
  public function getInputValues() {
    return $this->inputValues;
  }

  /**
   * Save stored values from current step.
   */
  public function saveStoredValues() {
    $stored_values = $this->storedValues;
    $stored_values[$this->currentStep] = $this
      ->getStepValues($this->steps[$this->currentStep]);
    $this->storedValues = $stored_values;
  }

  /**
   * Get stored values.
   */
  public function getStoredValues() {
    return $this->storedValues;
  }

  /**
   * Prepare Multistep Form.
   *
   * @param array $form
   *   Reference to form.
   */
  public function rebuildForm(array &$form) {

    // Add step indicator.
    $this->stepIndicator = new StepIndicator($form, $this->formState, $this->currentStep);
    $this->stepIndicator
      ->render($form);
    unset($form['actions']['next']['#limit_validation_errors']);
    foreach ($this->steps as $key => $step) {
      $all_children = $this
        ->getAllChildren($step);
      if (!empty($all_children)) {

        // Another step.
        if ($key != $this->currentStep) {
          foreach ($all_children as $child_id) {
            if (isset($form[$child_id])) {
              if ($this->currentStep != count($this->steps) - 1) {
                unset($form[$child_id]);
              }
              else {
                $form[$child_id]['#access'] = FALSE;

                // @todo need found solution with password.
                if ($child_id == 'account' && isset($form[$child_id]['pass'])) {
                  $form[$child_id]['pass']['#required'] = FALSE;
                }
              }
            }
          }
        }
        else {
          foreach ($all_children as $child_id) {
            if (isset($form[$child_id])) {
              $form['actions']['next']['#limit_validation_errors'][] = [
                $child_id,
              ];
            }
          }
        }
      }
    }

    // Last step.
    if ($this->currentStep == count($this->steps) - 1) {
      foreach ($form as $form_element) {
        if (is_array($form_element) && isset($form_element['#type'])) {
          if (isset($form['actions']['next']['#limit_validation_errors'])) {
            unset($form['actions']['next']['#limit_validation_errors']);
          }
        }
      }
    }

    // Add additional button for form.
    $this->formButton = new FormButton($form, $this->formState, $this->currentStep);
    $this->formButton
      ->render($form);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormStep::$currentStep protected property Current step.
FormStep::$form protected property Form array.
FormStep::$formState protected property Form state.
FormStep::$steps protected property Steps.
FormStep::$stepSettings protected property Step settings.
FormStep::fetchSteps protected function Get array with form steps.
FormStep::fetchStepSettings protected function Fetch form step settings by current step.
FormStep::getAllChildren protected function Get all child from field group.
FormStep::getCurrentStep public function Get current step.
FormStep::getSteps public function Get all form steps.
FormStep::getStepSettings public function Get form step settings.
FormStep::getStepValues public function Get submission values for current step.
FormStep::increaseStep public function Increase step number.
FormStep::reduceStep public function Reduce step number.
FormStep::setCurrentStep protected function Set current step.
FormStep::setFormState public function Set $form_state.
FormStep::sortStep protected static function Sort array by object property.
FormStep::updateStepInfo public function Update step info.
MultistepController::$formButton public property Form button.
MultistepController::$inputValues protected property Input values from $form_state.
MultistepController::$stepIndicator public property Steps indicator.
MultistepController::$storedValues protected property Stored values from $form_state.
MultistepController::getInputValues public function Get input values.
MultistepController::getStoredValues public function Get stored values.
MultistepController::rebuildForm public function Prepare Multistep Form.
MultistepController::saveInputValues public function Save input values from current step.
MultistepController::saveStoredValues public function Save stored values from current step.
MultistepController::__construct public function MultistepController constructor. Overrides FormStep::__construct