Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

The TempStore API allows temporary data to be stored so that it persists across multiple requests.

  • TempStore data expires automatically after a certain amount of time (e.g. a week).
  • The TempStore is not a cache, because the data in it cannot be rebuilt.
  • It should be used for work in progress that is not ready to be saved permanently, for example, for multistep forms, previewing changes, etc.

Usage

// Fetch or create the my_module data collection in the default temporary storage.
$my_tempstore = Drupal::service('user.tempstore')->get('my_module');

// Retrieve a piece of data from the tempstore.
$data = $my_tempstore->get('foo');

// Update an item in the tempstore, overwriting any previous data for it.
$my_tempstore->set('foo', $data);

// Update an item only if it is not already stored.
if (!$my_tempstore->setIfNotExists('bar', $data)) {
  // See who owns the data and inform the user.
  $owner = $my_tempstore->getMetadata('bar')->owner;
  drupal_set_message(t('%key is already being edited by %owner.', array(
    '%key' => 'bar',
    '%owner' => $owner,
  ), 'warning');
}
Impacts: 
Module developers