Q:

How do I extend another plugin?

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?

A:

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

A:

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

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.
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?

