1

I'm currently building a plugin that uses a table that contains rows with an 'active' field set to 1.

If an admin deletes a user from the system, I want to change this value to 0.

I found an 'onUserDelete' event documented but can't work out where or how to use it.

Any help would be greatly appreciated!

EDIT UPDATED:

I now have this code in the init() function of my plugin controller but deleting a user does not seem to fire the code:

    parent::init();

    craft()->on('users.onDeleteUser', function(Event $event) {
        $user = $event->params['user'];

        $userId = $user->id; // ???????

        $craft->memberships_membership->cancelMembershipById($userId);
    });

Any ideas?

1

From the documentation on events, they follow this pattern:

craft()->on('component.eventName', function(Event $event) {
    // ...
});

So in your example, it'd be something like this and you'd want to add it to your plugin's init() method in the main plugin class.

public function init()
{
    parent::init();

    craft()->on('users.onDeleteUser', function(Event $event) {
        $user = $event->params['user'];
        $userToTransferContentTo = $event->params['transferContentTo']
    });
}

The full list of available events and the parameters they have can be found here.

| improve this answer | |
  • Thanks, it doesn't seem to work though. I've edited the question to show how my code looks at the moment. – H2ONOCK Nov 6 '15 at 19:24
  • Updated my answer. – Brad Bell Nov 6 '15 at 19:26
  • Thanks Brad, that really helps. I feel like I'm close now. I can't seem to find how to get the user's id from that $user var which I believe to be of UserModel type? I've updated my question with the latest code. – H2ONOCK Nov 6 '15 at 19:38
  • You're correct and that should do it! – Brad Bell Nov 6 '15 at 19:39
  • It doesn't :( I'm pretty sure it's not firing. The same code to cancel a membership is used elsewhere so I know that works. I'm not sure $userId = $user->id; is correct because I can't see an id mentioned in the UserModel documentation. – H2ONOCK Nov 7 '15 at 11:25

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.