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;
  }
}

Riadh Rahmi

Senior Web Developer (Drupal & Laravel)

I am a senior web developer, I have experience in planning and developing large scale dynamic web applications especially in Drupal and Laravel.

Web Posts

Search

Page Facebook