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

Having trouble creating my first XenForo addon

Discussion in 'XenForo Development Discussions' started by DeltaHF, Jan 13, 2014.

  1. DeltaHF

    DeltaHF Well-Known Member

    I'm trying to create an add-on which modified what happens when a user requests an account confirmation email. After some searching, I think I need to override actionResend() in /library/XenForo/ControllerPublic/AccountConfirmation.php.

    I've never made a XenForo add-on before, and I'm trying to keep it as simple as possible while getting my feet wet. I've created the following files in a /library/DeltaHF directory:

    /library/DeltaHF/ActivationMod.php
    PHP:
    <?php
    class DeltaHF_ActivationMod extends XFPC_DeltaHF_ActivationMod
    {
        public function 
    actionResend()
        {
            
    // send an email to let me know we made it in here       
            
    mail("my_email_address@example.com""Extension Test""It worked!");
           
            return 
    parent::actionResend();
        }
    }
    /library/DeltaHF/Listener.php
    PHP:
    <?php

    class DeltaHF_Listener
    {
        public static function 
    loadClassListener($class, array &$extend)
        {
            if (
    $class == 'XenForo_ControllerPublic_AccountConfirmation')
            {
                
    $extend[] = 'DeltaHF_ActivationMod';
            }
        }
    }
    I then defined a new Event Listener as follows:
    Screen Shot 2014-01-13 at 2.33.58 AM.png

    So far, so good, until I actually try to re-send an activation email. That throws an error warning on the page and puts the following Fatal Error into the Server Error Log:
    What am I doing wrong? I can tell it has to be something simple, but I'm lost.
     
  2. EQnoble

    EQnoble Well-Known Member

    I think you might have went dyslexic on a few keystrokes...

    XFPC_DeltaHF_ActivationMod

    I think it should be
    XFCP_DeltaHF_ActivationMod
     
    DeltaHF likes this.
  3. DeltaHF

    DeltaHF Well-Known Member

    I'm not sure if I should be happy or sad. :eek: Thank you!
     
  4. EQnoble

    EQnoble Well-Known Member

    It doesn't matter if it works. ;) Your welcome.
     
  5. DeltaHF

    DeltaHF Well-Known Member

    OK - another problem/question.

    In my listener, before calling the parent method, I send myself an email for a quick sanity check. However, that email is being sent to me twice, indicating that the listener is being executed twice. Only one re-activation email is sent, however.

    Have I missed something else?
     
  6. AndyB

    AndyB Well-Known Member

    You always want to call the parent first.

    PHP:
    <?php
    class DeltaHF_ActivationMod extends XFPC_DeltaHF_ActivationMod
    {
        public function 
    actionResend()
        {
            
    // call parent function
            
    parent::actionResend();        
            
            
    // send an email to let me know we made it in here       
            
    mail("my_email_address@example.com""Extension Test""It worked!");
           
            return 
    parent::actionResend();
        }
    }
     
  7. DeltaHF

    DeltaHF Well-Known Member

    Hmm, OK, I guess this is a hole in my OOP knowledge. Wouldn't that just call parent::actionResend() twice?
     
  8. Liam W

    Liam W Well-Known Member

    Yes,

    it should really be like this:

    PHP:
    <?php
    class DeltaHF_ActivationMod extends XFPC_DeltaHF_ActivationMod
    {
        public function 
    actionResend()
        {
            
    // call parent function
            
    $response parent::actionResend();       
           
            
    // send an email to let me know we made it in here      
            
    mail("my_email_address@example.com""Extension Test""It worked!");
          
            return 
    $response;
        }
    }
     
  9. Syndol

    Syndol Guest

    There was nothing wrong with your original function. No need to change it.
    You are receiving two emails because the function is being called twice. Once to confirm the action and once to actually send the confirmation email.

    Edit:
    When you actually do add your own code to that function you should do so only if it is upon confirmation: if ($this->isConfirmedPost())
     
    Last edited by a moderator: Jan 13, 2014
    DeltaHF likes this.
  10. Liam W

    Liam W Well-Known Member

    You really shouldn't call the parent more than once, it may cause multiple insertions etc.

    Also, the main function checks for confirmed post, so as long as you call it at the beginning no issue...

    Liam
     
  11. DeltaHF

    DeltaHF Well-Known Member

    Thank you, guys, I've got it sorted now:
    PHP:
    <?php

    class DeltaHF_ActivationMod extends XFPC_DeltaHF_ActivationMod
    {
      public function 
    actionResend()
      {
         
    $response parent::actionResend();

         if ( 
    $this->isConfirmedPost() )
         {
              
    mail("my_email_address@example.com""Extension Test""It worked!");
         }

         return 
    $response;
       }
    }
    For my own curiosity, how does actionResend() actually get called? I found no references to that method anywhere else in the source code, and I don't understand how/where it gets called - much less, called twice!
     
  12. Syndol

    Syndol Guest

    It is being called from template notice_confirm_email
    Once you click on that it calls the function which displays a confirmation template. Once confirmed, the function is called again in order to execute the relevant code.
     
    DeltaHF and AndyB like this.

Share This Page