1

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?)

  • 1
    This module is probably using UrlGeneratorTrait::redirect. Then you are out of luck, because the route parameters are the second parameter, which is not offered in the alter hook. – 4k4 Nov 7 '17 at 12:24
1

As pointed out by @4k4, the problem is that the module did not ask for the route parameters, so I patched it.

Then you can call the redirect like this:

  if (!($account->hasRole('admin'))) {
    $routename = $test->getRouteName();
    $routeparameters= $test->getRouteParameters();
  }
0

You can pass parameters like below:

$url = \Drupal\Core\Url::fromRoute(
    $test->getRouteName(),
     array(
       'query' => array('query1' => 'YOUR QUERY'),
       'absolute' => TRUE,
     )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.