#! code: Drupal 9: Using The Private Temporary Store Service

Author:
Source

Sponsored:

Atlas of AI: Power, Politics, and the Planetary Costs of Artificial Intelligence - Audiobook


Uncover the true cost of artificial intelligence.

"Atlas of AI" by Kate Crawford exposes how power, politics, and profit extract from our planet, our labor, and our freedom.

From hidden mines to massive data empires, discover how AI is reshaping who we are—and who holds control.

Listen now, and see the system behind the screens before the future listens to you. = > Atlas of AI $0.00 with trial. Read by Larissa Gallagher


The Drupal tempstore.private service is used to allow temporary user data that is available from one web request to the next. It is intended to be used for non-cache data that cannot easily be rebuild. This includes work in progress data that isn’t in the position to be saved permanently.

The temporary store is a key value/store and cam therefore store anything from a single vale to a serialised object.

The tempstore.private service is really a factory (called PrivateTempStoreFactory) that will allow you to create instance of a PrivateTempStore object. It’s this object that van be used to manage the data in the store. If you are familiar with the way that configuration factories work then this will seem familiar.

Using the temporary storage is quite straightforward, the service has a get() method that takes the name of the temporary store you want to use. What you call it is up to you, but it is best to namespace this so that you can easily tell where the temporary store came from. You can also add information like whatsort of temporay store you are using, but don’t add any user identifyable information for the key.

Once the PrivateTempStore object has been created you can then use it to set whatever data you might want to set.

/** @var DrupalCoreTempStorePrivateTempStore $store */
$store = Drupal::service('tempstore.private')->get('mymodule');
$store->set('var_name', $data);

To get the data back again just use the get() method.

Read more.

Read more