public function RoleBase::__construct

8.0.x RoleBase.php public RoleBase::__construct($rid = DRUPAL_ANONYMOUS_RID)
7.x RoleBase.php public RoleBase::__construct($rid = DRUPAL_ANONYMOUS_RID)
master RoleBase.php public RoleBase::__construct($rid = DRUPAL_ANONYMOUS_RID)

This constructor will allow the role to be selected either via the role id or via the role name.

File

lib/Drush/Role/RoleBase.php, line 31

Namespace

Drush\Role

Class

RoleBase

Code

public function __construct($rid = DRUPAL_ANONYMOUS_RID) {
  $this->roles = user_roles();
  if (!is_numeric($rid)) {
    $role_name = $rid;
    if (in_array($role_name, $this->roles)) {
      $rid = array_search($role_name, $this->roles);
    }
  }

  if (isset($this->roles[$rid])) {
    $this->rid = $rid;
    // In D8+ Role is an object.
    $this->name = is_object($this->roles[$rid]) ? $this->roles[$rid]->label() : $this->roles[$rid];
  }
  else {
    throw new RoleException(dt('Could not find the role: !role', array('!role' => $rid)));
  }
}