Overwriting XenForo 'avatar' helper?

Dirt Diglett

New member
Hey there! I've just started delving into developing for XenForo and need to overwrite the avatar helper... From everything I can read I am sure i've done everything right but the helper add-on is having no effect on XenForo as it still shows the stock standard avatars.


Can anybody please tell me what I've either done wrong or missed?


The Directory of the Addon is "/library/SteamAvatar/"

The listener is "/library/SteamAvatar/Listener.php" and contains this:
PHP:
<?php

class SteamAvatar_Listener {
    public static function init(XenForo_Dependencies_Abstract $dependencies, array $data)
    {
        XenForo_Template_Helper_Core::$helperCallbacks += array(
            'avatar' => array('SteamAvatar_Helpers', 'helperAvatarUrl'),
            'avatarhtml' => array('SteamAvatar_Helpers', 'helperAvatarHtml')
        );
    }
}

?>

The Helper is "/library/SteamAvatar/Helpers.php" and contains this:
PHP:
<?php

class SteamAvatar_Helpers {
   
    public static function helperAvatarHtml(array $user, $img, array $attributes = array(), $content = '')
    {
        if (!empty($attributes['size']))
        {
            $size = strtolower($attributes['size']);

            switch ($size)
            {
                case 'l':
                case 'm':
                case 's':
                    break;

                default:
                    $size = 'm';
            }
        }
        else
        {
            $size = 'm';
        }

        // ~~~~~~~~~~ Manually set the $src for testing, to ensure the change is actually applied ~~~~~~~~~~~~
        $src = "http://media.steampowered.com/steamcommunity/public/images/avatars/ea/eab718e53d77e95c02271015f63e896171ede756_full.jpg";

        $href = self::getUserHref($user, $attributes);
        unset($attributes['href']);

        if ($img)
        {
            $username = htmlspecialchars($user['username']);
            $dimension = XenForo_Model_Avatar::getSizeFromCode($size);
           
            $image = "<img src=\"{$src}\" width=\"{$dimension}\" height=\"{$dimension}\" alt=\"{$username}\" />";
        }
        else
        {
            $text = (empty($attributes['text']) ? '' : htmlspecialchars($attributes['text']));

            $image = "<span class=\"img {$size}\" style=\"background-image: url('{$src}')\">{$text}</span>";
        }

        $class = (empty($attributes['class']) ? '' : ' ' . htmlspecialchars($attributes['class']));

        unset($attributes['user'], $attributes['size'], $attributes['img'], $attributes['text'], $attributes['class']);

        $attribs = self::getAttributes($attributes);

        if ($content !== '')
        {
            $content = " {$content}";
        }

        return "<a{$href} class=\"avatar Av{$user['user_id']}{$size}{$class}\"{$attribs} data-avatarhtml=\"true\">{$image}{$content}</a>";
    }
   
   
    public static function helperAvatarUrl($user, $size, $forceType = null, $canonical = false)
    {
        if (!is_array($user))
        {
            $user = array();
        }

        if ($forceType)
        {
            switch ($forceType)
            {
                case 'default':
                case 'custom':
                    break;

                default:
                    $forceType = null;
                    break;
            }
        }
       
        // ~~~~~~~~~~ Manually set the $url for testing, to ensure the change is actually applied ~~~~~~~~~~~~
        $url = "http://media.steampowered.com/steamcommunity/public/images/avatars/ea/eab718e53d77e95c02271015f63e896171ede756_full.jpg";

        return htmlspecialchars($url);
    }
   
    public static function getAvatarUrls(array $user)
    {
        $urls = array();

        foreach (XenForo_Model_Avatar::getSizes() AS $sizeCode => $maxDimensions)
        {
            $urls[$sizeCode] = self::getAvatarUrl($user, $sizeCode);
        }

        return $urls;
    }
   
   
    public static function getAvatarUrl(array $user, $size, $forceType = '')
    {
   
        // ~~~~~~~~~~ Manually set the $url for testing, to ensure the change is actually applied ~~~~~~~~~~~~
        $url = "http://media.steampowered.com/steamcommunity/public/images/avatars/ea/eab718e53d77e95c02271015f63e896171ede756_full.jpg";

        return $url;
    }
   
   
}

?>




Can anyone tell me what I've missed?



Thanks everyone!
 
PHP:
$href = self::getUserHref($user, $attributes);
Should be:
PHP:
XenForo_Template_Helper_Core::getUserHref($user, $attributes);
And by the way you don't need extends avatarHtml... In my case... just only extends avatar tag :D
 
PHP:
$href = self::getUserHref($user, $attributes);
Should be:
PHP:
XenForo_Template_Helper_Core::getUserHref($user, $attributes);
And by the way you don't need extends avatarHtml... In my case... just only extends avatar tag :D

Thanks for the help! I've made the adjustment, but all the avatars are still only displaying from the local copy rather than the URL in the code :(
 
Add var_dump("2"); after that line:
PHP:
$url = "http://media.steampowered.com/steamcommunity/public/images/avatars/ea/eab718e53d77e95c02271015f63e896171ede756_full.jpg";
What's result for?
 
Before we go anywhere, can we just clarify: Have you created a code event listener that listens to the init_dependencies event? You haven't explicitly mentioned performing that step, but we should check first, just in case that has been missed.

If you have done that already, it's worth doing this simple test to make sure it's working:

PHP:
<?php

class SteamAvatar_Listener {
    public static function init(XenForo_Dependencies_Abstract $dependencies, array $data)
    {
        Zend_Debug::dump('testing!');
    }
}

If you see the word 'testing' output at the top of your screen, at least we know that the code event listener is working.

This should be academic as certainly your code SHOULD be overwriting it, but just in case, try changing the code like this:

PHP:
<?php

class SteamAvatar_Listener {
    public static function init(XenForo_Dependencies_Abstract $dependencies, array $data)
    {
        XenForo_Template_Helper_Core::$helperCallbacks ['avatar'] = array('SteamAvatar_Helpers', 'helperAvatarUrl');
        XenForo_Template_Helper_Core::$helperCallbacks ['avatarhtml'] = array('SteamAvatar_Helpers', 'helperAvatarHtml');
    }
}

Finally, just try this:

PHP:
    public static function helperAvatarUrl($user, $size, $forceType = null, $canonical = false)
    {
        return '';
    }

That should straight away cause all avatars to disappear.
 
Well i
Before we go anywhere, can we just clarify: Have you created a code event listener that listens to the init_dependencies event? You haven't explicitly mentioned performing that step, but we should check first, just in case that has been missed.

If you have done that already, it's worth doing this simple test to make sure it's working:

PHP:
<?php

class SteamAvatar_Listener {
    public static function init(XenForo_Dependencies_Abstract $dependencies, array $data)
    {
        Zend_Debug::dump('testing!');
    }
}

If you see the word 'testing' output at the top of your screen, at least we know that the code event listener is working.

This should be academic as certainly your code SHOULD be overwriting it, but just in case, try changing the code like this:

PHP:
<?php

class SteamAvatar_Listener {
    public static function init(XenForo_Dependencies_Abstract $dependencies, array $data)
    {
        XenForo_Template_Helper_Core::$helperCallbacks ['avatar'] = array('SteamAvatar_Helpers', 'helperAvatarUrl');
        XenForo_Template_Helper_Core::$helperCallbacks ['avatarhtml'] = array('SteamAvatar_Helpers', 'helperAvatarHtml');
    }
}

Finally, just try this:

PHP:
    public static function helperAvatarUrl($user, $size, $forceType = null, $canonical = false)
    {
        return '';
    }

That should straight away cause all avatars to disappear.


THANK YOU! After seeing your way of doing it I instantly realized the error...

In my original Listener I was adding 'avatar' onto the end of the existing helpers, whereas with yours you've clearly overridden it, after switching the listener out for yours it is all working exactly as expected!

You are my new hero!
 
Code:
<?php

class SteamAvatar_Listener {
    public static function init(XenForo_Dependencies_Abstract $dependencies, array $data)
    {
        XenForo_Template_Helper_Core::$helperCallbacks ['avatar'] = array('SteamAvatar_Helpers', 'helperAvatarUrl');
        XenForo_Template_Helper_Core::$helperCallbacks ['avatarhtml'] = array('SteamAvatar_Helpers', 'helperAvatarHtml');
    }
}


I had the same issue and had to use this syntax. Is there any reason why the other way doesn't work?

Thank you
 
Back
Top Bottom