AccountSwitcherTrait
Provides a helper to run code as another account.
- Full name:
\Drupal\helper\AccountSwitcherTrait
Methods
executeAsAccount
Executes a callback as another account.
protected executeAsAccount(\Drupal\Core\Session\AccountInterface|null $account, callable $callback): mixed
While the callback runs, the account is the current user, so cache contexts such as user.permissions resolve for it. The original account is restored afterwards, even if the callback throws an exception. The account isn't switched when it's already the current user.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$account |
\Drupal\Core\Session\AccountInterface|null | |
| The account, or NULL to run the callback as the current user. | ||
$callback |
callable | |
| The callback. |
Return Value:
The callback's return value.
See Also:
- \Drupal\Core\Session\AccessPolicyProcessor::processAccessPolicies()