0

I'm building a custom field for a form based off of the values stored in a user reference field. The options for this field are being built from the following database query:

<?php 
// Fetch the users referenced in the provided node
$args = array(':nid' => $nid);
$result = db_query("
    SELECT 
        field_data_field_user_ref.field_user_ref_target_id, 
        users.name
    FROM field_data_field_user_ref
        JOIN users
            ON field_data_field_user_ref.field_user_ref_target_id=users.uid
    WHERE entity_id = :nid", $args);
$arr = $result->fetchAll();

// Add these users as an array to be displayed
$users = array();
foreach ($arr as $val) {
    $crafters[$val->field_user_ref_target_id] = $val->name;
}

...returning the array in the form of...

uid 1 => name 1
uid 2 => name 2
uid 3 => name 3

This works fine, except for the fact that the user's username is displayed instead of their "real name." In my case, the Real Name is being generated by a Name Field (from the Name project). I can't seem to figure out a way to grab the real name from the database. Not sure if it is even stored in there.

Is there an alternative way to grab each user's real name? I'm assuming I could use some application of user_load but I don't know if there's a performance hit associated with loading an unknown number of users one by one just to grab their names. Probably will never be more than a handful of users, but I'd rather keep it optimized.

1 Answer 1

0

The Name project used entity fields that can be queried in the DB, just like any other field. All you need to know is the machine name of field that you attached to the user entity. To use your example above:

$result = db_query("
    SELECT 
        field_data_field_user.field_user_target_id, 
        nf.*,
        users.name
    FROM field_data_field_user
        JOIN users
            ON field_data_field_user.field_user_target_id=users.uid
        JOIN field_data_field_MY_NAME_FIELD AS nf
            ON nf.entity_id=users.uid AND nf.entity_type = 'user' AND nf.bundle = 'user'
    WHERE field_data_field_user.entity_id = :nid", $args);

All you need to do is replace MY_NAME_FIELD with the machine name of your actual field. The result will be rows that contain name columns associated with the user reference.

4
  • While this does get me the information from the name field itself (although I don't think the users table is necessary given the direct query of the name_field), this doesn't return the rendered Real Name. I'd prefer to just grab the Real Name if at all possible so that if I ever make changes to that, they will be reflected here. But, this will indeed get me each piece of the name individually.
    – Mrweiner
    Apr 14, 2016 at 2:58
  • This was to address the question directly. The alternative is to find the Name module's implementation of its name field renderer and insert it into your array option builder code. It does add overhead as you mentioned, but scalability should only come into play if you: 1. have a massive number of user references or 2. have many user hitting the form field you're building.
    – Shawn Conn
    Apr 14, 2016 at 3:56
  • I appreciate the answer, but I don't see how this directly answers the question. The title might have been formatted better, but the end of my questions asks "Is there an alternative way to grab each user's real name?". The question is about how to get the real name, not how to query the database for the values of the name field.
    – Mrweiner
    Apr 14, 2016 at 5:38
  • Entity field query might be a better fit: drupal.org/node/1343708 or did you check the names module code? There may be an API you could use.
    – bloke_zero
    Apr 14, 2016 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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