function SessionExampleForm::submitForm
Same name in other branches
- 3.x modules/session_example/src/Form/SessionExampleForm.php \Drupal\session_example\Form\SessionExampleForm::submitForm()
- 4.0.x modules/session_example/src/Form/SessionExampleForm.php \Drupal\session_example\Form\SessionExampleForm::submitForm()
File
-
session_example/
src/ Form/ SessionExampleForm.php, line 149
Class
- SessionExampleForm
- Form to allow the user to store information in their session.
Namespace
Drupal\session_example\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
// We get the submitted form information and store it in the session. We use
// key names which include our module name in order to avoid namespace
// collision.
$this->setSessionValue('session_example.name', $form_state->getValue('name'));
$this->setSessionValue('session_example.email', $form_state->getValue('email'));
$this->setSessionValue('session_example.quest', $form_state->getValue('quest'));
$this->setSessionValue('session_example.color', $form_state->getValue('color'));
// Tell the user what happened here, and that they can look at another page
// to see the result.
$this->messenger()
->addMessage($this->t('The session has been saved successfully. @link', [
'@link' => Link::createFromRoute('Check here.', 'session_example.view')->toString(),
]));
// Since we might have changed the session information, we will invalidate
// the cache tag for this session.
$this->invalidateCacheTag();
}