Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Redirect the user logged in based on user role in drupal 9

 

You can use hook_user_login  to redirect the user logged in based on user role in drupal 9:

 

/**
 * Implements hook_user_login().
 */
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;

function modulename_user_login($account) {
 $routeName = 'entity.node.canonical';
 $nodeId = 1;
 $routeParameters = ['node' => $nodeId];
 $url = \Drupal::url($routeName, $routeParameters);

  $roles = $account->getRoles();
  if(in_array('administrator', $roles)){
    $response = new RedirectResponse($url);
    $response->send();
    return;
  }
}

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon