2

I've built lots of Craft 2 plugins that catch Sprout Forms submissions and integrate with external services (CRM's etc). It was easy, you'd make a plugin and catch the event going:

craft()->on('sproutForms.onSaveEntry', function(SproutForms_OnSaveEntryEvent $event) {
    // Hand submission off to your service to do some rad stuff.
});

But in Craft 3, that events functionality appears to be more complicated. All the Events & Hooks area in the Sprout Documentation just show "Example needed." in all the places where code samples would usually be. The Craft 3 plugin docs on events show a new system for registering and intercepting events - but my coffee deficient mind is struggling to figure out how to map that to Craft3/Sprout Forms 3.0.0-beta.17.

Has anyone built a Craft 3 plugin that works with Sprout Forms Beta yet? I'm even struggling to find examples of any event integration in plugins at the moment.

Halp!!!

2

I've updated the Sprout docs with examples of Craft 3 events and hooks.

In your case, the Form Entry Element extends the craft\base\Element class and also supports the same after the base element save event: EVENT_AFTER_SAVE. In your primary plugin class init method:

use barrelstrength\sproutforms\elements\Entry;
use yii\base\Event;
use Craft;

public function init()
{
    parent::init();

    Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function(Event $event) {
        if (Craft::$app->request->isSiteRequest)
        {
            // A Form Entry Element has been saved from a Front-end Form Submission
            Craft::dd($event->sender);
        }

        if (Craft::$app->request->isCpRequest)
        {
            // A Form Entry Element has been updated from the Control Panel

            Craft::dd($event->sender);
        }
    });
}

You can access the specific Form Entry Element being submitted via the $event->sender property.

  • Could you expand the example to show how to interact with the entry/form itself? I've tried $event->entry and $event->params['entry'], but neither works. – peirix Jun 15 '18 at 8:45
  • @peirix I realize my first example was for a Form Element not a Form Entry Element. I've updated the example and added bit more info on where you can find the Element in the event. – Ben Parizek Jun 15 '18 at 13:34

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.