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;
}
?>