Same name and namespace in other branches
  1. 4.6.x modules/user.module \user_roles()
  2. 4.7.x modules/user.module \user_roles()
  3. 5.x modules/user/user.module \user_roles()
  4. 6.x modules/user/user.module \user_roles()
  5. 7.x modules/user/user.module \user_roles()
  6. 8.9.x core/modules/user/user.module \user_roles()
  7. 9 core/modules/user/user.module \user_roles()

Retrieve an array of roles matching specified conditions.

Parameters

bool $members_only: (optional) Set this to TRUE to exclude the 'anonymous' role. Defaults to FALSE.

string|null $permission: (optional) A string containing a permission. If set, only roles containing that permission are returned. Defaults to NULL, which returns all roles.

Return value

\Drupal\user\RoleInterface[] An associative array with the role id as the key and the role object as value.

Deprecated

in drupal:10.2.0 and is removed from drupal:11.0.0. Use \Drupal\user\Entity\Role::loadMultiple() and, if necessary, an inline implementation instead.

See also

https://www.drupal.org/node/3349759

1 call to user_roles()
user_role_names in core/modules/user/user.module
Retrieves the names of roles matching specified conditions.
5 string references to 'user_roles'
HandlerFilterRolesTest::testDependencies in core/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php
Tests that role filter dependencies are calculated correctly.
HandlerFilterRolesTest::testMissingRole in core/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php
Tests that a warning is triggered if the filter references a missing role.
UserViewsData::getViewsData in core/modules/user/src/UserViewsData.php
Returns views data for the entity type.
views.view.test_views_handler_field_role.yml in core/modules/user/tests/modules/user_test_views/test_views/views.view.test_views_handler_field_role.yml
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 core/modules/user/config/optional/views.view.user_admin_people.yml
core/modules/user/config/optional/views.view.user_admin_people.yml

File

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

Code

function user_roles($members_only = FALSE, $permission = NULL) {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use \\Drupal\\user\\Entity\\Role::loadMultiple() and, if necessary, an inline implementation instead. See https://www.drupal.org/node/3349759', E_USER_DEPRECATED);
  $roles = Role::loadMultiple();
  if ($members_only) {
    unset($roles[RoleInterface::ANONYMOUS_ID]);
  }
  if (!empty($permission)) {
    $roles = array_filter($roles, function ($role) use ($permission) {
      return $role
        ->hasPermission($permission);
    });
  }
  return $roles;
}