Hi there,

I suspect there might be an easy answer to this but I'm having trouble getting my head around it :)

I have inherited a Drupal 7 site that uses a custom variable $field_my_pic to display the users picture on all the user info pages.

I'd also like to use that same picture ($field_my_pic) anywhere that Drupal/modules use $user_picture. Is there somehow/somewhere I can set $user_picture to equal $field_my_pic wherever it is called and by whatever module?

The current issue I have is that the forum module displays $user_picture (on this install a blank avatar instead of all the lovely pics we have under the $filed_my_pic variable).

I appreciate any help/hints about the best way to proceed. I've considered a few hacks but I'd rather do this properly :D

Cheers,
Kim.

Comments

nevets’s picture

You can implement hook_preprocess_user_picture(), see: < href="http://api.drupal.org/api/drupal/modules!user!user.module/function/template_preprocess_user_picture/7">template_preprocess_user_picture() and reset the variable user_picture. This will handle any case where theme('user_picture', ...) is used to display the user picture which ideally would be all such cases.

Kimeros’s picture

Hi,

Thanks very much for this. I've read up on implementing hooks and have so far:

1. created a custom module to house my hook in
2. created a function to call the hook.

I know it is being accessed as I've printed out large amounts of variables but I am having trouble accessing the variables I want directly and re-setting them. I've ended up trying to change everyone's user picture to mine (logged in user) and then setting every user image to the person I was then testing on! (Yay for test sites).

If anyone could point me in the right direction/hints/code or docs that would be great.

And partly to show the limit of my understanding this is what I have so far:

function mine_preprocess_user_picture(&$variables) {
  global $user;

  $account = $variables['account']; 
  $user = user_load(array('uid' => $account->uid));
   print_r($user);
   $picture = $user->picture; 
   echo "<p>This is the image info using the default profile info</p>";
   echo "<p>Filename: " . $picture->filename;
   echo " Uri: " . $picture->uri . "</p>";
   
   echo "<p>This is the image info using the custom field</p>";
   echo "<p>Filename: " .  $user->field_my_pic[und][0][filename];
   echo " Uri: " . $user->field_my_pic[und][0][uri] . "</p>" ;
   
   //This is where I need to set the user image for all users to their custom pic (field_my_pic). 

 }
nevets’s picture

It would help if you expanded on the problems you are having.

I do see some possible issues, $account is passed in so should be not reason to reload it. And setting the global $user is likely to cause problems

Kimeros’s picture

Hi Nevets,

Thanks for your comments about $account and $users. I'll try to do it without reloading them.

I'm trying to replace all the standard drupal user pictures with another image that is stored in a variable called my_user_pic.

At the moment I'm trying to do this at the hook_preprocess_user_picture stage.

I don't know the best way/a way to set the default drupal user picture to another picture everywhere it occurs. I tried using variable_set but I ended up setting all the images to the same person's one.

nevets’s picture

I wonder if >User Picture Field already does what you want.

Kimeros’s picture

Thanks Nevets,

I tried that module but it seems to be for setting up a new field in the registration form which captures images to use for the user picture rather than using fields that are already created.

nevets’s picture

Should work with an existing image field you just need to follow steps 2 and 3 on the project page