2

I'm using the profiles2 module for my user profiles. I want to keep the user accounts hidden, and instead, use the profiles for public viewing. I disabled user pictures in users, and added an image field to the profile entity. When users post comments, the links point back to the user account. How can I change that behavior so links point back to that user's profile page? Also, how can I display the profile pics for these comments? Essentially, I'm wanting the normal user functionality to occur with profiles instead of users.

4

This can be done with a combination of

hook_preprocess_username (make links go to profile path)

Something like:

function MYTHEME_preprocess_username(&$variables) {
  $account = $variables['account']
  if($profile = profile2_load_by_user($account, 'profile_type')){
    // This gets passed onto theme_username
    $variables['link_path'] = 'user/' . $variables['uid'].'/profile/path';
    // Possibly:
    // $type = $profile->type();
    // $path = profile2_page_get_base_path($type) . '/' . $account->uid
  }
}

and then the picture override in

hook_preprocess_user_picture (override user picture)

Something like:

function MYTHEME_preprocess_user_picture(&$variables) {

  $account = variables['account'];
  if($profile = profile2_load_by_user($account, 'profile_type')){
    // You could use a custom 'display' parameter, e.g image_style
    $image = field_view_field('profile2', $profile, 'field_image');
    $variables['user_picture'] = render($image);
  }

}
8
  • Perfect. Thanks David! (note to other viewers: don't forget the '&' in the variables argument on the preprocess_username fx). Also, profile2_page_get_base_path() expected an object. I'll have to look at it later (or post if you know).
    – vintorg
    Jul 15 '13 at 5:33
  • For some reason, the field_view_field call isn't working right.
    – vintorg
    Jul 15 '13 at 6:04
  • To clarify, the field is populated, but the text 'Array' is rendered where the image should be.
    – vintorg
    Jul 15 '13 at 6:42
  • @vintorg it needs $variables['user_picture'] = render($image); - answer updated Jul 15 '13 at 9:13
  • Answer also updated to use $type = $profile->type(); for profile2_page_get_base_path object param Jul 15 '13 at 9:17
2

Drilling down a bit, the picture there is generated inside comment.tpl.php by the user-picture.tpl.php template, inside of which it relies upon the $user_picture variable.

So, the easiest way might be to put something along these lines in your theme's template.php...

YOURTHEME_preprocess_user_picture(&$variables) {

  $account=$variables['account'];

  $variables['user_picture'] = // whatever you want it to be instead of the default

}

...with whatever logic you need based upon the user information passed in via the $account variable.

This would also have a possible added benefit of overriding this everywhere a user picture is similarly themed.

3
  • Excellent jimajamma. Can I also modify the user links to point to the profile pages instead of user accounts from template.php?
    – vintorg
    Jul 15 '13 at 4:17
  • 1
    A small typo on the second line of the function, should be $variables['account'].
    – AjitS
    Jul 15 '13 at 7:25
  • good catch, fixed up. thx!
    – Jimajamma
    Jul 15 '13 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.