2

I added following code in onUserBeforeSave of user profile plugin(plugins/user/profile/profile.php):

    if ($isnew)
    {
        if (!strcasecmp($data['role'], 'group0'))
        {
            $data['groups'] = array(10);
        }
        elseif (!strcasecmp($data['role'], 'group1'))
        {
            $data['groups'] = array(11);
        }

        JFactory::getApplication()->enqueueMessage(print_r($user, true));
        JFactory::getApplication()->enqueueMessage(print_r($data, true));       
    }

The print_r results show the $data has been modified successfully, but in the end, the new user added to database still in old group, which is 'Registered'.

I am not sure what's the reason for this. And how can I achieve overriding default user groups. Thanks!

1 Answer 1

2

You can't edit user data in onUserBeforeSave and onUserAfterSave event. This is just a copy of what will/was saved. It would be better to:

  • use onUserAfterSave event
  • get user object after save $user = JFactory::getUser($user_id) where $user_id is id of a user you want to modify
  • modify user groups like this $user->groups = Array($new_user_group)
  • save user $user->save()

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.