Overwrite plugin method with own plugin class

Hello, I’m trying to extend the JanVince\SmallContactForm to add some IP white/blacklisting.

Now I can’t get it running. I did my own plugin and added class ContactFormExtensionExtend extends SmallContactForm in Acme\ContactFormExtension\Classes.

In my class ContactFormExtensionExtend , I just added the onFormSend() method from SmallContactForm with some logs.

I try to register my new class in my Plugin.php like this:

public function register()
{
    $this->app->bind(SmallContactForm::class, ContactFormExtensionExtend::class);
}

Then I try to listen to the onFormSend() in boot like this:

public function boot()
{
        $smallContactForm = app(SmallContactForm::class);
        $contactFormExtensionExtend = app(ContactFormExtensionExtend::class);
        
        if (method_exists($smallContactForm, 'onFormSend')) {
            $smallContactForm->onFormSend = function () use ($contactFormExtensionExtend) {
                Log::error('onFormSend ');
                $contactFormExtensionExtend->onFormSend();
            };
        }
}

But i can’t see the log “onFormSend” and my own method onFormSend isn’t called. Instead the onFormSend from SmallContactForm is called.

I guess, I don’t register my class right…
How can I fix this?

I would take another approach in simply extending the component class

Class YourComponent extends \JanVince\SmallContactForm\ComponentsSmallContactForm {


/**
   * your Form handler here
   **/
  public function onFormSend(){
   ...
  }
}```
1 Like

Hey Chris! Thanks for your reply!

I was thinking exactly the same in the bus on my way to work :grinning:

So I did this now:

...
use JanVince\SmallContactForm\Components\SmallContactForm;
...

class ContactFormExtension extends SmallContactForm
{

    private $validationRules;
    private $validationMessages;
    private $validationReCaptchaServerName;

    private $postData = [];
    private $post;

    private $formDescription;
    private $formDescriptionOverride;

    private $formRedirect;
    private $formRedirectOverride;

    public function componentDetails() {
        return [
            'name'        => 'SmallContactForm Erweiterung',
            'description' => 'Fügt eine IP Überprüfrungsmöglichkeit hinzu'
        ];
    }

    public function onFormSendExtend() {
        // mostly the same as onFormSend in SmallContactForm component, but when methods were public, I I added parent:: instead of $this
        // the methods that are private are setFieldsValidationRules and setPostData. So I keopt them in my class extension
        ...
    }

    // I also left the method getFormAttributes and renamed it to getFormAttributesExtend
    public function getFormAttributesExtend(){

        $attributes = [];

        $attributes['request'] = $this->alias . '::onFormSendExtend'; // here I'm calling my own onFormSend
            ...
}

So, I copied the content from plugins\janvince\smallcontactform\components\smallcontactform\scf-form.htm to my extend plugin compontent default.htm and changed __SELF__.getFormAttributes to __SELF__.getFormAttributesExtend, which sets

$attributes['request'] = $this->alias . '::onFormSendExtend';

and my own onFormSend get called.

Of course I then replaced the SmallContactForm component on my page with my own extension component.

It took me a while to figure that out, but now I think, I can now implement my IP check.

PS UPDATE:
This is an unfinished state of this extension, so if you intend to do an extension of the SmallContactForm yourself like this, don’t forget to put the other partials in the component folder and apply GoogleCaptcha and other functionality, if you need it. I hope, I find time to publish my finished SmallContactForm extension on GitHub. I try to update you here…

Hey! Here’s the link to my repo of the extension of Jan Vinces SmallContactForm plugin:

1 Like