You are here

public function InputDemo::submitForm in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/form_api_example/src/Form/InputDemo.php \Drupal\form_api_example\Form\InputDemo::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

form_api_example/src/Form/InputDemo.php, line 305

Class

InputDemo
Implements InputDemo form controller.

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 && $label) {
      $display_value = is_array($value) ? preg_replace('/[\\n\\r\\s]+/', ' ', print_r($value, 1)) : $value;
      $message = $this
        ->t('Value for %title: %value', [
        '%title' => $label,
        '%value' => $display_value,
      ]);
      $this
        ->messenger()
        ->addMessage($message);
    }
  }
}