User::addRole

public User::addRole($rid)

Add a role to a user.

Parameters

string $rid: The role ID to add.

Overrides UserInterface::addRole

File

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

Class

User
Defines the user entity class.

Namespace

Drupal\user\Entity

Code

1
2
3
4
5
6
7
8
9
10
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));
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.