By jasonsafro on
I am trying to build an array of UID=>name pairs for all users with a particular role. I have the following code snippet. It works but could be written more efficiently.
- How do I filter by role in the query?
- How do I add fields to the query return?
- Is there a better way to get the output?
<?php
// Get a data set of all users who are team leaders
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'user')
;
$result = $query->execute();
$team_leaders = array();
foreach($result['user'] as $one_user) {
$user_record = user_load( $one_user->uid );
if( !empty($user_record->roles[RID_TEAM_LEADER]) ) {
$team_leaders[$one_user->uid] = $user_record->name;
}
}
?>
Comments
Cannot be done. You can only
Cannot be done. You can only create following conditions: entityCondition, fieldCondition and propertyCondition.
The one that fits your requirement is propertyCondition however it works only on properties within the entity table that is in this case the user table. Role ids are in a separate table and currently entity query cannot join other than field table.
Go for custom query and use $users = user_load_multiple($uids); to load user objects.
One way
Seems like this comes from
Seems like this comes from here: http://drupal.stackexchange.com/a/11186/10667