8.5.x user.module user_roles($membersonly = FALSE, $permission = NULL)
8.0.x user.module user_roles($membersonly = FALSE, $permission = NULL)
8.1.x user.module user_roles($membersonly = FALSE, $permission = NULL)
8.2.x user.module user_roles($membersonly = FALSE, $permission = NULL)
8.3.x user.module user_roles($membersonly = FALSE, $permission = NULL)
8.4.x user.module user_roles($membersonly = FALSE, $permission = NULL)
8.6.x user.module user_roles($membersonly = FALSE, $permission = NULL)
4.6.x user.module user_roles($membersonly = 0, $permission = 0)
4.7.x user.module user_roles($membersonly = 0, $permission = 0)
5.x user.module user_roles($membersonly = 0, $permission = 0)
6.x user.module user_roles($membersonly = FALSE, $permission = NULL)
7.x user.module user_roles($membersonly = FALSE, $permission = NULL)

Retrieve an array of roles matching specified conditions.

Parameters

$membersonly: Set this to TRUE to exclude the 'anonymous' role.

$permission: A string containing a permission. If set, only roles containing that permission are returned.

Return value

An associative array with the role id as the key and the role name as value.

18 calls to user_roles()
block_admin_configure in modules/block/block.admin.inc
Form constructor for the block configuration form.
field_update_7004 in modules/field/field.install
Grant the new "administer fields" permission to trusted users.
FilterFormatAccessTestCase::testFormatRoles in modules/filter/filter.test
Tests if text format is available to a role.
filter_admin_format_form in modules/filter/filter.admin.inc
Form constructor for the text format add/edit form.
filter_get_roles_by_format in modules/filter/filter.module
Retrieves a list of roles that are allowed to use a given text format.

... See full list

File

modules/user/user.module, line 2928
Enables the user registration and login system.

Code

function user_roles($membersonly = FALSE, $permission = NULL) {
  $query = db_select('role', 'r');
  $query
    ->addTag('translatable');
  $query
    ->fields('r', array(
    'rid',
    'name',
  ));
  $query
    ->orderBy('weight');
  $query
    ->orderBy('name');
  if (!empty($permission)) {
    $query
      ->innerJoin('role_permission', 'p', 'r.rid = p.rid');
    $query
      ->condition('p.permission', $permission);
  }
  $result = $query
    ->execute();
  $roles = array();
  foreach ($result as $role) {
    switch ($role->rid) {

      // We only translate the built in role names
      case DRUPAL_ANONYMOUS_RID:
        if (!$membersonly) {
          $roles[$role->rid] = t($role->name);
        }
        break;
      case DRUPAL_AUTHENTICATED_RID:
        $roles[$role->rid] = t($role->name);
        break;
      default:
        $roles[$role->rid] = $role->name;
    }
  }
  return $roles;
}

Comments

rogical’s picture

It shouldn't return a translated role name, as the user object doesn't return translated role name!

panche’s picture

Look at the comment:
// We only translate the built in role names

So it's ok to translate those.

stefgosselin’s picture

Actually, I feel it's sort of weird. One should be able to rely on a consistant role name regardless of enabled locales.

ker688@gmail.com’s picture

I agree, it make drush always throw exception when use "drush role-list 'anonymous user'" while the site's default language is not English.

See below drush source 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;
$this->name = $this->roles[$rid];
}
else {
throw new DrushRoleException(dt('Could not find the role: !role', array('!role' => $rid)));
}
}

And here is the link: https://github.com/drush-ops/drush/blob/6.x/classes/DrushRole.php#L29