Save a temporary data - Key Value Expirable. | Drupal 8

Save a temporary data - Key Value Expirable.

Submitted by editor on Fri, 02/09/2018 - 13:53
Question

How to save a variable temporarily on drupal 8 ?
How to use keyvalue.expirable service ?

 

keyvalue.expirable service allow to save Key / Value variable in the database.

Example:

// Save without date limit.
\Drupal::service('keyvalue.expirable')->get('my_module')->set('my_var', 123);

// Save for 1 day.
\Drupal::service('keyvalue.expirable')->get('my_module')->setWithExpire('time', time(), 86400);

Read data:
$my_var = \Drupal::service('keyvalue.expirable')->get('my_module')->set('my_var', $default_value);

Example 2:
$keyvalues = \Drupal::service('keyvalue.expirable')->get('my_module');
$keyvalues->set('name', 'The name');
$name = $keyvalues->get('name');

 

Tags

Add new comment