function InputDemo::submitForm

Same name in other branches
  1. 4.0.x modules/form_api_example/src/Form/InputDemo.php \Drupal\form_api_example\Form\InputDemo::submitForm()

Overrides FormInterface::submitForm

File

modules/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 = $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);
        }
    }
}