public function PrivateTempStore::set in Drupal 8
Same name and namespace in other branches
- 9 core/lib/Drupal/Core/TempStore/PrivateTempStore.php \Drupal\Core\TempStore\PrivateTempStore::set()
- 10 core/lib/Drupal/Core/TempStore/PrivateTempStore.php \Drupal\Core\TempStore\PrivateTempStore::set()
Stores a particular key/value pair in this PrivateTempStore.
Parameters
string $key: The key of the data to store.
mixed $value: The data to store.
Throws
\Drupal\Core\TempStore\TempStoreException Thrown when a lock for the backend storage could not be acquired.
File
- core/
lib/ Drupal/ Core/ TempStore/ PrivateTempStore.php, line 121
Class
- PrivateTempStore
- Stores and retrieves temporary data for a given owner.
Namespace
Drupal\Core\TempStoreCode
public function set($key, $value) {
// Ensure that an anonymous user has a session created for them, as
// otherwise subsequent page loads will not be able to retrieve their
// tempstore data.
if ($this->currentUser
->isAnonymous()) {
// @todo when https://www.drupal.org/node/2865991 is resolved, use force
// start session API rather than setting an arbitrary value directly.
$this
->startSession();
$this->requestStack
->getCurrentRequest()
->getSession()
->set('core.tempstore.private', TRUE);
}
$key = $this
->createkey($key);
if (!$this->lockBackend
->acquire($key)) {
$this->lockBackend
->wait($key);
if (!$this->lockBackend
->acquire($key)) {
throw new TempStoreException("Couldn't acquire lock to update item '{$key}' in '{$this->storage->getCollectionName()}' temporary storage.");
}
}
$value = (object) [
'owner' => $this
->getOwner(),
'data' => $value,
'updated' => (int) $this->requestStack
->getMasterRequest()->server
->get('REQUEST_TIME'),
];
$this->storage
->setWithExpire($key, $value, $this->expire);
$this->lockBackend
->release($key);
}