XF 1.2 Conditional to say "if default avatar"

jdeg

Active member
Is there a conditional that I can use everywhere an avatar is displayed to say if default (or if custom) avatar, do this....
 
To find out more on how avatars are being called take a look at the avatar functions in file XenForo/Template/Helper/Core.php

You may check to see if someone has a custom avatar by checking for their 'avatar_date' or 'gravatar' variables.
<xen:if is="{$user.avatar_date} OR {$user.gravatar}">

Of course the opposite applied for checking whether they are using the default avatar:
<xen:if is="!{$user.avatar_date} AND !{$user.gravatar}">

However not all templates provide this information in an array named $user. Some templates use $message for example.

Additionally, please note that some avatar calls can be forced to display as default even though the user has a custom avatar, so you may need to check for that as well (see $forceType parameter/attribute in the Core.php file).
 
Thanks so much for the advice! Do you know which templates use the $message variable vs $user?

To be more specific I'm trying to write some logic around custom user fields. So I'd like to replace default avatars with other ones. I realize that I'll have to find a way to get $user.customFields in other templates as well (right?). Is $message the same with with customFields? ($message.customFields)
 
It was just an example as I have no idea which templates you are interested in. Template message uses $message while template message_user_info uses $user.
But if all you want to do is replace the default avatar then you are probably better off editing the function _getDefaultAvatarUrl() in XenForo/Template/Helper/Core.php
Simply check for your custom filed:
if (isset($user['customFields']['your_custom_field']))

then change the path to the avatar image accordingly.
 
Hooks are for templates and besides it is now better to use a template modification for that.
As far as I know you cannot extend the _getDefaultAvatarUrl() function as it is a static one. I do believe you will have to actually edit it.
 
Hooks are for templates and besides it is now better to use a template modification for that.
As far as I know you cannot extend the _getDefaultAvatarUrl() function as it is a static one. I do believe you will have to actually edit it.
Right. Mixing up my vbulletin terminology. Hrm, editing files when updating is never fun, but if that's the only way...
 
Actually, that wasn't too hard to code in at all to code in.

I'm seeing something strange though. The avatar is not updating in thread_list_item. I'm guessing "customFields" isn't available in this template?

When looking at the output I see this:
HTML:
<span class="avatarContainer">
            <a href="index.php?members/admin.1/" class="avatar Av1s" data-avatarhtml="true"><img src="styles/default/xenforo/avatars/avatar_s.png" width="48" height="48" alt="Admin"></a>
            <a href="index.php?members/admin.1/" class="avatar Av1s miniMe" title="You have posted 1 message(s) in this thread" data-avatarhtml="true"><img src="styles/default/xenforo/avatars/avatar_other_s.png" width="48" height="48" alt="Admin"></a>
        </span>

The small image "avatar_other_s.png" is correct, but the other one should also be this image.
 
Last edited:
Which route did you take? Template modification or function edit?
If function edit then there may be situations when the custom fields are not present in the $user array and so you will need to fetch them yourself.
If you post your code I will try to help.
 
Function edit. This is what I have:

PHP:
        if (!isset($user['customFields']['gender']))
        {
            $user['customFields']['gender'] = '';
        }
       
        switch ($user['customFields']['gender'])
        {
            case 'field1':
            case 'field2':
            case 'field3':
                $gender = $user['customFields']['gender'] . '_';
                break;
               
            default:
                $gender = '';
                break;
        }

Seriously, thanks so much for the help!
 
Try this:
Code:
if (!$imagePath = self::styleProperty('imagePath'))
{
    $imagePath = 'styles/default';
}

if (!isset($user['customFields']['gender']))
{
    $user = self::_getModelFromCache('XenForo_Model_User')->getFullUserById($user['user_id']);
    $user['customFields'] = (!empty($user['custom_fields']) ? @unserialize($user['custom_fields']) : array());
}

$gender = '';
$customFields = $user['customFields'];
if (key_exists('gender', $customFields))
        $gender = strtolower($customFields['gender']) . '_';

return "{$imagePath}/xenforo/avatars/avatar_{$gender}{$size}.png";
 
Thanks, but I'm seeing the same result with that code. Although I see that is a better solution if the custom field is added to or changed.
 
Last edited:
The function we are editing is for users who do not have a custom avatar.
The avatar of the thread starter in the thread list - is that a default one or a custom one?
It would seem I'm no longer sure what it is you are after.
 
Yes, in this case the user has a default avatar (male or female was not chosen so it is jus the ? one)

All templates are default as well. Running XF 1.2.4
 
Well I don't see why it would not work.
I'm still confused as to why you have a custom field for gender when that is already available.
Perhaps if you tell me exactly how you created the custom field I could try duplicating it all for testing.
 
If the custom field is not mandatory then perhaps you need to have the default avatar and thus gender as 'other'?
$gender = 'other_';
and then change it only if the field is set?!
 
The site I'm working on has specific gender requirements, but this could be applied to any custom field to change the avatar, I'm sure.

Just to check, this is how I have things inserted - I didn't miss anything, did I?

PHP:
    protected static function _getDefaultAvatarUrl(array $user, $size)
    {
        if (!isset($user['gender']))
        {
            $user['gender'] = '';
        }

        switch ($user['gender'])
        {
            case 'male':
            case 'female':
                $gender = $user['gender'] . '_';
                break;

            default:
                $gender = '';
                break;
        }
       
        if (!$imagePath = self::styleProperty('imagePath'))
        {
            $imagePath = 'styles/default';
        }
       
    //added
        if (!isset($user['customFields']['gender']))
        {
            $user = self::_getModelFromCache('XenForo_Model_User')->getFullUserById($user['user_id']);
            $user['customFields'] = (!empty($user['custom_fields']) ? @unserialize($user['custom_fields']) : array());
        }

        $gender = '';
        $customFields = $user['customFields'];
        if (key_exists('gender', $customFields))
                $gender = strtolower($customFields['gender']) . '_';
    //end added

        return "{$imagePath}/xenforo/avatars/avatar_{$gender}{$size}.png";
    }
 
Back
Top Bottom