Reply to comment

Drupal. How to use an image field from the profile node instead of user picture (avatar)

Julia 19/01/2010

I've recently had to make default pictures for user pictures,
but they had to be defferent for different user roles.

I used the module content_profile
and created two profile content types for Student and Teacher roles.
So I decided to change a user picture derectly in my theme.

I change the preprocess function mytheme_preprocess_user_picture from the user module in the template.php file:

<?php
function mytheme_preprocess_user_picture(&$variables) {

$variables['picture'] = '';
 
   
$account = $variables['account'];
   
   
$user = user_load(array('uid' => $account->uid));   
   
$roles = $user->roles;

   

$profile = '';   
    if (
is_array($roles)) {     
      if (
array_search('student', $roles)) {
         
$profile = 'studentprofile';
      } else if (
array_search('sensei', $roles)) {
         
$profile = 'profile';
      }
    }
   
    if (!empty(
$profile)) {
     
$node_profile = content_profile_load($profile, $account->uid);
     
$field_photo = content_fields('field_photo', $profile);
     
     
$picture = content_view_field($field_photo, $node_profile);
    } else {
   
       
// from default preprocess_user_picture:
       
if (!empty($account->picture) && file_exists($account->picture)) {
         
$picture = file_create_url($account->picture);
        }
        else if (
variable_get('user_picture_default', '')) {
         
$picture = variable_get('user_picture_default', '');
        }
   
        if (isset(
$picture)) {
         
$alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous'))));
         
$variables['picture'] = theme('image', $picture, $alt, $alt, '', FALSE);
          if (!empty(
$account->uid) && user_access('access user profiles')) {
           
$attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
           
$variables['picture'] = l($variables['picture'], "user/$account->uid", $attributes);
          }
        }
       
//
       
   
}
   
$variables['picture'] = $picture;

}

?>

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options