class SharedTempStoreFactory

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

Creates a shared temporary storage for a collection.

Hierarchy

Expanded class hierarchy of SharedTempStoreFactory

8 files declare their use of SharedTempStoreFactory
BreakLockForm.php in core/modules/views_ui/src/Form/BreakLockForm.php
LayoutBuilderSampleEntityGenerator.php in core/modules/layout_builder/src/Entity/LayoutBuilderSampleEntityGenerator.php
LayoutTempstoreRepository.php in core/modules/layout_builder/src/LayoutTempstoreRepository.php
LayoutTempstoreRepositoryTest.php in core/modules/layout_builder/tests/src/Unit/LayoutTempstoreRepositoryTest.php
SharedTempStoreTest.php in core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php

... See full list

1 string reference to 'SharedTempStoreFactory'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses SharedTempStoreFactory
tempstore.shared in core/core.services.yml
Drupal\Core\TempStore\SharedTempStoreFactory

File

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

Namespace

Drupal\Core\TempStore
View source
class SharedTempStoreFactory {
    
    /**
     * The storage factory creating the backend to store the data.
     *
     * @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface
     */
    protected $storageFactory;
    
    /**
     * The lock object used for this data.
     *
     * @var \Drupal\Core\Lock\LockBackendInterface
     */
    protected $lockBackend;
    
    /**
     * The request stack.
     *
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    protected $requestStack;
    
    /**
     * The current user.
     *
     * @var \Drupal\Core\Session\AccountProxyInterface
     */
    protected $currentUser;
    
    /**
     * The time to live for items in seconds.
     *
     * @var int
     */
    protected $expire;
    
    /**
     * Constructs a Drupal\Core\TempStore\SharedTempStoreFactory object.
     *
     * @param \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $storage_factory
     *   The key/value store factory.
     * @param \Drupal\Core\Lock\LockBackendInterface $lock_backend
     *   The lock object used for this data.
     * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
     *   The request stack.
     * @param \Drupal\Core\Session\AccountProxyInterface $current_user
     *   The current user.
     * @param int $expire
     *   The time to live for items, in seconds.
     */
    public function __construct(KeyValueExpirableFactoryInterface $storage_factory, LockBackendInterface $lock_backend, RequestStack $request_stack, $current_user = NULL, $expire = 604800) {
        $this->storageFactory = $storage_factory;
        $this->lockBackend = $lock_backend;
        $this->requestStack = $request_stack;
        if (!$current_user instanceof AccountProxyInterface) {
            @trigger_error('Calling ' . __METHOD__ . '() without the $current_user argument is deprecated in drupal:9.2.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/3006268', E_USER_DEPRECATED);
            if (is_int($current_user)) {
                // If the $current_user argument is numeric then this object has been
                // instantiated with the old constructor signature.
                $expire = $current_user;
            }
            $current_user = \Drupal::currentUser();
        }
        $this->currentUser = $current_user;
        $this->expire = $expire;
    }
    
    /**
     * Creates a SharedTempStore for the current user or anonymous session.
     *
     * @param 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.
     * @param 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 \Drupal\Core\TempStore\SharedTempStore
     *   An instance of the key/value store.
     */
    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);
    }

}

Members

Title Sort descending Modifiers Object type Summary
SharedTempStoreFactory::$currentUser protected property The current user.
SharedTempStoreFactory::$expire protected property The time to live for items in seconds.
SharedTempStoreFactory::$lockBackend protected property The lock object used for this data.
SharedTempStoreFactory::$requestStack protected property The request stack.
SharedTempStoreFactory::$storageFactory protected property The storage factory creating the backend to store the data.
SharedTempStoreFactory::get public function Creates a SharedTempStore for the current user or anonymous session.
SharedTempStoreFactory::__construct public function Constructs a Drupal\Core\TempStore\SharedTempStoreFactory object.

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