In Drupal, I want to read a custom field value for a group of users while logged into an admin account. I only need 1 field value from each user, the same field and I do not need this information cached.

When I use user_load it requests the user object as I only need one field this is very inefficient.

So I investigated field_get_items, but I cannot see how to use this function without first using user_load. I have attached my code and MARKED the line that is giving me an issue.

  1. how can I use field_get_items without using user_load, if not possible is there a efficient way of doing this? Please don't mention user_load_multiple

  2. with user_load does setting , $reset = TRUE prevent Drupal caching the data or does it force a cache reset, get the data from the DB and then re-cache the new data?

Code I use:

$user_a = array();  //  This is the array of URIs we need to query
foreach ($user_a AS $uid){  
  $account = user_load($uid, $reset = TRUE);  // access the user objects, one at a time
  $fullname[$uid] = $account->field_fullname[LANGUAGE_NONE][0]['value']; // extract value

$user_a = array();  //  This is the array of URIs we need to query
foreach ($user_a AS $uid){
  **$account = user_load($uid, $reset = TRUE);**
  $fullname[$uid] = field_get_items('user', $account, 'field_fullname');  // extract array
  foreach ($fullname[$uid] as $item) {
    $fullname[$uid] = $item['value'];  // extract value

You can uses field_attach_load() to load the value of a single field for multiple entities (users are entities). You will need an array of stub entities for your users, which can be build using entity_create_stub_entity(). You will also need the field ID of your field, which can found using field_info_field() or field_info_read().

After using field_attach_load(), you should be able to get the values of the field using field_get_items().

Note the field_attach_load() may inject data for other fields into the stub entities, for example if they are read from a cache.

| improve this answer | |
  • Think there is a miss type, "may object data for other fields". This sounds like something I should be aware of, can you edit/elaborate? – Niccolo Feb 4 '14 at 11:03
  • This took a lot of working out, I found the drupal explanations of the various functions very unhelpful. I had to resort to deconstructing the code for each to work out what they were doing. – Niccolo Feb 14 '14 at 10:20
  • Indeed, the documentation of the functions is not clear and does not provide any indication on how to use them together. Contributions are always welcome on drupal.org/node/443536 and in the code, so hopefully one day we may get a better documentation. – Pierre Buyle Feb 14 '14 at 16:19

No, You can't use field_get_items without the loaded user object. As the entity has to be passed as the second argument as illustrated in documentation.

Any reason why you hate user_load_multiple? As user_load itself uses user_load_multiple.

If you don't want to retrieve that huge amount of arrays. You can consider building your own query to retrieve just the field you want.

| improve this answer | |
  • I don't hate user_load_multiple. In this case it has the same problem as user_load, in that I need one value and it is retrieving the larger user object. I can write a query to access the one value, I just hoped that Drupal would already ahve a way of doing this. – Niccolo Feb 3 '14 at 11:18
  • Unfortunately, it doesn't. You have to do it the hard way. – Muhammad Reda Feb 3 '14 at 11:51
  • I guess you are the right answer then. Any advice on part (1) alternatives or part (2) caching – Niccolo Feb 3 '14 at 13:24
  • I guess there's no alternative, as I told you that I had the same issue and I had to write a custom query – Muhammad Reda Feb 3 '14 at 15:04
  • Yes, there is an alternative which does not require querying the DB: field_attach_load(). If not usable, user_load_multiple() may load too much data but at least it will perform better since it reduces the number of DB queries required to retrieve the data (because it can uses queries for all user). – Pierre Buyle Feb 3 '14 at 22:47

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.