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.
2 Answers
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);
}
}
-
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).– vintorgCommented Jul 15, 2013 at 5:33
-
For some reason, the field_view_field call isn't working right.– vintorgCommented Jul 15, 2013 at 6:04
-
To clarify, the field is populated, but the text 'Array' is rendered where the image should be.– vintorgCommented Jul 15, 2013 at 6:42
-
@vintorg it needs
$variables['user_picture'] = render($image);
- answer updated Commented Jul 15, 2013 at 9:13 -
Answer also updated to use
$type = $profile->type();
forprofile2_page_get_base_path
object param Commented Jul 15, 2013 at 9:17
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.