User::getRoles

public User::getRoles($exclude_locked_roles = FALSE)

Returns a list of roles.

Parameters

bool $exclude_locked_roles: (optional) If TRUE, locked roles (anonymous/authenticated) are not returned.

Return value

array List of role IDs.

Overrides AccountInterface::getRoles

File

core/modules/user/src/Entity/User.php, line 142

Class

User
Defines the user entity class.

Namespace

Drupal\user\Entity

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public function getRoles($exclude_locked_roles = FALSE) {
  $roles = array();
 
  // Users with an ID always have the authenticated user role.
  if (!$exclude_locked_roles) {
    if ($this->isAuthenticated()) {
      $roles[] = RoleInterface::AUTHENTICATED_ID;
    }
    else {
      $roles[] = RoleInterface::ANONYMOUS_ID;
    }
  }
 
  foreach ($this->get('roles') as $role) {
    if ($role->target_id) {
      $roles[] = $role->target_id;
    }
  }
 
  return $roles;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.