function SharedTempStoreFactory::get

Same name in other branches
  1. 8.9.x core/modules/user/src/SharedTempStoreFactory.php \Drupal\user\SharedTempStoreFactory::get()
  2. 8.9.x core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php \Drupal\Core\TempStore\SharedTempStoreFactory::get()
  3. 10 core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php \Drupal\Core\TempStore\SharedTempStoreFactory::get()
  4. 11.x core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php \Drupal\Core\TempStore\SharedTempStoreFactory::get()

Creates a SharedTempStore for the current user or anonymous session.

Parameters

string $collection: The collection name to use for this key/value store. This is typically a shared namespace or module name, e.g. 'views', 'entity', etc.

mixed $owner: (optional) The owner of this SharedTempStore. By default, the SharedTempStore is owned by the currently authenticated user, or by the active anonymous session if no user is logged in.

Return value

\Drupal\Core\TempStore\SharedTempStore An instance of the key/value store.

File

core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php, line 96

Class

SharedTempStoreFactory
Creates a shared temporary storage for a collection.

Namespace

Drupal\Core\TempStore

Code

public function get($collection, $owner = NULL) {
    // Use the currently authenticated user ID or the active user ID unless
    // the owner is overridden.
    if (!isset($owner)) {
        $owner = $this->currentUser
            ->id();
        if ($this->currentUser
            ->isAnonymous()) {
            $owner = Crypt::randomBytesBase64();
            if ($this->requestStack
                ->getCurrentRequest()
                ->hasSession()) {
                // Store a random identifier for anonymous users if the session is
                // available.
                $owner = $this->requestStack
                    ->getCurrentRequest()
                    ->getSession()
                    ->get('core.tempstore.shared.owner', $owner);
            }
        }
    }
    // Store the data for this collection in the database.
    $storage = $this->storageFactory
        ->get("tempstore.shared.{$collection}");
    return new SharedTempStore($storage, $this->lockBackend, $owner, $this->requestStack, $this->currentUser, $this->expire);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.