What is the right way to save the entry id of a sprout form submission to a custom field in that form?

I've been fiddling with the SproutForms_EntriesService, but is that the completely wrong way to go about it?

This post was helpful, but the setContentFromPost threw errors for me. I've had success with getContent though:

$entry->getContent()->title = yourValue;

Still, I'm missing something:

Specifically, I've played with the saveEntry function, and I can save values before the event is validated (where the title is updated at line 156), but since I need the element ID, the value should be set after the entry has been successfully saved. But, if I move my code inside the successful new entry block at line 176, it's a dud:

Starting @176

// Now that we have an element ID, save it on the other stuff
if ($isNewEntry)
$entryRecord->id = $entry->id;
  // my crappy code:
  $entryId = $entry->id;
  $entry->getContent()->formEntryId = $entryId;
  // end crappy code

I could be going about this totally wrong. You'll have to forgive the ignorance; I'm a Front End Dev with general Craft skills who's delving into plugin dev for the first time. So, I'm at the beginning, studying the Craft Plugin Mijingo Course and just straight up reading through plugin files…

Thanks for your help!


1 Answer 1


You're almost there, as you can see on the SproutForms_EntriesService Class at line 197 we add a SproutForms_OnSaveEntryEvent. When this event is fired you can get the form entry submitted within the param 'entry'. All you need to do is add a listener to catch this event, please go ahead and add the following code to the init() function of the plugin of your custom field type:

craft()->on('sproutForms.onSaveEntry', function (Event $event)
        $entry = $event->params['entry'];

        // Let's assume that your field handle is "custom"
            'custom' => $entry->id,
        // save it again. (avoid an infinite loop using sproutForms()->entries->saveEntry)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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