public function AccountSwitcher::switchTo

public AccountSwitcher::switchTo(AccountInterface $account)

Safely switches to another account.

Each invocation of AccountSwitcherInterface::switchTo() must be matched by a corresponding invocation of AccountSwitcherInterface::switchBack() in the same function.

Parameters

\Drupal\Core\Session\AccountInterface $account: The account to switch to.

Return value

\Drupal\Core\Session\AccountSwitcherInterface $this.

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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Session!AccountSwitcher.php/function/AccountSwitcher::switchTo/8.1.x