I'm trying to switch user session thanks to Drupal Switch. I want to do this so that when I have a certain role, I can switch to another user account to check some data from the user i choose to switch.
My routing :
double_vue.page_double_vue:
path: '/double-vue'
defaults:
_controller: '\Drupal\double_vue\Controller\DoubleVueController::doubleVue'
_title: 'Double vue'
requirements:
_permission: 'access content'
double_vue.switched_user:
path: '/double-vue/{userSwitchedID}'
defaults:
_controller: '\Drupal\double_vue\Controller\DoubleVueController::switchUser'
_title: 'UserSwitched'
requirements:
_permission: 'access content'
My view :
<a class="btn btn-primary btn-xs" href="{{ path('double_vue.switched_user', { 'userSwitchedID': "15" }) }}">switch</a>
My function :
public function switchUser($userSwitchedID){
$accountSwitcher = \Drupal::service('account_switcher');
$account = \Drupal\user\Entity\User::load($userSwitchedID);
return $accountSwitcher->switchTo($account);
}
When I click on my to link to switch to another user, I have this error in my logs :
The controller must return a response (Object(Drupal\Core\Session\AccountSwitcher) given)
If I remove the return, I have this :
The controller must return a response (null given)
How can I fix that so that I can switch to another user account ?