You are here

function user_roles in Drupal 7

Same name and namespace in other branches
  1. 8 core/modules/user/user.module \user_roles()
  2. 4 modules/user.module \user_roles()
  3. 5 modules/user/user.module \user_roles()
  4. 6 modules/user/user.module \user_roles()
  5. 9 core/modules/user/user.module \user_roles()
  6. 10 core/modules/user/user.module \user_roles()

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.

19 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 2951
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;
}