Apr 10, 2014

Output user profile picture programmatically in Drupal

Drupal is a wonderful platform to create complex websites in simpler way but sometimes, a simple task is too difficult to achieve in Drupal.
There are many pros of using Drupal in terms of other CMS available in market, such as its:

  • Extremely Flexible
  • Developer Friendly
  • Strong SEO
  • Capabilities Enterprise
  • Friendly Stability 

For instance lets take the example of user profile, if someone wants to show the user profile picture in Drupal then its a complicated task for him/her. Let me simply this for you in the below code. I've created the code in such a way if the user profile picture is not set, then it will show you the default profile pic.

Code:

global $user;
if ($user->uid) {
$user = user_load($user->uid);
print theme(
'image_style',
array(
'style_name' => 'thumbnail',
'path' => !empty($user->picture->uri)?$user->picture->uri:variable_get('user_picture_default'),
'attributes' => array(
'class' => 'avatar'
)
)
);
}
Cheers!
Photo Credit: Instagram

No comments :

Post a Comment