You are here

class FormButton in Simple multi step form 8

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

Class FormButton.

@package Drupal\simple_multistep

Hierarchy

Expanded class hierarchy of FormButton

File

src/FormButton.php, line 12

Namespace

Drupal\simple_multistep
View source
class FormButton extends FormStep {

  /**
   * Constructor.
   *
   * @param array $form
   *   Form settings.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state object.
   * @param int $current_step
   *   Current step.
   */
  public function __construct(array $form, FormStateInterface $form_state, $current_step) {
    parent::__construct($form, $form_state);
    $this->currentStep = $current_step;
    $this
      ->fetchStepSettings();
  }

  /**
   * Show back button.
   *
   * @param array $form
   *   Reference to form array.
   */
  private function showBackButton(array &$form) {
    $step_format_settings = $this->stepSettings->format_settings;
    if ($this->currentStep != 0 && !empty($step_format_settings['back_button_show'])) {

      // Add back button and remove validation.
      $form['actions']['back_button'] = [
        '#type' => 'button',
        '#value' => $step_format_settings['back_button_text'],
        '#validate' => [
          'simple_multistep_register_back',
        ],
        '#submit' => [],
        '#limit_validation_errors' => [],
        '#weight' => 0,
      ];
    }
  }

  /**
   * Show next button.
   *
   * @param array $form
   *   Reference to form array.
   */
  private function showNextButton(array &$form) {
    $step_format_settings = $this->stepSettings->format_settings;
    if (count($this->steps) - 1 != $this->currentStep) {
      $form['actions']['next'] = [
        '#type' => 'button',
        '#value' => $step_format_settings['next_button_text'],
        '#validate' => [
          'simple_multistep_register_next_step',
        ],
        '#submit' => [],
        '#weight' => 0.1,
      ];
      $form['actions']['submit']['#access'] = FALSE;
    }
    else {
      $form['actions']['submit']['#access'] = TRUE;
      array_unshift($form['#validate'], 'simple_multistep_multistep_validate');
    }
  }

  /**
   * Render form button.
   *
   * @param array $form
   *   Form array.
   */
  public function render(array &$form) {
    $this
      ->showBackButton($form);
    $this
      ->showNextButton($form);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormButton::render public function Render form button.
FormButton::showBackButton private function Show back button.
FormButton::showNextButton private function Show next button.
FormButton::__construct public function Constructor. Overrides FormStep::__construct
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.