• 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

DeltaHF

Well-known member
#1
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:
ErrorException: Fatal Error: Class 'XFPC_DeltaHF_ActivationMod' not found - library/DeltaHF/ActivationMod.php:10
What am I doing wrong? I can tell it has to be something simple, but I'm lost.
 

DeltaHF

Well-known member
#5
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?
 

AndyB

Well-known member
#6
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();
    }
}
 

Liam W

Well-known member
#8
Hmm, OK, I guess this is a hole in my OOP knowledge. Wouldn't that just call parent::actionResend() twice?
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;
    }
}
 
S

Syndol

Guest
#9
However, that email is being sent to me twice, indicating that the listener is being executed twice.
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:

Liam W

Well-known member
#10
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
 

DeltaHF

Well-known member
#11
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!
 
S

Syndol

Guest
#12
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.