<p>There are two ways you could get something like this to work:</p>

<ol>
<li><p>If the other plugin fires an <a href="http://buildwithcraft.com/docs/plugins/hooks-and-events#events" rel="nofollow noreferrer">event</a> 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.</p></li>
<li><p>If the other plugin was written according to best practices, then all of the heavy-lifting business logic is tucked away in a <a href="http://buildwithcraft.com/docs/plugins/services" rel="nofollow noreferrer">service class</a>. 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: <a href="https://craftcms.stackexchange.com/questions/2997/booking-paid-for-events-how/3006#3006">Booking paid-for events: How?</a></p></li>
</ol>

CC BY-SA 3.0
2014-10-30T20:47:53.637Z
2014-10-30T20:47:53.637Z
2017-04-13T12:51:00.303Z
Brandon Kelly
<p>I believe what you asking about are <a href="http://buildwithcraft.com/docs/plugins/hooks-and-events" rel="nofollow">Hooks and Events</a>.  Craft has several of them listed in the <a href="http://buildwithcraft.com/docs/plugins/hooks-reference" rel="nofollow">Hooks Reference</a> and <a href="http://buildwithcraft.com/docs/plugins/events-reference" rel="nofollow">Events Reference</a>.  And Craft also allows us to add Hooks and Events in our plugins, which other plugins can leverage.</p>

<p>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.</p>

<p>You'll probably want to take a look at <code>sproutForms.beforeSaveEntry</code> Event.  Right now, plugins like <a href="http://sprout.barrelstrengthdesign.com/craft-plugins/invisible-captcha" rel="nofollow">Sprout Invisible Captcha</a>, <a href="https://github.com/selvinortiz/craft.spamguard" rel="nofollow">SpamGuard</a>, and <a href="https://www.putyourlightson.net/craft-cms-plugins" rel="nofollow">Snaptcha</a> 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.</p>

<p>Here's an example of how SpamGuard uses the event, which you can <a href="https://github.com/selvinortiz/craft.spamguard/blob/master/SpamGuardPlugin.php" rel="nofollow">see here</a> in the <code>init()</code> method of primary plugin class file (I've added the comments myself in relation to your question):</p>

<pre><code># 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-&gt;getSettings()-&gt;getAttribute('enableSproutFormsSupport'))
    {
        # Listen for the event where you want to add behavior
        craft()-&gt;on('sproutForms.beforeSaveEntry', function(Event $event)
        {
            # Do your thing!
            $spam = spamGuard()-&gt;detectDynamicFormSpam($event-&gt;params['entry']);
            if ($spam)
            {
                $event-&gt;fakeIt  = true;
                $event-&gt;isValid = false;
            }
        });
    }
}
</code></pre>

CC BY-SA 3.0
2014-10-30T20:49:18.007Z
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 :)
Brandon Kelly
CC BY-SA 3.0
2014-10-31T11:23:06.627Z
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
CC BY-SA 3.0
2014-10-31T11:36:15.013Z
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
CC BY-SA 3.0
2014-10-31T14:57:59.933Z
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.
Ben Parizek
CC BY-SA 3.0
2014-10-31T16:38:53.093Z
Ahh makes sense, I'll use that instead then!
KeironLowe
CC BY-SA 3.0
2014-10-30T20:47:07.930Z
2014-10-31T18:45:55.327Z
2014-10-31T18:45:55.327Z
Ben Parizek
<p>I'd like to know if it's possible to extend another plugin with my own.</p>

<p>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).</p>

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

<plugin-development><form><events><plugin-sproutforms><extends>
How do I extend another plugin?
# How do I extend another plugin?

(Asked by: username_0 on 2014-10-30)

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?

**Tags:** plugin-development, form, events, plugin-sproutforms, extends


---

## 2 answers

### Answer by username_2 on 2014-10-30. Score: 6 | Accepted answer

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;
            }
        });
    }
}

> **Comments:**
> **username_1**: 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 :)  
> **username_0**: 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.  
> **username_0**: 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?  
> **username_2**: Hey @username_0, 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.  
> **username_0**: Ahh makes sense, I'll use that instead then!  

---

### Answer by username_1 on 2014-10-30. Score: 3

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

1. If the other plugin fires an [event](http://buildwithcraft.com/docs/plugins/hooks-and-events#events) 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](http://buildwithcraft.com/docs/plugins/services). 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?](https://craftcms.stackexchange.com/questions/2997/booking-paid-for-events-how/3006#3006)


---

