1

How do you query custom fields on the user table? If edit a user in the Admin panel, and hit the DEVEL tab, I see the field I added, 'field_display_name'.

In a node template php file, I want to replace the 'name' field with 'field_display_name'.

When I query, either using:

$result = db_select('users', 'u')
  ->fields('u')
  ->condition('name', 'bubba', '=')
  ->execute()
  ->fetchAssoc();

or

$rst = db_query("select * from {users} where name = :name", array(':name' => 'bubba',));

neither one of these return my custom field on the user table. (DPM does not show field_display_name)

So obviously, i'm missing something fundamental in my understanding of the data structure.

How do i return this added field by querying with uid or name to return and print the 'field_display_name' ?

3 Answers 3

2

Entity fields are in their own separate table(s), they don't get attached to the base entity table.

You can use an EntityFieldQuery to get that data:

$query = new \EntityFieldQuery();
$query->entityCondition('entity_type', 'user')
  ->fieldCondition('field_display_name', 'value', 'bubba');

$results = $query->execute();
if (!empty($results['user'])) {
  $users = user_load_multiple(array_keys($results['user']));
}

See Where does Drupal 7 store custom user account fields? for a bit more info

2
  • thx but to clarify my fundamental confusion... you have name field on the User table, and the custom field_display_name on the a separate table. The EntityFieldQuery function queries the custom fields? In above, I want to find/return the value(s) for field_display_name searching on user.name. This returns error, it does not like if I replace above fieldCondition to search on User.name like: $query->fieldCondition('name', 'value', 'bubba');
    – Dubya
    Commented Jun 16, 2013 at 5:37
  • I realise this is very late but I've only just seen your comment - the columns in the base table are known as properties rather than fields, and you can use a propertyCondition() to query them: $query->propertyCondition('name', 'bubba');
    – Clive
    Commented Oct 1, 2013 at 17:50
0

Drupal 8:

$query = \Drupal::service('entity.query')
  ->get('user')
  ->condition('field_custom', $custom_value)
  $entity_ids = $query->execute();
0

Alternatively, you could use the entity type manager service.

/** @var \Drupal\Core\Entity\Query\QueryInterface $query */
$query = \Drupal::service('entity_type.manager')
  ->getStorage('user')
  ->getQuery();

$query->condition('field_custom', $custom_value)
$entityIds = $query->execute();

Previous code only gives you the user IDs that fulfill the condition, then you need to do a call to 'user_load_multiple' function.

$users = [];
if (!empty($entityIds)) {
  $users = user_load_multiple(array_keys($entityIds));
}

For Drupal 8.7+ and Drupal 9, I strongly recommend using Dependency Injection where it is possible because of code standards and performance.

Your Answer

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

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