Is It Possible to Override <xen:username /> At All?

James

Well-known member
<xen:username /> directly calls XenForo_Template_Helper_Core::userNameHtml() and there doesn't seem to be a hook that allows me to either:
a) extend the compiler and specify a new callback
b) extend the XenForo_Template_Helper_Core::userNameHtml() to specify a new callback

I know that in 1.1 the <xen:username /> is called via callHelper() for things like this, but is it possible at present?
 
No, as of 1.1 it is not possible. This helper is called from XenForo_Template_Helper_Core class, which is not extendable.
There is a discussion here - hopefully, this will be added in one of the future releases (you might actually help if you also post in that thread and ask for them to do it).
Meanwhile, as of now the only possibility to overide the helper is to hack into the source core of XenForo_Template_Helper_Core. However, there are a few limitations to that - one of them is the simple fact that some of the templates use {$user.username} instead of <xen:username /> - there are not too many of them, but still that limits the usefulness of hacking into username helper code.
 
You can by doing something like this.


In the init_dependencies listener

PHP:
MyAddon_Template_Helper::setHelperUsernameOriginalCallback(XenForo_Template_Helper_Core::$helperCallbacks['username']);
XenForo_Template_Helper_Core::$helperCallbacks['username'] = array('MyAddon_Template_Helper', 'helperUsername');

Then in your MyAddon_Template_Helper:

PHP:
class MyAddon_Template_Helper {
protected static $_helperUsernameOriginalCallback = false;
 
public static function setHelperUsernameOriginalCallback($callback) {
if ($callback[0] == 'self') $callback[0] = 'XenForo_Template_Helper_Core';
self::$_helperUsernameOriginalCallback = $callback;
}
 
public static function helperUsername(array $user, $class = '', $rich = false) {
$user['username'] = 'some other interesting name';
return call_user_func(self::$_helperUsernameOriginalCallback , $user, $class, $rich);
}
 
}

This code is not tested though :P
 
Top Bottom