Hi guys!

I'm trying to create a custom welcome block in drupal 7. I've been able to print Username an all the other links that i want, but have not been able to figure out how to print the current logged-in user picture in a block.

Here's the code I've been trying to adapt to no avail:

<?php 
            $user = user_load(arg(1));
 if($user->picture){           
            print theme('image_style',
                array(
                    'style_name' => 'avatar',
                    'path' => $user->picture->uri,
                    'width' => NULL,
                    'height' => NULL,
                    'attributes' => array('class' => 'profile-avatar')  
                )
            ); 
}else{
                    echo '<img src=" ' . base_path() . drupal_get_path('theme', 'theme_path') . '/images/user_icon.png" />' ;
                }   
        ?>

The above code only shows up when I'm viewing a user's profile (it displays the picture of the user being viewed as opposed to current logged-in user). I want to know how to show the current logged-in user picture on all pages where the block is displayed.

Comments

Jaypan’s picture

Change this:

$user = user_load(arg(1));

To this:

global $user;
cnario’s picture

I did that already but got the following message:

Notice: Trying to get property of non-object in eval() (line 9 of /home/nollycom/public_html/modules/php/php.module(80) : eval()'d code)

Jaypan’s picture

global $user;
if($user->uid && isset($user->picture, $user->picture->uri)){
            print theme('image_style',
                array(
                    'style_name' => 'avatar',
                    'path' => $user->picture->uri,
                    'width' => NULL,
                    'height' => NULL,
                    'attributes' => array('class' => 'profile-avatar')
                )
            );
}else{
                    echo '<img src=" ' . base_path() . drupal_get_path('theme', 'theme_path') . '/images/user_icon.png" />' ;
                }
        
cnario’s picture

it displays the default picture instead instead of the user picture. What could be wrong with the code?

ColdSun’s picture

Did you ever resolve why the default picture was displayed instead of the user's?
I'm running into a similar issue.

Jaypan’s picture

What does your code look like?

ColdSun’s picture

Was resolved by using a user_load to get the full object.
Lapse of memory with regards to the global being a slimmed down version

dapseen’s picture

Using [current:user-picture] token in a block will display the currently logged in user

adigunsherif’s picture

By default, token may not be used

The best way to get this is to use views.

With fields being user picture, you can filter down to current logged in user.

With this method, u avoid using php and going down to template