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

Grab custom field value

Discussion in 'XenForo Development Discussions' started by Retricide, Dec 10, 2012.

  1. Retricide

    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.
  2. Chris D

    Chris D XenForo Developer Staff Member

    This is the easiest way to grab your custom field value:

    $visitor XenForo_Visitor::getInstance();
    $value $visitor['customFields']['field1'];
    Jake Bunce and Retricide like this.
  3. Retricide

    Retricide Member

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

    Retricide Member

    Here's some more information:

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

    Thanks for the help, Chris.
  5. James

    James Well-Known Member

    What if the custom field doesn't exist, or is empty? You'll need a fallback else it'll be a broken image.
    Retricide and Jake Bunce like this.
  6. Retricide

    Retricide Member

    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?
  7. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
    Retricide likes this.
  8. Retricide

    Retricide Member

    Ah, makes sense.

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

    Thank you for your help, guys.

Share This Page