O'Reilly logo

Drupal 8 Module Development by Daniel Sipos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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'); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required