1

I want to assign a new user to a user group using a variable passed in the frontend registration form. I was going to listen for the OnSaveUser event but I don't think there's a way to get the usergroup from the original post that way(?) so I've instead created a controller that forwards to users/onSaveUser using:

$forward = $this->forward('users/saveUser', false);

The idea being I'll then have access to the post after the user has been created. The problem is everything ends here and doesn't reach the rest of my function. Here's my controller:

<?php
namespace Craft;

class UserGroupController extends BaseController
{

    protected $allowAnonymous = true;

    public function actionSaveUser()
    {
        $this->requirePostRequest();


        $forward = $this->forward('users/saveUser', false);
        throw new Exception('Made It back!'); // never reached 
        return TRUE;
    }

}

According to the Yii docs, false should stop the things ending, right?

  • Just did a simple test with actionTest1() forwarding to actionTest2() and it works as you'd expect. UsersController->saveUser() is pretty complicated, though. My guess is that you'd going down one of the branches of it's logic where it's redirecting or ending the request via some other means. xDebugging should help see what's going on in your case. – Brad Bell May 15 '17 at 18:48
  • What kind of controller works with wild craft. I have bought 4 different ones – D timmons Apr 19 '19 at 20:31
1

I have this working in another plugin calling another function:

$forward = $this->forward('commerce/products/saveProduct', false);

and it returns to the original controller fine. So, as Brad comments, the forward is actually working as expected but that particular UsersController->saveUser() function prevents it happening internally.

Within my particular example, I was able to access the original post request and access a userGroupId from the user registration form like so:

    craft()->on('users.onSaveUser', function (Event $event) {
        if ($event->params['isNewUser']) {
            if (isset($_POST['userGroup'])) {

                $userGroupId = $_POST['userGroup'];
                $user = $event->params['user'];

                craft()->userGroup->setUserGroup($user, $userGroupId);

            }               
        }
    });

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.