5

I want to print user_picture thumbnail in my page template. I am using the following code in hook_preprocess_html() in my .theme file.

$user = user_load(\Drupal::currentUser()->id());
$style = entity_load('image_style', 'thumbnail');
$variables['user_picture'] = $style->buildUrl($user->get('user_picture')->entity->url());

It's generating a URl but it is showing 404, what could be wrong here?

  • Check whether URI is present is or not, or try to generate image url and pass to theme file with image tag – Ajay Reddy Aug 24 '16 at 15:01
  • @AAjayKumarReddy - Url is there I want to make a thumbnail image from that URL – Unnikrishnan Aug 25 '16 at 5:34
  • @zhilevan - What I'm trying to do is showing logined user image in my site header. I implemented hook_preprocess_html and trying to show rendered user_picture in a variable. is that the correct way for doing this ? – Unnikrishnan Aug 25 '16 at 6:05
  • 1
    @Unnikrishnan merci for taking my advice. If you want another one, I think you should change the title of your question to something like "Drupal 8 - What cause my 404 error for my user_picture thumbnail in template file?" (= compromise between your google concern and my prior revision). It will probably help to illustrate why, IMO, your question is NOT a duplicate of the so called "possible duplicate". But, the choice is yours to yes/no do so. – Pierre.Vriens Aug 25 '16 at 8:12
  • 2
    @Unnikrishnan : Remember "my" title recommendation from yesterday, in revision 2? – Pierre.Vriens Aug 26 '16 at 6:28
2

In hook_preprocess_html() add the following code.

  $user = user_load(\Drupal::currentUser()->id());;
  $variables['user_pic'] = $user->user_picture->entity->uri->value;

In your template file use code similar to the following one.

 <img width="62" height="62" class="image-responsive img-circle" src="{{ file_url(user.pic) }}" />

If you want to apply an image style, use the following code in hook_preprocess_html().

$variables['user_pic'] = ImageStyle::load('thumbnail')->buildUrl($user->get('user_pic‌​ture')->entity->uri-‌​>value);

In the template file, use the following.

 <img src="{{ user.pic }}" />
  • ImageStyle::load('thumbnail')->buildUrl($user->get('user_picture')->entity->uri->value); This helped me thanks a lot :) – Unnikrishnan Aug 25 '16 at 7:49
  • @Unnikrishnan you're welcome :) – Yusef Aug 25 '16 at 8:03
0

First you have to load your file. In Drupal 8 everything is based on entities, and so are files. Loading a file is the same as loading a node.

$file = File::load($fid);

If your file is valid, you can start by building the render array for the image. In the showed example, "thumbnail" is the used image style.

$variables = array(
  'style_name' => 'thumbnail',
  'uri' => $file->getFileUri(),
);

// The image.factory service will check if our image is valid.
$image = \Drupal::service('image.factory')->get($file->getFileUri());
if ($image->isValid()) {
  $variables['width'] = $image->getWidth();
  $variables['height'] = $image->getHeight();
}
else {
  $variables['width'] = $variables['height'] = NULL;
}

$logo_render_array = [
  '#theme' => 'image_style',
  '#width' => $variables['width'],
  '#height' => $variables['height'],
  '#style_name' => $variables['style_name'],
  '#uri' => $variables['uri'],
];

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