How to get the currently logged in user's roles in Drupal 8

/** 
* Get current loged user's roles
*/ 
$current_user = \Drupal::currentUser(); 
$roles = $current_user->getRoles();
 //Will return an array like this:
 Array (
 [0]=> authenticated,
 [1]=> administrator,
 [2]=> editor,
 [3]=> developer 
);

 

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.