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

Event Listener Hints

Discussion in 'XenForo Development Discussions' started by Chris D, Jun 28, 2013.

  1. Chris D

    Chris D XenForo Developer Staff Member

    So now when we create our event listeners, e.g. load_class_controller, we can specify an event hint. In the case of that event, the hint is the class name being called, e.g. XenForo_ControllerPublic_Thread.

    This clearly has benefit if you've created one event listener to extend one controller. But what about if you're extending multiple controllers?

    Before XenForo 1.2 I'd simply have one code event listener per code event. So if I were extending 5 controllers I'd do so in a single code event listener method via switch / case statements.

    Now we have event hints. What's the best thing to do?

    One code event listener per controller and make use of event hints?
    One code event listener for all my controllers and not use event hints?
     
    Last edited: Jun 29, 2013
    r1pe and Adam Howard like this.
  2. Robbo

    Robbo Well-Known Member

    Just don't set the hint.

    Edit: oh I see some other stuff in there, just set the hint to "_" to work on everything I think? This whole hint stuff seems a bit pointless to me, the performance increases would be so minimal from loading a single class into memory which usually just has static methods.
     
    Chris D likes this.
  3. tyteen4a03

    tyteen4a03 Well-Known Member

    If it took a comma-separated list I would be very greatful.
     
    Xon, AndyB, AlexT and 1 other person like this.
  4. Daniel Hood

    Daniel Hood Well-Known Member

    Can't you leave it blank and be fine? I haven't upgraded from 1.2 Beta 3 yet so I might be wrong but I've been leaving it blank...
     
  5. tyteen4a03

    tyteen4a03 Well-Known Member

    Yes, event hints are entirely optional.
     
  6. Jeremy

    Jeremy XenForo Moderator Staff Member

    Yes. I have yet to set one.
     
  7. AlexT

    AlexT Well-Known Member

    You can. But it's a nice new feature helping to improve performance by avoiding to load your addon classes and methods for each and every controller, datawriter, etc. In particular if you have a lot of addons, this has the potential to make quite a difference. I am currently modifying my own addons to make use of the hinting feature-

    For example, as long as only one hint is supported, I do it like this:

    PHP:
    <?php
    class MobileRead_HidePollResults_Listener_Proxy
    {
       public static function 
    loadControllerPublicForum($class, array &$extend)
       {
          if (
    $class == 'XenForo_ControllerPublic_Forum')
          {
             
    $extend[] = 'MobileRead_HidePollResults_ControllerPublic_Forum';
          }
       }

       public static function 
    loadControllerPublicThread($class, array &$extend)
       {
          if (
    $class == 'XenForo_ControllerPublic_Thread')
          {
             
    $extend[] = 'MobileRead_HidePollResults_ControllerPublic_Thread';
          }
       }
    }
    Then I enter two code event listeners in the xF admin panel, both listening to load_class_controller, only with different event hints, and with the respective callback method from above.

    In pre-1.2, I would only use one event listener and my class would look like this:
    PHP:
    <?php
       
    /* pre-1.2 */
       
    public static function loadController($class, array &$extend)
       {
          switch (
    $class)
          {
             case 
    'XenForo_ControllerPublic_Forum':
                
    $extend[] = 'MobileRead_HidePollResults_ControllerPublic_Forum';
                break;

             case 
    'XenForo_ControllerPublic_Thread':
                
    $extend[] = 'MobileRead_HidePollResults_ControllerPublic_Thread';
                break;
          }
       }
    ?>
     
  8. Chris D

    Chris D XenForo Developer Staff Member

    Of course you don't need to even wrap your $class in a conditional anymore...

    PHP:
    <?php



    class YourAddOn_Listener

    {

    public static function 
    extendThreadController($class, array &$extend)

    {

    $extend[] = 'YourAddOn_ControllerPublic_Thread';

    }

    }
     
    tyteen4a03 likes this.
  9. AlexT

    AlexT Well-Known Member

    You are right, of course. But I decided to leave it for the time being in the case I forget to fill out the hint or if backward compatibility with 1.1.x is required.
     
    Chris D likes this.
  10. Chris D

    Chris D XenForo Developer Staff Member

    That is true. Currently I'm working on 1.2 exclusive stuff so BC isn't an issue. But definitely a consideration for 1.1.x compatibility.
     
  11. Radi

    Radi Member

    @Chris D would you please explain what the "Event Listener Hints" are used for and how to use them?
     
  12. Xon

    Xon Well-Known Member

    It is a significant performance improvement on larger and more active forums, where your event listener is only called for the classes required.

    Otherwise 'load_class' without a hint is called on every class used by XenForo which is extendable. This is an extra function call, and conditional to be parsed.

    Rather than "load_class_xxx" I just use "load_class" and a hint, and then in the method I prefix a string which maps to a standard structure for how I override stuff.

    For example, one of my listeners looks like:
    Code:
    class SV_WarningImprovements_Listener
    {
        const AddonNameSpace = 'SV_WarningImprovements';
    
        public static function load_class($class, array &$extend)
        {
            $extend[] = self::AddonNameSpace.'_'.$class;
        }
    }
    
    The AddonNameSpace is then sometimes used in the installer bits as required.

    This does mean that I replicate the XenForo code layout under the addon's library folder, which makes matching the XenForo Code and my extension code quite easy.
     
    Marcus and Steve F like this.
  13. Radi

    Radi Member

    In my case, I am already using load_class_xxx. Does that mean that I do not need to use the "Event Listener Hints"?
     
  14. Xon

    Xon Well-Known Member

    Using event listener hints is still a good idea when using any hintable event listener.
     

Share This Page