7

I need to get the user's profile picture using the following code:

\Drupal::service('renderer')->renderPlain(\Drupal\user\Entity\User::load(\Drupal::currentUser()->id())->user_picture->first()->view('large'))

It returns an object with the image tag, but it fails with the _Call to a member function view() on a non-object_ error when a user has no profile picture set.

I also tried the answer to "How do I get the default user picture?", but it gives the error

Drupal\Core\Database\InvalidQueryException: Query condition 'file_managed.uuid IN ()' cannot be empty.

4

The code is OK, but you have to check if the field is not empty:

if ($user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id())) {
  if (!$user->user_picture->isEmpty()) {
    $picture = $user->user_picture->view('large')
  }
  else {
    $picture = // get default picture
  }
}

In Drupal 8 don't use render service. The new template system does this automatically.

3
// Getting the actual user uid.
$uid = \Drupal::service('current_user')->id();
// Getting the actual user entity.
$user = \Drupal::service('entity.manager')->getStorage('user')->load($uid);
// Getting the user picture.
$user_picture = $user->user_picture->entity->getFileUri();

// To use dpm you need the devel module.
dpm($user_picture);
  • 1
    It's preferable to use this way to call a user entity. You'll need to use this approach, or else, it wont respect the Drupal coding standards that requires you to put the call "\Drupal\user\Entity\User" on the top inside a "use". If you use the 4k4's answer, you'll have a problem if you need this in a static function. This answer here is the good one. – JF Kiwad Jun 19 at 18:38
1
1. Use the getAccount method on the AccountProxy class, which should save an entity load if the account object already exists.
$account = \Drupal::currentUser()->getAccount();
2. Use methods to on the field item (entity reference) to check if there is a value.
if (!$account->user_picture->isEmpty()) {
3. Instead of using the renderer to render the plain output from the field item. This data can be retrieved via some other methods.
   $image_url = $account
                  ->user_picture
                  ->first()
   // Fetch the file entity associated with the image item entity reference.
                  ->get('entity')
   // Use the url() to call file_create_url() as in Drupal 7 to get the URL.
                  ->url();
}
  • I got an error Call to a member function isEmpty() on a non-object on the second point. Also, on print_r($account), I got a Drupal\Core\Session\UserSession Object with no user_picture property. – user1960 Dec 14 '16 at 17:30
1

This is really more of a PHP issue. You shouldn't put calls to object methods within the arguments of another function, especially if you're dealing with fields on a drupal entity, as they may be set or not set.

$uid = \Drupal::currentUser()->id();

// Might be zero if user is logged out.
if ($uid) {
  $user = \Drupal\user\Entity\User::load($uid);
   // check that the field exists and that it has a value.
  if (!empty($user->user_picture) && $user->user_picture->isEmpty() === FALSE) {
    $image = $user->user_picture->first();
    $rendered = \Drupal::service('renderer')->renderPlain($image);
  }
}

Also, Drupal coding standards suggest you try and keep your lines under 80 characters, and I feel that's good practice, as it makes the code more readable. See:

https://www.drupal.org/node/935284

Also, here's a post about getting the default image for a user. You might want to set a default image for this field.

How get the default user picture?

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.