0

The following works so far:

  1. I can set a custom user field e.g. 'full_name' in Drupal Account Settings
  2. The logged in user has access to this field using the following code.

    global $user; $user = user_load($user->uid); // Fully load the user object $custom_vars['full_name'] = $user->field_full_name[LANGUAGE_NONE][0]['value']; echo "My full name is : $custom_vars['full_name']";

This is what I want to do:

  • I want to log in as an admin account and for a specific group of
    users who's unique IDs know I want to display this field.
  • So perhaps for userID = (1234, 1175, 14532) I want to see this value for 'full_name', how do I do it?

I am hoping for a PHP solution that uses code similar to the above rather than a SQL DB call, but if I need to directly access the DB so be it.

  • In the "Danger Will Robinson" category, you are halfway re-logging in as the $user->uid above by resetting the global $user variable. So, whatever you do, use $account or another variable in any solution you come up with. – Jimajamma Jan 29 '14 at 14:08
  • Thanks, I am aware of that, but you are right to point it out in case someone else uses this as a solution to their problem. $user refers to the logged in user and which in this case would be an admin user so messing with this value giving another drupal userID to $user is not a good idea. – Niccolo Jan 29 '14 at 14:14
  • 1
    Do not mess with global $user unless you are damn sure you know what you are doing. Substituting it on the fly is dangerous at best, outright broken at worst. – Mołot Jan 29 '14 at 14:22
  • That danger was mentioned in the previous comments. In the code example, as state, it is deliberately using $user as logged in user. The question was asking for an alternative so an admin can access multiple user data. – Niccolo Jan 31 '14 at 15:54
1

This seems a bit dirty, but using your exact code as a base, something like the following may work:

$uids = array(
  // Here is the list of user ID's that you know.
);

$full_names = array();

foreach ($uids AS $uid){
  $account = user_load($uid);
  $full_names[$uid] = $account->field_full_name[LANGUAGE_NONE][0]['value'];
}

unset($account);

return $full_names;

This will give you an array of full usernames keyed by the UID.

Also to bear in mind, this performs no access checking or permission checking.

For further reading, you might want to try taking a look at something like field_get_items() as well.

  • That worked once I altered return $full_usernames; to return $full_names; – Niccolo Jan 29 '14 at 14:56
  • @Niccolo ha..cheers, I've edited that typo! – Chapabu Jan 29 '14 at 15:00
  • You can replace foreach ($uids AS $uid) with foreach ($uids AS $value) – Niccolo Jan 30 '14 at 15:34
  • Yes, that's merely semantics though. It's only a PHP variable, and $uid makes more sense in this context. It's totally up to you though :-) – Chapabu Jan 30 '14 at 15:37
  • True. I was keeping things consistent in my code as I am also using associative arrays to store the data, lots of $key $values going around. I now have three ways of doing this. Am trying to work out which is most efficient and safest. – Niccolo Jan 30 '14 at 15:40
0

I would investigate the user_load_multiple() function, which would allow you to do something like this:

$user_ids = array(1234, 1175, 14532);

$accounts = user_load_multiple($user_ids);

which would return an array of user objects keyed by their user id, so you would now have access to the field as:

$accounts[UID]->field_name_etc_etc_etc

If you want to simply it further, that's up to you as the rest is just PHP.

  • user_load_multiple works the same as user_load except it means that all the user objects are loaded into $account[] instead of one being loaded into $account at a time. I still need a foreach loop to extract the information. So it is a question of which is worse (1) the additional time it takes to run user_load multiple times than running user_load_multiple once and (2) the larger size of $account[] over $account. – Niccolo Jan 30 '14 at 15:27
  • I would be more concerned with SQL and run time performance vs memory usage unless you are loading 1000s and 1000s of users but that's just my opinion. For example, each user_load() generates a SQL call to the DB whereby user_load_multiple() creates just one. Also realize of course that both cache the resulting user entity so memory usage could become an issue if you are loading a LOT of users. – Jimajamma Jan 31 '14 at 13:38
  • That is the point I made. It is a cost between time and memory. Note after each user_load you can clear memory if needed. I am checking out field_get_items() as an alternate. – Niccolo Jan 31 '14 at 15:48
  • I know YOU can clear it (via unset()). I was also making the point that the entity_loads called by both cache all of the information as well, eg, so if later on in the page you need information about user ###, it already has it and doesn't have to grab it from the db again. So, if you call user_load() 1000 times, it is caching 1000 users statically and if you call user_load_multiple(1000 users) it too caches them unless you use the argument $reset: TRUE to reset the internal cache and load from the database; FALSE (default) to load from the internal cache, if set with either of them. – Jimajamma Jan 31 '14 at 15:59
  • You are absolutely right in general terms of use of user_load_..... However, the original question is about extracting a single field value from the user object. I would not keep the object in memory for 1 field value, hence "foreach loop to extract the information". I would free the memory immediately after I no longer need the information it stores. So again it is between keeping memory overhead low by systematically clearing after each user_load or reducing run time demands by clearing the larger memory overhead after 1 user_load_multiple. – Niccolo Jan 31 '14 at 16:17
0

3 years later this is my analysis:

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'user')
    //    ->propertyCondition('uid', array(21,22,23,24), 'IN')
    ->propertyCondition('status', 1);

  $result = $query->execute();
  if (isset($result['user'])) {
    $news_items_nids = array_keys($result['user']);
    $news_items = entity_load('user', $news_items_nids);
  }

Time: 6.16s

$total_users = entity_load('user', FALSE, array('status' => 1));

Time: 6.00s

$local_user = db_query('SELECT u.uid, u.name, fn.field_last_name_value FROM users as u INNER JOIN field_data_field_last_name  as fn ON u.uid = fn.entity_id WHERE u.status = 1')->fetchAll();

Time: 3.63s

Total users: 1184

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.