Hi folks,

I disabled core user pictures in favor of an imagafield in a content profile. I'd like to place a small user picture thumb on every page, so the current user can easily access his own profile (with a click on his avatar).

I got it working, but I fear that the following approach could be bad for performance. Furthermore, isn't it the good drupal way to leave logic out of the template and use only print statement in templates.

So this is my code:

  <?php 
            $avatar = $content_profile->get_variable('profile', 'field_profile_avatar');

            $title =  $content_profile->get_variable('profile', 'title');
  	    print theme('imagecache', 'user-thumb-small', $avatar[0]['filepath'], $title, $title); 
   ?>

How can I do it the Drupal way? I surely want to get these variable initialisation out of the template file. I tried to put it in template.php, but this did not work, as I am not that exprienced with this ($content_profle->get_variable() ) is not available in template.php, is it?

thanks.

Comments

igorik’s picture

Hi

Why do you think it is not a drupal way? I think it is right.
You test and print avatar image in user-picture.tpl.php where it is correct.

My content profile code for user avatar is this, it is a little bit complicated, because I am doing user avatar
from 1. his imagefield image for avatar, 2. from his imagefield profil image 3. default image based on user sex (cck field 'pohlavie')

<div class="picture">
<?php   		
//STEP 1 - If user has inserted the imagefield image for avatar
$img_url = $content_profile->get_variable('uprofile', 'field_ikonka');
$img_url = $img_url[0]['filepath'];

if ($img_url == '') { 
	//If yes, we use it, if no we continue 
	//STEP2 If user has a imagefield image for profile foto
	$img_url = $content_profile->get_variable('uprofile', 'field_profil_fotografia1'); 
	$img_url = $img_url[0]['filepath'];
	//If user has a imagefield for profile foto, we use it, if no, we use default image for avatar
	if ($img_url == '') {  
		$pohlavie =  $content_profile->get_variable('uprofile', 'field_profil_pohlavie_2');
		$pohlavie =  $pohlavie[0]['value'];

		if ($pohlavie == 'Muž') {
			$img_url = 'files/fotky_profil/default/stredne_velke_bez_textu/kacer.jpg';
		} else if ($pohlavie == 'Žena') {
			$img_url = 'files/fotky_profil/default/stredne_velke_bez_textu/kacka.jpg';
		} else {
			$img_url = 'files/fotky_profil/default/stredne_velke_bez_textu/totoro.gif';
		}
	}	 
}		
print l(theme('imagecache', 'ikonka', $img_url), 'user/' . $account->uid , array('attributes' => array('class' => 'user_theme_link', 'rel' => 'uid:' . $account->uid),'html' => TRUE) );  
?>
</div>

Igor
http://www.somvprahe.sk

pheraph’s picture

I have a very similar question, but I cannot use the .tpl.php-method, because I have to override the function phptemplate_activity_user_picture($account) from the activity-module in the template.php. As the TS stated the $content_profle->get_variable()-function is not available in the template.php.

Basically the code looks like this, but the last line throws an error. :-(

function phptemplate_activity_user_picture($account) {	
  $content_profile = content_profile_load('profile', $account->uid);
  $pict = $content_profile->variable_get('profile', 'fieldname');
  ...
}

How can I achieve that in the template.php?

Regards,
jakewalk