function 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 object as value.

6 calls to user_roles()
EntityOperationsTest::testEntityOperationAlter in drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityOperationsTest.php
Checks that hook_entity_operation_alter() can add an operation.
FilterFormatStorageController::postSave in drupal/core/modules/filter/lib/Drupal/filter/FilterFormatStorageController.php
Overrides \Drupal\Core\Config\Entity\ConfigStorageController::postSave().
Roles::pre_render in drupal/core/modules/user/lib/Drupal/user/Plugin/views/field/Roles.php
Run before any fields are rendered.
theme_comment_post_forbidden in drupal/core/modules/comment/comment.module
Returns HTML for a "you can't post comments" notice.
UserRoleAdminTest::testRoleWeightOrdering in drupal/core/modules/user/lib/Drupal/user/Tests/UserRoleAdminTest.php
Test user role weight change operation and ordering.

... See full list

4 string references to 'user_roles'
UserRoleAdminTest::testRoleWeightOrdering in drupal/core/modules/user/lib/Drupal/user/Tests/UserRoleAdminTest.php
Test user role weight change operation and ordering.
user_views_data in drupal/core/modules/user/user.views.inc
Implements hook_views_data().
views.view.test_views_handler_field_role.yml in drupal/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_role.yml
drupal/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_role.yml
views.view.user_admin_people.yml in drupal/core/modules/user/config/views.view.user_admin_people.yml
drupal/core/modules/user/config/views.view.user_admin_people.yml

File

drupal/core/modules/user/user.module, line 1832
Enables the user registration and login system.

Code

function user_roles($membersonly = FALSE, $permission = NULL) {
  $user_roles =& drupal_static(__FUNCTION__);

  // Do not cache roles for specific permissions. This data is not requested
  // frequently enough to justify the additional memory use.
  if (empty($permission)) {
    $cid = $membersonly ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
    if (isset($user_roles[$cid])) {
      return $user_roles[$cid];
    }
  }
  $roles = entity_load_multiple('user_role');
  if ($membersonly) {
    unset($roles[DRUPAL_ANONYMOUS_RID]);
  }
  if (!empty($permission)) {
    $result = db_select('role_permission', 'p')
      ->fields('p', array(
      'rid',
    ))
      ->condition('p.rid', array_keys($roles))
      ->condition('p.permission', $permission)
      ->execute()
      ->fetchCol();
    $roles = array_intersect_key($roles, array_flip($result));
  }
  if (empty($permission)) {
    $user_roles[$cid] = $roles;
  }
  return $roles;
}