XF 2.2 Add-on that modifies an existing .php file?

sub_ubi

Well-known member
How do I make an add-on that modifies an existing php file? I've made addons that modify templates but I don't know how to modify files.

I've read the "let's make an addon" doc several times but my brain doesn't work that way. I need to see a much simpler example.
 

Xon

Well-known member
Actually modifying existing files will cause the XF file hash checking to complain.

Extending classes via class extension is straight forward and most of the time you aren't going to need anything too complicated.
 

sub_ubi

Well-known member
What file you are trying to modify ?
Here's an edit I made to limit API spam, I commented out the following functions,

src/XF/Service/ApiKey/Manager.php
PHP:
    protected function contactSuperAdmins()
    {
/*         $superAdmins = $this->em()->getFinder('XF:Admin')
            ->where('is_super_admin', 1)
            ->with('User', true)
            ->fetch();

        foreach ($superAdmins AS $superAdmin)
        {
            $this->sendApiKeyNotification($superAdmin->User);
        } */
    }

    protected function sendApiKeyNotification(\XF\Entity\User $user)
    {
/*         if (!$user->email)
        {
            return;
        }

        $mail = $this->app->mailer()->newMail();
        $mail->setToUser($user)
            ->setTemplate('api_key_change', [
                'user' => $user,
                'changer' => \XF::visitor(),
                'apiKey' => $this->key
            ]);
        $mail->send(); */
    }

Actually modifying existing files will cause the XF file hash checking to complain.

And that's my problem, I have a lot of little edits like the above, and it's making upgrading & health checks difficult.
 

thetechguy

Active member
I see there is no option to enable or disable this current method that you just commented. But by doing this, it will undo changes everytime once you upgrade your XF.

Best possible way for you is to make a small addon and extend these classes and you can comment out the code using method extension.
 

sub_ubi

Well-known member
I apologize, my title says "modify a php file" and I believe I used the wrong term. I think the way people "modify" php files is by extending the class, correct? That's what I want to do.

I do NOT want to directly modify php files. I do that enough and it's a bad habit, but I don't know how to break it.

I made an addon called sub_ubi/APISpam

I made a file called src/addons/sub_ubi/APISpam/XF/Service/ApiKey/Manager.php

I don't know what to put in the file in order to coment out, remove, or nuke the two functions quoted above.
 
Last edited:

Kirby

Well-known member
I don't know what to put in the file in order to coment out, remove, or nuke the two functions quoted above.
PHP:
namespace sub_ubi\APISpam\XF\Service\ApiKey;

class Manager extends XFCP_Manager
{
    protected function contactSuperAdmins()
    {
    }

    protected function sendApiKeyNotification(\XF\Entity\User $user)
    {
    }
}
 

sub_ubi

Well-known member
Thank you all, this is going to make upgrading much easier. Now hopefully my forum can pass a health check 😊
 
Top