Working in Public: The Making and Maintenance of Open Source Software Audible Audiobook
Free with Audible trial
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.