You are here

public function User::addRole in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::addRole()
  2. 9 core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::addRole()

File

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

Class

User
Defines the user entity class.

Namespace

Drupal\user\Entity

Code

public function addRole($rid) {
  if (in_array($rid, [
    RoleInterface::AUTHENTICATED_ID,
    RoleInterface::ANONYMOUS_ID,
  ])) {
    throw new \InvalidArgumentException('Anonymous or authenticated role ID must not be assigned manually.');
  }
  $roles = $this
    ->getRoles(TRUE);
  $roles[] = $rid;
  $this
    ->set('roles', array_unique($roles));
}