X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Flib%2FDrupal%2FCore%2FTempStore%2FPrivateTempStore.php;fp=web%2Fcore%2Flib%2FDrupal%2FCore%2FTempStore%2FPrivateTempStore.php;h=ea7ec3fc3725d655e223167106f9aacc3a64a3b0;hp=f9d10347050450e388e790deeb425aeaa01cfde3;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/lib/Drupal/Core/TempStore/PrivateTempStore.php b/web/core/lib/Drupal/Core/TempStore/PrivateTempStore.php index f9d103470..ea7ec3fc3 100644 --- a/web/core/lib/Drupal/Core/TempStore/PrivateTempStore.php +++ b/web/core/lib/Drupal/Core/TempStore/PrivateTempStore.php @@ -117,6 +117,19 @@ class PrivateTempStore { * Thrown when a lock for the backend storage could not be acquired. */ 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); @@ -207,7 +220,34 @@ class PrivateTempStore { * The owner. */ protected function getOwner() { - return $this->currentUser->id() ?: $this->requestStack->getCurrentRequest()->getSession()->getId(); + $owner = $this->currentUser->id(); + if ($this->currentUser->isAnonymous()) { + $this->startSession(); + $owner = $this->requestStack->getCurrentRequest()->getSession()->getId(); + } + return $owner; + } + + /** + * 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. + * + * @todo when https://www.drupal.org/node/2865991 is resolved, use force + * start session API. + */ + protected function startSession() { + $has_session = $this->requestStack + ->getCurrentRequest() + ->hasSession(); + if (!$has_session) { + /** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */ + $session = \Drupal::service('session'); + $this->requestStack->getCurrentRequest()->setSession($session); + $session->start(); + } } }