Oct 27, 2020 in Drupal 8

Getting the current user role in code is quite easy, here's the snippet.

$roles = \Drupal::currentUser()->getRoles();

if(in_array($roles, 'editor')) {
  // Do something if user has role editor
}

 

Comments