I'd like to know if it's possible to extend another plugin with my own.

I'm currently using the Sprout Forms plugin to create a contact form on our website, and I would like to extend that so when a user submits the form, it signs them up to a campaign monitor list as well (with their permission of course).

Is this possible with Craft? If so where in the documentation would I look for more info?


I believe what you asking about are Hooks and Events. Craft has several of them listed in the Hooks Reference and Events Reference. And Craft also allows us to add Hooks and Events in our plugins, which other plugins can leverage.

In the case of Sprout Forms, as of this post, we don't have any documented events yet, but there are a couple in there and we'll be formalizing them more in the next release.

You'll probably want to take a look at sproutForms.beforeSaveEntry Event. Right now, plugins like Sprout Invisible Captcha, SpamGuard, and Snaptcha use that Event to add spam protection to Forms. You could use that event as well to check for a confirmation field, and add a user to a list in Campaign Monitor.

Here's an example of how SpamGuard uses the event, which you can see here in the init() method of primary plugin class file (I've added the comments myself in relation to your question):

# Check for an Event in your plugins init method
public function init()
    # If you add a setting that allows a user to turn your integration on or off
    # you can check for that setting here, before you run your code
    if ($this->getSettings()->getAttribute('enableSproutFormsSupport'))
        # Listen for the event where you want to add behavior
        craft()->on('sproutForms.beforeSaveEntry', function(Event $event)
            # Do your thing!
            $spam = spamGuard()->detectDynamicFormSpam($event->params['entry']);
            if ($spam)
                $event->fakeIt  = true;
                $event->isValid = false;
  • Haha, I was about to ping you and say you might want to have a look at this question, but it looks like you beat me with an answer by a few seconds :) Oct 30 '14 at 20:49
  • Thanks Ben, I've just tried this. I've created a plugin and activated it, but it's like the event never fires. I've added the init function, removed the if ($this->getSettings()->getAttribute('enableSproutFormsSupport')) if statement, and inside the event I've just added echo 'test'; exit;. I never see 'test', it just goes through like normal.
    – KeironLowe
    Oct 31 '14 at 11:23
  • Infact, I've just tried with sproutForms.saveEntry and it worked. Although I'm not sure if this is the best place to do this?
    – KeironLowe
    Oct 31 '14 at 11:36
  • Hey @KeironLowe, in the next release we'll have sproutForms.beforeSaveEntry Event which will be what you want to use moving forward. In the current version you should find a sproutForms.onBeforeSubmitForm Event, but we haven't documented it because things are still changing under the hood and it will no longer be available in the next release. Oct 31 '14 at 14:57
  • Ahh makes sense, I'll use that instead then!
    – KeironLowe
    Oct 31 '14 at 16:38

There are two ways you could get something like this to work:

  1. If the other plugin fires an event when at a point in the code where your plugin wants to do something, you can just listen for that. (You will have to look through the plugin’s source code to be sure, if they haven’t documented it.) This is the cleanest route, so even if the event doesn’t exist, it’s probably worth writing to the developer and requesting it, explaining your use case.

  2. If the other plugin was written according to best practices, then all of the heavy-lifting business logic is tucked away in a service class. If that’s the case, your plugin could provide its own controller action which replaces the other plugin’s controller, as well as runs your own custom code. HTML forms would then just need to be updated post to your own custom controller rather than the other plugin’s. Here’s an example of this sort of thing: Booking paid-for events: How?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.