simple_multistep-8.x-1.x-dev/src/MultistepController.php

src/MultistepController.php
<?php

namespace Drupal\simple_multistep;

/**
 * Provides controller routines for Multistep forms.
 *
 * @package Drupal\simple_multistep
 */
class MultistepController extends FormStep implements MultistepControllerInterface {

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

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

  /**
   * Form text.
   *
   * @var \Drupal\simple_multistep\FormText
   */
  public FormText $formText;

  /**
   * {@inheritdoc}
   */
  public function rebuildForm(array &$form): void {
    $this->addStepIndicator($form);
    $this->addButtons($form);
    $this->addFormText($form);
    $this->processSteps($form);
  }

  /**
   * Add step indicator.
   *
   * @param array $form
   *   Reference to form array.
   */
  protected function addStepIndicator(array &$form): void {
    // Add step indicator.
    $this->stepIndicator = new StepIndicator($form, $this->formState, $this->currentStep);
    $this->stepIndicator->render($form);
  }

  /**
   * Add buttons.
   *
   * @param array $form
   *   Reference to form array.
   */
  protected function addButtons(array &$form): void {
    // Add additional button for form.
    $this->formButton = new FormButton($form, $this->formState, $this->currentStep);
    $this->formButton->render($form);
  }

  /**
   * Add form text.
   *
   * @param array $form
   *   Reference to form array.
   */
  protected function addFormText(array &$form): void {
    // Add form text.
    $this->formText = new FormText($form, $this->formState, $this->currentStep);
    $this->formText->render($form);
  }

  /**
   * Process steps.
   *
   * @param array $form
   *   Reference to form array.
   */
  protected function processSteps(array &$form): void {
    foreach ($this->steps as $key => $step) {
      $is_current_step = $key === $this->currentStep;

      foreach ($this->getAllChildren($step) as $child_id) {
        if ($is_current_step && isset($form[$child_id])) {
          $form['actions']['next']['#limit_validation_errors'][] = [$child_id];
        }

        if (!$is_current_step) {
          if (isset($form[$child_id])) {
            $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;
            }
          }
          elseif (isset($form['#fieldgroups'][$child_id])) {
            // Hide nested field groups.
            $form['#fieldgroups'][$child_id]->format_type = 'hidden';
          }
        }
      }
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc