1

I'm trying to filter users using entityfieldquery (type 'user') by fieldConditions, but I don't know how to print out the usernames on a page. If I use "print_r", it works. But I don't want to print them out on a white page. ^^ If I use $output, I get only one name. For nodes, there is a function "node_view_multiple" to use it like $output = node_view_multiple($nodes); Is there also a way for entity_type user?

Here is my code:

global $user;
$angemeldeter_user = user_load($user->uid); 
$alter = $angemeldeter_user->field_alter['und'][0]['value'];
$alter_faktor = 4;
$alter_untergrenze = $alter - $alter_faktor;
$alter_obergrenze = $alter + $alter_faktor;

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'user')
->fieldCondition('field_alter', 'value', array($alter_untergrenze, $alter_obergrenze), 'BETWEEN');
$result = $query->execute();

if (isset($result['user'])){   
global $user; 
$angemeldeter_user = user_load($user->uid);                                         
$users=user_load_multiple(array_keys($result['user']));
dpm($users);

foreach($users as $value) {
  $user_list = (array)$value;
  $users[$user_list['uid']] = $user_list['name'];
  $output = '';
  $output .= $user_list['name'];
  return $output;
  }
  }
  else{
    $hihi = 'no results';
    return $hihi;
  }
}
1

Since user_load_multiple() returns an array of user objects you need to use the '->' in order to access them.

$output = '';
foreach ($users as $key => $value) {
  $output .= "index: $key; name: $value->name<br />";
}
return $output;
  • You're welcome! Don't forget to mark the answer as useful. Thank you :) – Leolando Tan Mar 8 '15 at 15:21
  • 1
    The username is user-input, and therefore considered unsafe to output directly. This also doesn't take into account hook_username_alter, which lets modules change the what gets displayed. Checkout format_username() and theme_username() for more information. – mpdonadio Mar 8 '15 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.