How to get user permissions for the role Administrator? I need to get permissions for all user roles, which are used. The code looks something like this:

$anonymous = Role::load('anonymous');
$anonymousPermissions = $anonymous-&gt;getPermissions();


The code for anonymous (and also for authenticated) returns an array of permissions, but the same code for administrator returns null:

$administrator = Role::load('administrator');


How can I get permissions for the role Administrator? (The machine name of role Administrator is administrator and also in the database is nothing suspicious.)



I have tried also:

$administratorPermissions = user_role_permissions(['administrator']);


It returns:

array(1) {
    ["administrator"] =&gt;
        array(0) {
    }
}




If I try:

$roles = Role::loadMultiple();
foreach ($roles as $role =&gt; $rolesObj) {
    $role_list[$role] = $rolesObj-&gt;get('label');
}
print_r($role_list);


Then I get:

Array
(
    [anonymous] =&gt; Anonymous user
    [authenticated] =&gt; Authenticated user
)
See the Role::getPermissions() method code:

  /**
   * Returns a list of permissions assigned to the role.
   *
   * @return array
   *   The permissions assigned to the role.
   */
  public function getPermissions() {
    if ($this-&gt;isAdmin()) {
      return [];
    }
    return $this-&gt;permissions;
  }


So, if the user has all permissions the returned value is an empty array.

Why? I really don't know and I can't find the answer, maybe other user knows Why Role::getPermissions() method return an empty array for admin users?.

The good news is that you can get all the Administrator permission with this code:

array_keys(\Drupal::service('user.permissions')-&gt;getPermissions());


Test this code (You'll need the Devel module to use dpm()):

$administrator = Role::load('administrator');
if($administrator-&gt;isAdmin()) {
  dpm(array_keys(\Drupal::service('user.permissions')-&gt;getPermissions()));
}