You are here

public function SessionExampleForm::submitForm in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 session_example/src/Form/SessionExampleForm.php \Drupal\session_example\Form\SessionExampleForm::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

modules/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\Form

Code

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();
}