Use of Drupal TempStore API

How to use Drupal TempStore API in application

TempStore API - A key/value storage to keep temporary data (private or shared) across page requests.
  • Set value using Drupal service private_tempstore:

  $tempstore = \Drupal::service('user.private_tempstore')->get('modulename');
  $tempstore->set('key', 'Value to set');
  
  • Get value using Drupal service private_tempstore:

  $tempstore = \Drupal::service('user.private_tempstore')->get('modulename');
  $myKey = $tempstore->get('key');
  
  • By default it keep value stored a week long, while you can also delete value using delete function:

  $tempstore = \Drupal::service('user.private_tempstore')->get('modulename');
  $store->delete('key_name');
  

Comments

Popular posts from this blog

How to setup Drupal 8 Multisite on nginx webserver with different domain.

Install drush globally using composer on WSL

Drupal views create exposed filter programmatically