You are here

public function SessionExampleForm::submitClearSession 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::submitClearSession()

Remove all the session information.

File

modules/session_example/src/Form/SessionExampleForm.php, line 170

Class

SessionExampleForm
Form to allow the user to store information in their session.

Namespace

Drupal\session_example\Form

Code

public function submitClearSession(array &$form, FormStateInterface $form_state) {
  $items = [
    'session_example.name',
    'session_example.email',
    'session_example.quest',
    'session_example.color',
  ];
  foreach ($items as $item) {
    $this->session
      ->remove($item);
  }
  $this
    ->messenger()
    ->addMessage($this
    ->t('Session is cleared.'));

  // Since we might have changed the session information, we will invalidate
  // the cache tag for this session.
  $this
    ->invalidateCacheTag();
}