class DemoBase
Implements common submit handler used in form_api_example demo forms.
We extend FormBase, which is the simplest form base class used in Drupal, to add a common submitForm method that will display the submitted values via the Messenger service.
Hierarchy
- class \Drupal\form_api_example\Form\DemoBase extends \Drupal\Core\Form\FormBase
Expanded class hierarchy of DemoBase
See also
\Drupal\Core\Form\FormBase
File
-
modules/
form_api_example/ src/ Form/ DemoBase.php, line 17
Namespace
Drupal\form_api_example\FormView source
abstract class DemoBase extends FormBase {
/**
* Implements a form submit handler.
*
* @param array $form
* The render array of the currently built form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Object describing the current state of the form.
*/
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) {
$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);
}
}
}
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overrides |
|---|---|---|---|---|
| DemoBase::submitForm | public | function | Implements a form submit handler. | 2 |