4

I am using the following code to get the URL of the image associated to a user.

$user=\Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$picture=$user->get('user_picture')->entity->url();

If the user didn't upload a picture, this code doesn't return anything useful.

How can I get the URL for the default image?

5

User pictures are no longer guaranteed to exist in Drupal 8. It is added by the standard profile, but not added if you use the minimal profile.

The user picture is simply a image field attached to the user bundle, so normal file field API's can be used. To get the file path of the default image for the user picture field attached to the user bundle:

<?php
$field = \Drupal\field\Entity\FieldConfig::loadByName('user', 'user', 'user_picture');
$default_image = $field->getSetting('default_image');
$file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid']);
$file->getFileUri();
?>

I'm not sure what you are trying to achieve. If you are looking for better default avatars, then I encourage you to look at Avatar Kit for Drupal 8. Disclaimer: I am the developer of Avatar Kit.

  • Thank that work ! i just add $picture=file_create_url($file->getFileUri()); for get the real path – Kevin Jul 31 '15 at 7:33

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.