Avatar based on Custom User Field problems.

oPryzeLP

New member
Hello,
I run a Minecraft Server forum and I wanted to let people enter their Minecraft username in their account details and automatically change their avatar to their Minecraft skin.

I added the custom field and everything works fine. I made an edit to library/Xenforo/Template/Helper/Core.php

Code:
protected static function _getDefaultAvatarUrl(array $user, $size)
    {
        $customFields = unserialize($user['custom_fields']);

        if(!empty($customFields['_minecraft_username']))
        {
            return "http://cravatar.eu/helmavatar/{$customFields['_minecraft_username']}/".XenForo_Model_Avatar::getSizeFromCode($size).".png"; 
        }
        else
        {
            return "http://cravatar.eu/helmavatar/steve/".XenForo_Model_Avatar::getSizeFromCode($size).".png"; 
        }
    }

Basically, if a user enters their username, it will use their skin. However if the field is blank it will default to the "steve" skin. All of this works fine except that the steve skin is used when there are smaller versions of the Avatar.

Users with Custom Avatars display fine (Darth Vader for example)
Users that haven't entered their username display fine
Users that HAVE entered their username still shows the steve skin (The user to the right of Vader)
2f045fd0edfbe85004a2b62aa27144c9.png

HOWEVER, On that specific user next to Vader, their Avatar works as it should.
(The user being shown is the same user that is to the right of Vader)
1a113349da6b1e07a8e1d302eaaf8430.png

This occurs everywhere a "small" avatar icon is present.

I have already rebuilt the caches and I watched a friend of mine create an account. The issue is still present on his account as well.

Any ideas?
 
'custom_fields' probably isn't available in the user record in that area. An addon or code edit is required to do the proper join for those user records.
 
'custom_fields' probably isn't available in the user record in that area. An addon or code edit is required to do the proper join for those user records.
Thanks for the reply! How extensive of a code edit? I do not know PHP fluently, however I can try my best to use my knowledge of C++ to work something out.

EDIT: If 'custom_fields' isn't in that area, how do you explain it working properly for the larger avatar images?
 
I have taken a screenshot that shows my problem PERFECTLY.
The user has a StampyLongnose skin (orange) but the default Steve skin in the smaller avatar. Same user, Matthew Garbelman, two different default avatars. The correct one is the orange one, the one using the custom field. WHY?!
4a99187a3065be0fd0ba8d0ef3acbf5b.png
 
Thanks for the reply! How extensive of a code edit? I do not know PHP fluently, however I can try my best to use my knowledge of C++ to work something out.

EDIT: If 'custom_fields' isn't in that area, how do you explain it working properly for the larger avatar images?

You can post a request here:

https://xenforo.com/community/forums/resource-and-add-on-requests.68/

The member card is a separate request. It's a different "area" with different code behind it.
 
You can post a request here:

https://xenforo.com/community/forums/resource-and-add-on-requests.68/

The member card is a separate request. It's a different "area" with different code behind it.
Thanks.

That raises a question though, aren't they in the same "area" here?
You can see Newest Members is still bugged showing the Steve skin instead of the custom field skins.
a0ce699ffd999f23bd8f1f721e0f0e75.png


After a lot of searching, I have discovered that what you said is true. For example, in profile_post_list_item_simple, there is the line of code to display the avatar:
Code:
<xen:avatar user="$profilePost" size="s" img="true" />

In every scenario that user="something other than $user", the custom fields from the user aren't "passed." I'm currently searching through ProfilePost.php to try and find a spot where I can add it in somehow, but I have no clue how to go about doing that. Are you able to help me with one section, for example... profile posts? If so, I can do everything else :)
 
In _getDefaultAvatarUrl all you have to do is check to see whether your custom field is set and if not fetch the user in order to obtain it.
Easiest way would be to simply fetch the full user and then unserialize the custom fields.

if (!isset($user['customFields']['your_field']))
{
$user = self::_getModelFromCache('XenForo_Model_User')->getFullUserById($user['user_id']);
$user['customFields'] = (!empty($user['custom_fields']) ? @unserialize($user['custom_fields']) : array());
}
 
In _getDefaultAvatarUrl all you have to do is check to see whether your custom field is set and if not fetch the user in order to obtain it.
Easiest way would be to simply fetch the full user and then unserialize the custom fields.

if (!isset($user['customFields']['your_field']))
{
$user = self::_getModelFromCache('XenForo_Model_User')->getFullUserById($user['user_id']);
$user['customFields'] = (!empty($user['custom_fields']) ? @unserialize($user['custom_fields']) : array());
}
YES! Thank you so much! Worked like a charm! :D So happy. Been trying to fix that for 4 days straight now!
 
I think you should create new field on `xf_user` then make rebuild user move custom field to new field :)
 
Top Bottom