1

Having an issue with using the event \craft\elements\User::EVENT_AFTER_SAVE in a plug in. I am adding a new user to a group. My goal is to have the newly added user automatically added to the same user group as the logged in user. This is my code:

Event::on(
    \craft\elements\User::class,
    \craft\elements\User::EVENT_AFTER_SAVE,
    function(ModelEvent  $event) {
        if (Craft::$app->getUser()->getIsAdmin() == false){
            $loggedInUser = Craft::$app->getUser();
            $userGroupUser = new UserGroup_User();
            $userGroupUser->groupId = $loggedInUser->getIdentity()->getGroups()[0]['id'];
            $userGroupUser->userId = $event->sender->id;
            $result = $userGroupUser->insert();
         }
    }
);

I have put a breakpoint on the code after the if statement and then checked the users table - the new user is not in the users table yet.

I would expect that the new user would be in the table at this point. Is this transaction being saved and executed later? If so is there something I can do to force the transaction to save the new user?

Is this possible a bug?

I do not get any error messages. Why?

EDIT: I should also mention that the usergroup has permission to add users in their own usergroup.

  • You'll need to use the afterSaveElement event github.com/craftcms/cms/blob/develop/src/services/… – Robin Schambach Jun 3 '18 at 16:43
  • Thanks @robin. Not making sense to me though. Here is the doc on the event I was using: EVENT_AFTER_SAVE. What does not make sense is the docs says "The event that is triggered after the element is saved". When I changed this to use the event: \craft\services\elements::EVENT_AFTER_SAVE_ELEMENT the event does not fire. – Eric Snyder Jun 3 '18 at 19:28
  • It does fire. The users afterSave is still inside the transaction so if you break it up (via breakpoint) the user isn't inside the table at that moment. The event AfterSaveElement is fired after the transaction is done, so after the element is stored – Robin Schambach Jun 3 '18 at 19:31
1

I don't know what your specific error is, but whenever I use

Event::on(
\craft\elements\User::class,
\craft\elements\User::EVENT_AFTER_SAVE,

I need to provide the next parameter with a \yii\base\Event callback, not a \craft\events\ModelEvent one.

Maybe that's your case as well ? You can also use \yii\base\ModelEvent, but not the craft one, as the craft ModelEvent does not extend \yii\base\Event

| improve this answer | |
  • That did not resolve the issue. I will post an "answer" with more detail below as it will not fit in a comment. – Eric Snyder Jun 5 '18 at 23:50
0

I have worked on this for days now. Using either:

\yii\base\Event

or

ElementEvent

Seems to register the event and it fires after a new user is entered. That's a good tip though.

My code currently looks like this:

Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(ElementEvent  $event) {
        if (Craft::$app->getUser()->getIsAdmin() == false){
            $loggedInUser = Craft::$app->getUser();
            $userGroupUser = new UserGroup_User();
            $loggedInUserGroup = $loggedInUser->getIdentity()->getGroups()[0]['id'];
            $newUserId =  $event->element['id'];
            $userGroupUser->groupId = $loggedInUserGroup;
            $userGroupUser->userId = $newUserId;
            $result = $userGroupUser->save();
         }
    }
);

Things I know:

  1. The code fires when I add a new user. I know this by placing a breakpoint on any line in the callback function.

  2. The callback code will NOT add a new row in the usergroups_users table for the brand new entered user. Let's say that user name is "Test1".

  3. After I allow the entry process to finish Test1 now shows up in the users table as a user with ID #50. The brand new "Test1" does not show up in the usergroups_uers table even though the callback code executed.

  4. Next if I change the callback code to use the user ID (50) of the newest user (Test1) that I just entered and then go and create a new user called test "Test2" the callback function will create a record for user "Test1" (user ID 50) will be entered into the usergroups_users table. I will see that record if I examine the usergroups_user table.

Code:

Event::on(
    Elements::class,
    Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(ElementEvent  $event) {
        if (Craft::$app->getUser()->getIsAdmin() == false){
            $userGroupUser = new UserGroup_User();
            $userGroupUser->groupId = 1;
            $userGroupUser->userId = 50;
            $result = $userGroupUser->save();
         }
    }
);

It's like the insert for the usergroups_users is silently failing if the user was just entered. It could also be that something was not committed???

I am new to Craft so it could be a noobie error but this one has me stumped.

| improve this answer | |
0

I'm not sure what your issue is, the following Code works as expected. Of course you should check if the element is an instance of craft\elements\User and the user is not already in the group.

Event::on(
    craft\services\Elements::class,
    craft\services\Elements::EVENT_AFTER_SAVE_ELEMENT,
    function(craft\events\ElementEvent  $event) {
            $userGroupUser = new craft\records\UserGroup_User();
            $userGroupUser->groupId = 1;
            $userGroupUser->userId = $event->element->getId();
            if(!$userGroupUser->save()){
                Craft::error('Could not save group ' . Json::encode($userGroupUser->getErrors()));
            }
    }
);
| improve this answer | |

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.