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

Extending the Core library?

Discussion in 'XenForo Development Discussions' started by Floren, Mar 28, 2011.

  1. Floren

    Floren Well-Known Member

    Hi guys,

    I did not deeped into Xen code properly, so I will ask the question here.
    What is the proper way to extend the XenForo_Template_Helper_Core class?

    Thanks.
     
  2. ragtek

    ragtek Guest

    AFAIK there is ATM no way to extend it
     
  3. xfrocks

    xfrocks Well-Known Member

    You can add a helper by modify the static property XenForo_Template_Helper_Core::$helperCallbacks in the init_dependencies listener (basically as soon as possible). It's a pubic property so... feel free to add your new helper or modifying the core ones

    PHP:
    XenForo_Template_Helper_Core::$helperCallbacks['myawesomehelperfunciton'] = array('My_Awesome_Helper_Class''functionX');
    Hope this helps :)
     
    Floren, Jeremy and ragtek like this.
  4. Jeremy

    Jeremy XenForo Moderator Staff Member

    xfrokcs, I was looking for this the other day. Now to turn trombone13's custom add-on into an actual add-on with no code edits! (probably still templates. haha)
     
  5. Kier

    Kier XenForo Developer Staff Member

    That's exactly what I do. :)
    PHP:
    /**
    * Called by a code event to add special helpers to the template helper core
    */
    public static function initDependencies()
    {
        
    XenForo_Template_Helper_Core::$helperCallbacks array_merge(
            
    XenForo_Template_Helper_Core::$helperCallbacks, array
            (
                
    'photothumb' => array('Thingy_Helper_FileFetch''helperPhotoThumb'),
                
    'itemthumb' => array('Thingy_Helper_FileFetch''helperItemThumb'),
                
    'video' => array('Thingy_Helper_FileFetch''helperVideo')
            )
        );
    }
     
  6. Floren

    Floren Well-Known Member

    Thanks guys, much appreciated.
     
  7. Floren

    Floren Well-Known Member

    OK, I'm back at this. So, all I have to do is call it with an init_dependencies, right?
    I used that listener and the code is working half way:
    Code:
    class Addon_CodeEvent
    {
    	private function __construct()
    	{}
    
    	public static function init()
    	{
    		XenForo_Template_Helper_Core::$helperCallbacks = array_merge(
    			XenForo_Template_Helper_Core::$helperCallbacks, array(
    				'bbcode'	=> array('Addon_Template_Helper_Client', 'helperBbCode'),
    				'usertitle'	=> array('Addon_Template_Helper_Client', 'helperUserTitle')
    			)
    		);
    	}
    }
    
    class Addon_Template_Helper_Client extends XenForo_Template_Helper_Core
    {
    
    	public static function helperBbCode($parser, $text)
    	{
    		var_dump($text);
    		exit;
    		// broken
    	}
    
    	public static function helperUserTitle($user, $allowCustomTitle = true)
    	{
    		var_dump($user);
    		exit;
    		// works
    	}
    }
    I only get a var_dump on helperUserTitle, the helperBbCode event does nothing...
     
  8. Kier

    Kier XenForo Developer Staff Member

    There's no need to formally extend XenForo_Template_Helper_Core with an inheriting class - just add static properties and methods to it from another class:

    PHP:
    class Addon_CodeEvent
    {
        public static function 
    initDependencies()
        {        
            
    XenForo_Template_Helper_Core::$helperCallbacks['bbcode']
                = array(
    'Addon_CodeEvent''helperBbCode');

            
    XenForo_Template_Helper_Core::$helperCallbacks['helperUserTitle']
                = array(
    'Addon_CodeEvent''helperUserTitle');
        }
        
        public static function 
    helperBbCode($parser$text)
        {
            
    // do stuff
        
    }
        
        public static function 
    helperUserTitle($user$allowCustomTitle true)
        {
            
    // do other stuff
        
    }
    }
    Try that instead and see if you have more luck. If that doesn't work, I'm going to need more info.

    Bear in mind that the bbcode helper there is used very very infrequently - we do almost all of our bbcode parsing in View.
     
    Fuhrmann likes this.
  9. Fuhrmann

    Fuhrmann Well-Known Member


    What about if we are trying to extend the XenForo_Template_Compiler? There is no static var so we can add our own tags.
     
  10. semprot

    semprot Active Member

    Until now there is still no way to extend core helper?
    I've tried this but it didn't work
    PHP:
    <?php
    class Semprot_RegExpCensor_InitDependenciesListener {
        public static function 
    init()
        {
            
    XenForo_Template_Helper_Core::$helperCallbacks['string']
                = array(
    'Semprot_RegExpCensor_InitDependenciesListener''customCensorString');
        }

        public static function 
    customCensorString($string, array $words null$censorString null)
        {
            
    // do stuff
            
    $string str_replace('p''q'$string);
            return 
    $string;
        }
    }
    ?>
     

Share This Page