Skip to main content
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 PHP/Drupal & Laravel

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

Web Posts

Search

Page Facebook