2

I am trying to get user data by UUID in my custom module and I don't know how to join on users_field_data table to get user fields (email, username, etc.).

My request for the users table works but I don't know how to join.

$query = \Drupal::entityQuery('user')
          ->condition('uuid', $uuid)
          ->execute();
$result = User::loadMultiple($query);
dpm($result);

How can I execute a join with the users table?

  • EQ is only for getting entity ids, not specific fields. You have to load the entities as you are doing now. If you only want fields you need "classic" query. – user21641 Oct 18 '17 at 13:44
  • Please don’t add solutions to the question body - you can post an answer in the text field below – Clive Oct 18 '17 at 14:10
1

The easiest way to load a field from any entity when querying is to use the entityTypeManager:

$users = \Drupal::entityTypeManager()->getStorage("user")
        ->loadByProperties(['uuid' => $uuid]); // or any other properties.

foreach ($users as $user) {
    var_dump($user->field_customfield->getValue());
}

see: working-with-the-entity-api

Be careful to not use \Drupal::entityManager, since it is deprecated.

  • Great solution @Cawet. Could you tell me how to specify I have only one result for my request so I will not have a subarray ? – Pauloscorps Oct 18 '17 at 15:07
  • I don't think it's possible, but if you sure you'll get only one result and you don't want use a foreach, y'ou can use array_pop or array_shift to extract the entity. – Cawet Oct 20 '17 at 8:04

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.