Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / TempStore / PrivateTempStore.php
index f9d10347050450e388e790deeb425aeaa01cfde3..ea7ec3fc3725d655e223167106f9aacc3a64a3b0 100644 (file)
@@ -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();
+    }
   }
 
 }