user_roles

5 user.module user_roles($membersonly = 0, $permission = 0)
6 user.module user_roles($membersonly = FALSE, $permission = NULL)
7 user.module user_roles($membersonly = FALSE, $permission = NULL)
8 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.

14 calls to user_roles()

File

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

Code

function user_roles($membersonly = FALSE, $permission = NULL) {
  // System roles take the first two positions.
  $roles = array(
    DRUPAL_ANONYMOUS_RID => NULL, 
    DRUPAL_AUTHENTICATED_RID => NULL,
  );

  if (!empty($permission)) {
    $result = db_query("SELECT r.* FROM {role} r INNER JOIN {permission} p ON r.rid = p.rid WHERE p.perm LIKE '%%%s%%' ORDER BY r.name", $permission);
  }
  else {
    $result = db_query('SELECT * FROM {role} ORDER BY name');
  }

  while ($role = db_fetch_object($result)) {
    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;
    }
  }

  // Filter to remove unmatched system roles.
  return array_filter($roles);
}

Comments

SORT BY rid... NOT name!

I am unclear as to why this is not sorted by rid, which would support hierarchies
being crystal clear on the admin/user/roles and admin/user/permissions pages.

*How does alphabetizing role names help us?*

I suggest:

if (!empty($permission)) {
$result = db_query("SELECT r.* FROM {role} r INNER JOIN {permission} p
ON r.rid = p.rid WHERE p.perm LIKE '%%%s%%' ORDER BY
r.rid", $permission);
}
else {
$result = db_query('SELECT * FROM {role} ORDER BY
rid');
}

Thanks!!

Jeremy

urbanspectra.com/resume

Instead of submitting a comment...

you should file an issue. No one will notice your message here.

Actually, why don't we just add a weight? The RID is a little static…you can't change the order once you've set it with the RID.

Why are only the names for the built in roles translated?

      // We only translate the built in role names

Why is that? Otherwise you would have a change to show non-English speaking users a more comprehensible name.

So I hacked this function in my copy of Drupal, to translate all role names.

That is because the base

That is because the base translation system isn't designed for dynamic content, only static. In other words the system built into core (provided by the t() function is only designed to localize the interface of Drupal, not the translation of user generated content (which is what additional roles are.

See the documentation for t() at http://api.drupal.org/api/drupal/includes%21common.inc/function/t/6, especially this section:

Because t() is designed for handling code-based strings, in almost all cases, the actual string and not a variable must be passed through t().

Extraction of translations is done based on the strings contained in t() calls. If a variable is passed through t(), the content of the variable cannot be extracted from the file for translation.

And this is the exception that allows the usage in user_roles:

The only case in which variables can be passed safely through t() is when code-based versions of the same strings will be passed through t() (or otherwise extracted) elsewhere.

Login or register to post comments