Get users by display name on reference autocomplete field I need a list of users for a reference autocomplete field. I know about the realname module, but I haven't installed it, neither will I as long as it remains an RC (and I don't need all it provides). Anyway, I copied the code I needed from it and adapted it accordingly.    

My users have a first_name and last_name fields. Here is the query I came up with in order to return the JSON for my autocomplete field:

$sql = "SELECT u.uid
        FROM users u 
        INNER JOIN users_field_data ud ON u.uid=ud.uid
        LEFT JOIN user__field_first_name fn ON fn.entity_id=u.uid 
        LEFT JOIN user__field_last_name ln ON ln.entity_id=u.uid 
        WHERE CONCAT_WS(:separator, fn.field_first_name_value, ln.field_last_name_value) LIKE :text OR ud.name LIKE :text";

$uids = \Drupal::database()-&gt;queryRange($sql, 0, 10, [
    ':text'      =&gt; '%' . db_like($input) . '%',
    ':separator' =&gt; ' ',
])-&gt;fetchCol();


Do I have an alternative to this? I would rather not reference individual tables.
A potentially cleaner/easier way to do that would be to leverage the JSON API.  After you install that module, you should able to make a call to the /jsonapi/user/user endpoint (see the "GET user accounts section on https://www.drupal.org/docs/8/modules/json-api/fetching-resources-get), and parse out the UIDs into the array.