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


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.

5 Answers 5


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.

// 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.
  • 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.
    – JFC
    Jun 19, 2019 at 18:38
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
   // Fetch the file entity associated with the image item entity reference.
   // Use the url() to call file_create_url() as in Drupal 7 to get the 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, 2016 at 17:30

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:


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?


This is the best way on Drupal 8, create a custom module with Controller and send a variable to Twig:

$pictureUri = $user->user_picture->entity->getFileUri();
                         $style = \Drupal::entityTypeManager()->getStorage('image_style')->load('thumbnail');
                         $urlPicture = $style->buildUrl($pictureUri);

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.