2

I have a simple task: one particular front-end form on a website is being used to update a logged in user's profile. It works. It uses the users.onSaveUser action.

I need to fire off notification emails when a particular input value is true on form submission.

My first attempt was a plugin that just watched the onSaveUser event; but while I could get that to send email, I could not seem to get access to the form fields in order to check whether the email should be sent or not. Therefore it sent emails whenever a user was saved, including when edited in the back-end.

Now I'm trying a plugin that has its own action - myPlugin/saveUser. But I am essentially duplicating the code in the existing users/saveUser function... and that makes it brittle. Is there not a way to get this done by just testing a field property and then passing everything over to the built-in users/saveUser action?

How would you do this?

Here's the simple plugin I had, which couldn't get the POST data and was therefor no good...

public function init() {
    craft()->on('users.saveUser', function(Event $event) {
        $this->requirePostRequest();

        // is in the relevant user group?
        if($event->params['user']->isInGroup('applications')){

            // check this is a submission from the front end form
            $applicationFormFrontend = craft()->request->getPost('applicationFormFrontend', 'default');

            // check the Consent field was true on this submission
            $consentGiven = craft()->request->getPost('fields[consentGiven]', 'default');

            NotifyPlugin::log("The value of applicationFormFrontend is $applicationFormFrontend.", LogLevel::Info);
            NotifyPlugin::log("The value of consentGiven is $consentGiven.", LogLevel::Info);

            if($applicationFormFrontend == '1' and $consentGiven == '1') {
                // Write Email

                ...

                // send the mail
                craft()->email->sendEmail($notificationEmail);
            }
        }
    });
}

EDIT, SOLUTION:

The reason I was not getting values for

$consentGiven = craft()->request->getPost('fields[consentGiven]', 'default');

is because it's the wrong style. It's why it was always getting 'default'. The fix is to use dot notation:

$consentGiven = craft()->request->getPost('fields.consentGiven', 'default');

The original code (as above) works with this modification.

5
  • 1
    So you'd like to send an email only if the user is being updated from a front-end form? You don't want to send an email if it's a new user or if it's being updated from the control panel? Jul 28, 2016 at 10:13
  • Also, are you able to post some/all of your form code to see if there is anything that could be causing the issue. Jul 28, 2016 at 10:22
  • Luke: spot on. I'll edit the post to include what I'd got... Jul 28, 2016 at 10:27
  • Remove $this->requirePostRequest(); that's only used in controller actions. This event gets triggered by a user save and won't know if that came from the CP or the front end. Jul 28, 2016 at 10:33
  • Luke: It doesn't make any difference whether it's there or not, I added it later trying to figure out why I wasn't getting any values for the getPost call. Well, they were always 'default', which is the fallback. Jul 28, 2016 at 10:34

2 Answers 2

2

You should be able to do something like this using the event:

craft()->on('users.onSaveUser', function(Event $event) {

  $testfield = craft()->request->getPost('someField');

  if($testfield)
  {
    // Send the email
  }

});
5
  • I tried this, but could not pull any values into $testfield, it never got any content no matter what field I tried to pull. Jul 28, 2016 at 9:55
  • This might be obvious, but are you sure your form has method="POST" or if its a GET request you should be able to remove the Post part of the function to get the value Jul 28, 2016 at 9:56
  • The onSaveUser event happens after the user is saved. So you don't need to get the field value from the post request - you'd just use the user param to check the fields value. Jul 28, 2016 at 10:16
  • True, unless its a field that doesn't get saved to the user and is purely used for the check Jul 28, 2016 at 10:17
  • Luke; you're right, except that the value will be the same for any time the user is saved again - meaning notifications sent each time. I need to test for the value of that variable - which can be done that way - but also that it's from that form, which i did by having a hidden input on that form. Jul 28, 2016 at 10:20
2

I think the following will complete all your requirements. If I've understood correctly you're trying to send an email notification by using a hidden field in a front end form. You don't want the email notification to send twice and you don't want it to send if a user is new or being saved from the control panel.

craft()->on('users.onSaveUser', function(Event $event) {

    $user = $event->params['user'];

    // Returns if the user is a new user
    if ( $event->params['isNewUser'] )
    {
      return
    }

    // If the user hasn't had an email before
    if ( $user->emailHasBeenSent == 0 )
    {
      // Get the frontEnd field value from the post request
      $frontEnd = craft()->request->getPost('frontEnd');

      // Check the frontEnd value (will be null if user saved through the CP)
      if ( $frontEnd == 1 )
      {
        // Send the email
        // Save the emailHasBeenSent on the user to true so we don't send another email the next time the user is saved
      }
    {

 });
3
  • $frontEnd = craft()->request->getPost('frontEnd'); won't work though, for the same reason the original ones don't (getPost doesn't seem to work inside the event). Jul 28, 2016 at 13:52
  • Strange, it worked for me. At the top of the event try the line Craft::dd( craft()->request->getPost() ); and see what it returns Jul 28, 2016 at 14:06
  • Oh my [redacted]. So, I didn't know about Craft::dd. Very useful, thanks. Turns out... it wasn't working for me because of this line: $consentGiven = craft()->request->getPost('fields[consentGiven]', 'default'); I'd been using fields[consentGive] which doesn't work. But fields.consentGiven does. Jul 28, 2016 at 15:12

Your Answer

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

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