You are here

public function DependentDropdown::submitForm in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/ajax_example/src/Form/DependentDropdown.php \Drupal\ajax_example\Form\DependentDropdown::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

ajax_example/src/Form/DependentDropdown.php, line 159

Class

DependentDropdown
Re-populate a dropdown based on form state.

Namespace

Drupal\ajax_example\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $trigger = (string) $form_state
    ->getTriggeringElement()['#value'];
  switch ($trigger) {
    case 'Submit':

      // Submit: We're done.
      $this
        ->messenger()
        ->addMessage($this
        ->t('Your values have been submitted. Instrument family: @family, Instrument: @instrument', [
        '@family' => $form_state
          ->getValue('instrument_family_dropdown'),
        '@instrument' => $form_state
          ->getValue('instrument_dropdown'),
      ]));
      return;
  }

  // 'Choose' or anything else will cause rebuild of the form and present
  // it again.
  $form_state
    ->setRebuild();
}