Private Tempstore - Store data on Drupal 9 session | Drupal 8

Private Tempstore - Store data on Drupal 9 session

Submitted by editor on Thu, 11/26/2020 - 11:32
Question

How to stores and retrieves temporary data on session.

This method allow to store data on drupal session using drupal session table (not php $_SESSION).
Storage is different for each users ans each visitors if anonymous

Store data

$tempstore = \Drupal::service('tempstore.private')->get('mymodule');
$tempstore->set('var_name', 'Just test');

Get data

$tempstore = \Drupal::service('tempstore.private')->get('mymodule');
$var = $tempstore->get('var_name');

Delete data

$tempstore = \Drupal::service('tempstore.private')->get('mymodule');
$tempstore->delete('var_name');

CAUTION : The user.private_tempstore and user.shared_tempstore are now deprecated.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.