Created
February 14, 2015 04:42
-
-
Save gh640/f6c5ce26a629259101a7 to your computer and use it in GitHub Desktop.
Drupal 7 function to get all users with specific role(s)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* get all users with specific role(s) | |
* | |
* @param int|int[] $role_id | |
* role id for role | |
* | |
* @return object[] | |
* user objects with given role(s) | |
*/ | |
function user_load_multiple_by_role($role_id) { | |
if (is_int($role_id)) { | |
$role_id = array($role_id); | |
} | |
$user_ids = db_select('users_roles', 'ur') | |
->fields('ur', array('uid')) | |
->condition('rid', $role_id, 'IN') | |
->execute() | |
->fetchCol(); | |
return (count($user_ids) > 0) ? entity_load('user', $user_ids): array(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment