class PrivateTempStore (View source)

Stores and retrieves temporary data for a given owner.

A PrivateTempStore can be used to make temporary, non-cache data available across requests. The data for the PrivateTempStore is stored in one key/value collection. PrivateTempStore data expires automatically after a given timeframe.

The PrivateTempStore is different from a cache, because the data in it is not yet saved permanently and so it cannot be rebuilt. Typically, the PrivateTempStore might be used to store work in progress that is later saved permanently elsewhere, e.g. autosave data, multistep forms, or in-progress changes to complex configuration that are not ready to be saved.

The PrivateTempStore differs from the SharedTempStore in that all keys are ensured to be unique for a particular user and users can never share data. If you want to be able to share data between users or use it for locking, use \Drupal\Core\TempStore\SharedTempStore.

Traits

Provides dependency injection friendly methods for serialization.

Properties

protected array $_serviceIds

An array of service IDs keyed by property name used for serialization.

from  DependencySerializationTrait
protected array $_entityStorages

An array of entity type IDs keyed by the property name of their storages.

from  DependencySerializationTrait
protected KeyValueStoreExpirableInterface $storage

The key/value storage object used for this data.

protected LockBackendInterface $lockBackend

The lock object used for this data.

protected AccountProxyInterface $currentUser

The current user.

protected RequestStack $requestStack

The request stack.

protected int $expire

The time to live for items in seconds.

Methods

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

__construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lock_backend, AccountProxyInterface $current_user, RequestStack $request_stack, int $expire = 604800)

Constructs a new object for accessing data from a key/value store.

mixed
get(string $key)

Retrieves a value from this PrivateTempStore for a given key.

set(string $key, mixed $value)

Stores a particular key/value pair in this PrivateTempStore.

Lock|null
getMetadata(string $key)

Returns the metadata associated with a particular key/value pair.

bool
delete(string $key)

Deletes data from the store for a given key and releases the lock on it.

string
createkey(string $key)

Ensures that the key is unique for a user.

string
getOwner()

Gets the current owner based on the current user or the session ID.

startSession()

Start session because it is required for a private temp store.

Details

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

__construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lock_backend, AccountProxyInterface $current_user, RequestStack $request_stack, int $expire = 604800)

Constructs a new object for accessing data from a key/value store.

Parameters

KeyValueStoreExpirableInterface $storage

The key/value storage object used for this data. Each storage object represents a particular collection of data and will contain any number of key/value pairs.

LockBackendInterface $lock_backend

The lock object used for this data.

AccountProxyInterface $current_user

The current user account.

RequestStack $request_stack

The request stack.

int $expire

The time to live for items, in seconds.

mixed get(string $key)

Retrieves a value from this PrivateTempStore for a given key.

Parameters

string $key

The key of the data to retrieve.

Return Value

mixed

The data associated with the key, or NULL if the key does not exist.

set(string $key, mixed $value)

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.

Exceptions

TempStoreException

Lock|null getMetadata(string $key)

Returns the metadata associated with a particular key/value pair.

Parameters

string $key

The key of the data to store.

Return Value

Lock|null

An object with the owner and updated time if the key has a value, or NULL otherwise.

bool delete(string $key)

Deletes data from the store for a given key and releases the lock on it.

Parameters

string $key

The key of the data to delete.

Return Value

bool

TRUE if the object was deleted or does not exist, FALSE if it exists but is not owned by $this->owner.

Exceptions

TempStoreException

protected string createkey(string $key)

Ensures that the key is unique for a user.

Parameters

string $key

The key.

Return Value

string

The unique key for the user.

protected string getOwner()

Gets the current owner based on the current user or the session ID.

Return Value

string

The owner.

protected startSession()

Start session because it is required for a private temp store.

Ensures that an anonymous user has a session created for them, as otherwise subsequent page loads will not be able to retrieve their tempstore data.

when https://www.drupal.org/node/2865991 is resolved, use force start session API.