Drupal 8 : How to get all available user roles in drupal 8.

The below code will return all the available user roles as an array in drupal 8.

$roles = array_map(['\Drupal\Component\Utility\Html', 'escape'], user_role_names(TRUE));

Sample result:

Array
(
    [authenticated] => Authenticated user
    [administrator] => Administrator
    [editor] => Editor
    [owner] => Owner
)

 

Category: