You are here

public function DemoBase::submitForm in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 form_api_example/src/Form/DemoBase.php \Drupal\form_api_example\Form\DemoBase::submitForm()

Implements a form submit handler.

Parameters

array $form: The render array of the currently built form.

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

Overrides FormInterface::submitForm

2 methods override DemoBase::submitForm()
AjaxAddMore::submitForm in modules/form_api_example/src/Form/AjaxAddMore.php
Final submit handler.
StateDemo::submitForm in modules/form_api_example/src/Form/StateDemo.php
Implements submitForm callback.

File

modules/form_api_example/src/Form/DemoBase.php, line 27

Class

DemoBase
Implements common submit handler used in form_api_example demo forms.

Namespace

Drupal\form_api_example\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Find out what was submitted.
  $values = $form_state
    ->getValues();
  foreach ($values as $key => $value) {
    $label = isset($form[$key]['#title']) ? $form[$key]['#title'] : $key;

    // Many arrays return 0 for unselected values so lets filter that out.
    if (is_array($value)) {
      $value = array_filter($value);
    }

    // Only display for controls that have titles and values.
    if ($value) {
      $display_value = is_array($value) ? print_r($value, 1) : $value;
      $message = $this
        ->t('Value for %title: %value', [
        '%title' => $label,
        '%value' => $display_value,
      ]);
      $this
        ->messenger()
        ->addMessage($message);
    }
  }
}