function AccountSwitcher::switchTo

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Session/AccountSwitcher.php \Drupal\Core\Session\AccountSwitcher::switchTo()
  2. 10 core/lib/Drupal/Core/Session/AccountSwitcher.php \Drupal\Core\Session\AccountSwitcher::switchTo()
  3. 11.x core/lib/Drupal/Core/Session/AccountSwitcher.php \Drupal\Core\Session\AccountSwitcher::switchTo()

Overrides AccountSwitcherInterface::switchTo

File

core/lib/Drupal/Core/Session/AccountSwitcher.php, line 58

Class

AccountSwitcher
An implementation of AccountSwitcherInterface.

Namespace

Drupal\Core\Session

Code

public function switchTo(AccountInterface $account) {
    // Prevent session information from being saved and push previous account.
    if (!isset($this->originalSessionSaving)) {
        // Ensure that only the first session saving status is saved.
        $this->originalSessionSaving = $this->writeSafeHandler
            ->isSessionWritable();
    }
    $this->writeSafeHandler
        ->setSessionWritable(FALSE);
    array_push($this->accountStack, $this->currentUser
        ->getAccount());
    $this->currentUser
        ->setAccount($account);
    return $this;
}

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