public function FormWidgetExampleForm::submitForm in Typed Data API enhancements 8
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
- tests/
modules/ typed_data_widget_test/ src/ FormWidgetExampleForm.php, line 162
Class
- FormWidgetExampleForm
- Class FormWidgetExampleForm.
Namespace
Drupal\typed_data_widget_testCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$context_definition = $form_state
->get('context_definition');
$widget_id = $form_state
->get('widget_id');
$widget = $this
->getFormWidgetManager()
->createInstance($widget_id);
if (($triggering_element = $form_state
->getTriggeringElement()) && $triggering_element['#id'] == 'edit-reset') {
// Erase the widget data.
$this->state
->set('typed_data_widgets.' . $widget_id, NULL);
$this
->messenger()
->addMessage($this
->t('Value reset to default'));
}
else {
$subform_state = SubformState::createWithParents([
'data',
], $form, $form_state);
$data = $this
->getTypedDataManager()
->create($context_definition
->getDataDefinition());
$widget
->extractFormValues($data, $subform_state);
// Read and write widget configuration via the state.
$this->state
->set('typed_data_widgets.' . $widget_id, $data
->getValue());
// Display the value saved. Use print_r in case the value is an array.
$this
->messenger()
->addMessage($this
->t('Value saved: %value', [
'%value' => print_r($data
->getValue(), TRUE),
]));
}
}