user_roles($membersonly = FALSE, $permission = NULL)
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.
File
- core/modules/user/user.module, line 1053
- Enables the user registration and login system.
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; }
Please login to continue.