SharedTempStore

Now that we've seen how the private tempstore works, let's look at the shared store. The first thing we need to do in order to interact with it is, as with the private one, use the factory to create a new shared store:

/** @var SharedTempStoreFactory $factory */$factory = \Drupal::service('user.shared_tempstore');$store = $factory->get('my_module.my_collection');

However, unlike the private tempstore, we can pass a user identifier (ID or session ID) as a second parameter to the get() method to retrieve the shared store of a particular owner. If we don't, it defaults to the current user (logged in or anonymous).

Then, the simplest way we can store/read an entry is like before:

$store->set('my_key', 'my_value');$value = $store->get('my_key'); ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.