function user_roles
Same name in other branches
- 7.x modules/user/user.module \user_roles()
- 9 core/modules/user/user.module \user_roles()
- 10 core/modules/user/user.module \user_roles()
Retrieve an array of roles matching specified conditions.
Parameters
bool $membersonly: (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.
6 calls to user_roles()
- EntityOperationsTest::testEntityOperationAlter in core/
modules/ system/ tests/ src/ Functional/ Entity/ EntityOperationsTest.php - Checks that hook_entity_operation_alter() can add an operation.
- FilterFormat::postSave in core/
modules/ filter/ src/ Entity/ FilterFormat.php - Acts on a saved entity before the insert or update hook is invoked.
- Roles::preRender in core/
modules/ user/ src/ Plugin/ views/ field/ Roles.php - Runs before any fields are rendered.
- UninstallTest::testUserPermsUninstalled in core/
modules/ system/ tests/ src/ Functional/ Module/ UninstallTest.php - Tests the hook_modules_uninstalled() of the user module.
- UserRoleAdminTest::testRoleWeightOrdering in core/
modules/ user/ tests/ src/ Functional/ UserRoleAdminTest.php - Test user role weight change operation and ordering.
6 string references to 'user_roles'
- EntityViewsMultiValueBaseFieldDataUpdateTest::testUpdateMultiValueBaseFields in core/
modules/ views/ tests/ src/ Functional/ Update/ EntityViewsMultiValueBaseFieldDataUpdateTest.php - Tests multi-value base field views data is updated correctly.
- 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
File
-
core/
modules/ user/ user.module, line 1142
Code
function user_roles($membersonly = FALSE, $permission = NULL) {
$roles = Role::loadMultiple();
if ($membersonly) {
unset($roles[RoleInterface::ANONYMOUS_ID]);
}
if (!empty($permission)) {
$roles = array_filter($roles, function ($role) use ($permission) {
return $role->hasPermission($permission);
});
}
return $roles;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.