You are here

public function MultistepController::rebuildForm in Simple multi step form 8

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

Prepare Multistep Form.

Parameters

array $form: Reference to form.

File

src/MultistepController.php, line 98

Class

MultistepController
Class MultistepController.

Namespace

Drupal\simple_multistep

Code

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