1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Avatar based on Custom User Field problems.

Discussion in 'XenForo Development Discussions' started by oPryzeLP, Nov 3, 2014.

  1. oPryzeLP

    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)
    [​IMG]
    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)
    [​IMG]
    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?
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    '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.
     
  3. oPryzeLP

    oPryzeLP New Member

    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?
     
  4. oPryzeLP

    oPryzeLP New Member

    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?!
    [​IMG]
     
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
     
  6. oPryzeLP

    oPryzeLP New Member

    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.
    [​IMG]

    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 :)
     
  7. Syndol

    Syndol Guest

    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());
    }
     
    yukisakura and oPryzeLP like this.
  8. oPryzeLP

    oPryzeLP New Member

    YES! Thank you so much! Worked like a charm! :D So happy. Been trying to fix that for 4 days straight now!
     
    Syndol likes this.
  9. oPryzeLP

    oPryzeLP New Member

    Brilliant way of doing that! Definitely going to be using that technique in the future. :)
     
  10. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Note that Syndol's code will dramatically increase the query count on some pages.
     
    Aayush likes this.
  11. Nobita.Kun

    Nobita.Kun Well-Known Member

    I think you should create new field on `xf_user` then make rebuild user move custom field to new field :)
     

Share This Page