Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Are there any examples of plugins using onUserBeforeDelete and onUserAfterDelete
    Joomla Events


Are there any examples of plugins using onUserBeforeDelete and onUserAfterDelete


there are indeed examples of plugins using onUserBeforeDelete and onUserAfterDelete events in Joomla. Here are the key points:

1. Example Plugin:
There is a reference to an example plugin that demonstrates the usage of these events[4][5]:

php
plugins/user/example.php

This file likely contains sample code for implementing onUserBeforeDelete and onUserAfterDelete event handlers.

2. onUserBeforeDelete:
This event is triggered when a user is about to be deleted from the system[4]. The event receives one parameter:

- $user: An associative array of the columns in the user table.

You can use this event to prevent user deletion by redirecting back to the User Manager[4]. For example:

php
public function onUserBeforeDelete($user)
{
    if ($this->deleteNotAllowed($user)) {
        $app = JFactory::getApplication();
        $url = JRoute::_('index.php?option=com_users&view=users', false);
        $msg = JText::sprintf('PLG_USER_DELETE_NOT_ALLOWED', $user['username']);
        $app->enqueueMessage($msg, 'error');
        $app->redirect($url);
    }
}

3. onUserAfterDelete:
This event is triggered after a user has been deleted from the system[5]. It receives three parameters:

- $user: An associative array of the columns in the user table.
- $success: Boolean indicating if the deletion was successful.
- $msg: Error message if the delete failed (JError object detailing the error, if any).

4. Usage in Core Files:
Both events are used in core Joomla files[4][5]:


libraries/joomla/user/user.php
plugins/user/joomla.php

5. Triggering the Events:
These events are automatically triggered when using the User::delete() method[3]. This method ensures that plugins listening for these events will be notified and can perform any necessary cleanup or additional actions.

While specific code examples from the example.php file are not provided in the search results, the information suggests that you can create a user plugin and implement these event methods to handle actions before and after user deletion in Joomla.

Citations:
[1] https://joomla.stackexchange.com/questions/2369/prevent-user-deletion-with-plugin
[2] https://docs.joomla.org/Plugin/Events/en
[3] https://joomla.stackexchange.com/questions/26450/what-is-the-correct-way-to-programmatically-delete-a-joomla-user-and-their-data
[4] https://docs.joomla.org/Plugin/Events/User
[5] https://docs.joomla.org/J1.5:Plugin/Events/User