Grab custom field value

Retricide

Member
I can't seem to figure out how to get the value of a custom field (ID = "field1") in Template/Helper/Core.php.

I'm trying to grab the value of a custom field and then, based on what they entered, select an avatar for them.

I have the avatar selection working fine using the username - because I am using the value from user['username'] - but I don't know how to get it working for a custom field.

Any advice would be much appreciated.
Thank you.
 
This is the easiest way to grab your custom field value:

PHP:
$visitor = XenForo_Visitor::getInstance();
$value = $visitor['customFields']['field1'];

Worked perfectly!

Thank you very much.

Actually, there's a problem.

I'm using $user['customFields']['field1'] to grab the value of a field for the user that is passed into the method, in this case _getDefaultAvatarUrl. I then return a url containing $user to serve as the avatar image.

However, it seems that this only works on certain parts of the site, like in the user panel. Everywhere else, like on the "members" page, avatars are blank.

Any idea why this is happening?
 
Here's some more information:

Code:
    protected static function _getDefaultAvatarUrl(array $user, $size)
    {
        $value = $user['customFields']['MCUsername'];
        if ($size=='s')
            $size=48;
        else if ($size=='l')
            $size=192;
        else
            $size=96;
        return "https://minotar.net/avatar/{$value}/{$size}.png"; 
    }

Screenshot:
Screenshot.webp

Thanks for the help, Chris.
 
Here's some more information:

Code:
    protected static function _getDefaultAvatarUrl(array $user, $size)
    {
        $value = $user['customFields']['MCUsername'];
        if ($size=='s')
            $size=48;
        else if ($size=='l')
            $size=192;
        else
            $size=96;
        return "https://minotar.net/avatar/{$value}/{$size}.png";
    }

Screenshot:
View attachment 37892

Thanks for the help, Chris.
What if the custom field doesn't exist, or is empty? You'll need a fallback else it'll be a broken image.
 
What if the custom field doesn't exist, or is empty? You'll need a fallback else it'll be a broken image.

All of the 2 members have the custom field set (it's a required field, too), so shouldn't it work fine currently?

If $value is set to user['username'] instead of user['customFields']['MCUsername'] everything works perfectly. However, I want it to use a custom field instead.

Perhaps there's a different way to reference custom fields for functions in XenForo_Template_Helper_Core?
 
The custom user fields may not always be available in the $user record in that function. You can't rely on it. The 'customFields' array might still be serialized, or it might not exist at all which pretty much kills your idea of using a custom field for an avatar. If you were to make an addon to add a column to the xf_user table then that should always be available in the avatar function.
 
The custom user fields may not always be available in the $user record in that function. You can't rely on it. The 'customFields' array might still be serialized, or it might not exist at all which pretty much kills your idea of using a custom field for an avatar. If you were to make an addon to add a column to the xf_user table then that should always be available in the avatar function.

Ah, makes sense.

Guess I'm going to just stick with the username.

Thank you for your help, guys.
 
Top Bottom