March 2019
Intermediate to advanced
580 pages
15h 3m
English
Now that we've seen how the private tempstore works, let's look at the shared one. The first thing we need to do in order to interact with it is to use the factory to create a new shared store:
/** @var \Drupal\Core\TempStore\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'); ...Read now
Unlock full access