function PrivateTempStore::startSession

Same name in other branches
  1. 9 core/lib/Drupal/Core/TempStore/PrivateTempStore.php \Drupal\Core\TempStore\PrivateTempStore::startSession()
  2. 8.9.x core/lib/Drupal/Core/TempStore/PrivateTempStore.php \Drupal\Core\TempStore\PrivateTempStore::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.

File

core/lib/Drupal/Core/TempStore/PrivateTempStore.php, line 236

Class

PrivateTempStore
Stores and retrieves temporary data for a given owner.

Namespace

Drupal\Core\TempStore

Code

protected function startSession() {
    @trigger_error(__METHOD__ . "() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3432359", E_USER_DEPRECATED);
    $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();
    }
}

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