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.
 
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.
 
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.
 
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.
 
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:
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)
    {
    }
}
 
Thank you all, this is going to make upgrading much easier. Now hopefully my forum can pass a health check 😊
 
I feel like I'm missing something when modifying a file in another addon.

For example,

I'm attempting to modify Xon's "Warning Improvements" to anonymize the thread creator in Warning summaries. I want to change a single line in
SV\WarningImprovements\XF\Service\User\Warn.php,

$warningUser = \XF::visitor(); //$this->user;
to
$warningUser = \XF::em()->find('XF:User', 1234);

However, it doesn't work. Here's what my sub_ubi\ModAnon\XF\Service\User\Warn.php looks like,

PHP:
namespace sub_ubi\ModAnon\XF\Service\User;

class Warn extends XFCP_Warn
{
    public function warningActionNotifications()
    {
        $options = $this->app->options();
        $postSummaryForumId = (int)($options->sv_post_warning_summaryForum ?? 0);
        $postSummaryThreadId = (int)($options->sv_post_warning_summary ?? 0);
        if (!$postSummaryForumId && !$postSummaryThreadId)
        {
            return;
        }
        $params = $this->warningRepo->getSvWarningReplaceables(
            $this->user,
            $this->warning,
            null,
            true,
            $this->contentAction, $this->contentActionOptions
        );
        // $warningUser = \XF::visitor(); //$this->user;
        $warningUser = \XF::em()->find('XF:User', 1234);

... snip rest of the function ....
}

No luck. I also tried copying over the entire file, no luck with that either.
 
Change the execution order to be higher than the class extension in the original addon. I believe they're loaded alphabetically if the execution order is the same, so yours will be loaded first, the extension from SV\WarningImprovements will actually be extending yours rather than the other way round.
 
Top Bottom