Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. User.php

function User::addRole

Overrides UserInterface::addRole

File

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

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));
    return $this;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal