1

I've got an existing service based site that uses Sprout Forms to request quotes. The quote form lives on all services pages in a sidebar (and the submissions have an entries field keeping track of which service the submission came from).

I've just added some fields into the services entry to allow them to override where the quote form goes.

enter image description here

Now I need to make this work. I have a plugin listening for sproutForms.onBeforeSaveEntry events, and when it receives them I'm wanting it to adjust where it sends the notification to.

Is this possible? Is it possible to modify notificationRecipients on an individual event or is this against the form (and in either case how do I do this if I have a SproutForms_OnBeforeSaveEntryEvent)?

In either case (as I can live with the former) a code example would be amazing.

1

If your Form has an Entries Relations field that identifies the Entry that you want to get a value from to send your email, you won't need a plugin at all and can target the email in the custom field on that entry. This can even be done for advanced fields such as Super Table as you have in your screenshot.

In the case of the Super Table field in your screenshot, you could access the emailOverride value like so:

{{ object.relatedEntry.first().superTableField.emailOverride }}

If you wanted to be a bit more defensive, you could even add a conditional to check to make sure the value existed and fallback to another email adddress:

{{ object.relatedEntry.first().superTableField.emailOverride|length ? object.relatedEntry.first().superTableField.emailOverride : "fallback@email.com" }}

Note: In some cases, when looping or using conditionals your dynamic value in the recipients' field can get quite long. In older versions of Sprout Forms, the Notification Recipients value was stored as a VARCHAR field with 255 characters. If you happen to go over 255 characters in an older version of Sprout Forms, you will want to update to a more recent version of the plugin or update that database column to use the TEXT type to capture your full recipient value.

1

Based on the event you are referencing, I believe this is a question about Sprout Forms for Craft 2. In Sprout Forms for Craft 3, notifications are handled a differently.

In Sprout Forms for Craft 2, the event you'll want to use for this is the SproutForms_OnBeforePopulateEntryEvent.

You're not able to modify which value gets set directly, so you'll have to swap in the value you want using Craft Object syntax.

In your notificationRecipients field, add a value using Craft Object Syntax: {myRecipientsEmail}. Once you have called sproutForms.onBeforePopulateEntryEvent you can modify the $_POST data that gets processed when assigning the recipient value and it sure it has the value you want: $_POST['myRecipientsEmail'] = 'so-and-so@website.com'

  • Hi Ben. Thanks for the response. Sorry yes Craft 2. I'm missing something. I see in the event I can get the form (and the entry), so I can see notificationRecipients, and I can see that it's set to {myRecipientsEmail} - however setting the post data like that results in "Craft\SproutForms_EntryModel and its behaviors do not have a method or closure named "myRecipientsEmail".". How is setting the post data supposed to modify the form recipients rather than what tries to populate the entry? Thanks. – JamesNZ Jul 30 '18 at 21:26
  • On the plus side, with your help for now I can update the $form->notificationRecipients and re-save it so at least functionally I can get where I need to be - but I'm curious about your answer. – JamesNZ Jul 30 '18 at 21:32
  • Oh nope, updating that form was a bad idea. Setting the recipient and re-saving it destroyed all the form fields and tabs associated with it. Yikes. I'll wait to hear back then. – JamesNZ Jul 30 '18 at 23:14
  • Can you drop us a note at sprout@barrelstrengthdesign.com with a copy of your db or at least a representative sample of what you are doing with forms? Sounds like something else is going on. – Ben Parizek Jul 31 '18 at 3:15

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.