Get all user's roles | Drupal 8

Get all user's roles

Submitted by editor on Wed, 12/09/2015 - 17:00
Question

How to get all user's roles in Drupal 8 ?
How to get a user's roles ?
How to get the current user's roles ?
 

Get All roles:
    $roles = \Drupal\user\Entity\Role::loadMultiple();

Get a user's roles:
    $uid = 4; //The user ID
    $user = \Drupal\user\Entity\User::load($uid);
    $roles = $user->getRoles();

To get current user's roles :
    $userCurrent = \Drupal::currentUser();
    $user = Drupal\user\Entity\User::load($userCurrent->id());
    $roles = $user->getRoles();

Comments

Add new comment