I am trying to use the Passwordless module API to redirect the user on login.
I want to redirect the user to a specific node edit page (node/12/edit).
In hook_passwordless_login_redirect()
, the module redirects the user with this code:
return $this->redirect($redirect);
So here is my implementation:
function MYMODULE_passwordless_login_redirect_alter(&$redirect) {
// You can use hasPermission() with currentUser, but need to load for hasRole()
$account = \Drupal\user\Entity\user::load(\Drupal::currentUser()->id());
$test = \Drupal::service('path.validator')->getUrlIfValid('node/12/edit');
if (!($account->hasRole('admin'))) {
$redirect= $test->getRouteName();
}
The problem is that I get this error:
Some mandatory parameters are missing ("node") to generate a URL for route "entity.node.edit_form".
The missing route parameter is the node id, which I can get with $test->getRouteParameters()
. But how do I pass this to the redirect? (How do I call the redirect and give it both the route name and the route parameters?)