function User::getRoles

Same name in other branches
  1. 9 core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::getRoles()
  2. 10 core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::getRoles()
  3. 11.x core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::getRoles()

Overrides AccountInterface::getRoles

4 calls to User::getRoles()
User::addRole in core/modules/user/src/Entity/User.php
Add a role to a user.
User::hasPermission in core/modules/user/src/Entity/User.php
Checks whether a user has a certain permission.
User::hasRole in core/modules/user/src/Entity/User.php
Whether a user has a certain role.
User::removeRole in core/modules/user/src/Entity/User.php
Remove a role from a user.

File

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

Class

User
Defines the user entity class.

Namespace

Drupal\user\Entity

Code

public function getRoles($exclude_locked_roles = FALSE) {
    $roles = [];
    // 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.