Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon OnUserBeforeDelete example code in Joomla


OnUserBeforeDelete example code in Joomla


here is an example of how to use the `onUserBeforeDelete` event in a Joomla plugin:

onUserBeforeDelete

Description

The `onUserBeforeDelete` event is triggered when a user is about to be deleted from the system. This event allows you to perform any necessary actions before the user is deleted.

Example Code

Here's an example of how you can use the `onUserBeforeDelete` event in a Joomla plugin:

php
<?php
/**
 * @copyright Copyright (C) 2005 - 2009 Open Source Matters, Inc. All rights reserved.
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access
defined('_JEXEC') or die;

/**
 * Example User Plugin
 *
 * @package Joomla.Plugin
 * @subpackage User.example
 * @since 1.6
 */
class PlgUserExample extends JPlugin
{
    /**
     * Example store user method
     *
     * Method is called before user data is deleted from the database
     *
     * @param   array  $user  Holds the user data.
     *
     * @return  void
     *
     * @since   1.6
     * @throws  Exception on error.
     */
    public function onUserBeforeDelete($user)
    {
        // Perform any necessary actions before the user is deleted
        
        // For example, you could check if the user has any associated data that should not be deleted
        if ($this->deleteNotAllowed($user))
        {
            // If the user cannot be deleted, you can redirect back to the User Manager
            $url = JRoute::_('index.php?option=com_users&amp;view=users', false);
            $msg = JText::sprintf('PLG_USER_DELETE_NOT_ALLOWED', $user['username']);
            $app = JFactory::getApplication();
            $app->enqueueMessage($msg, 'error');
            $app->redirect($url);
        }
    }

    /**
     * Example store user method
     *
     * Method is called after user data is deleted from the database
     *
     * @param   array    $user     Holds the user data.
     * @param   boolean  $success  True if user was successfully stored in the database.
     * @param   string   $msg      Message.
     *
     * @return  void
     *
     * @since   1.6
     */
    public function onUserAfterDelete($user, $success, $msg)
    {
        // Perform any necessary actions after the user is deleted
    }

    /**
     * This method should handle any delete logic and report back to the subject
     *
     * @param   array  $user  Holds the user data.
     *
     * @return  boolean  True on success
     *
     * @since   1.5
     */
    protected function deleteNotAllowed($user)
    {
        // Implement your custom logic to determine if the user can be deleted
        // Return true if the user cannot be deleted, false otherwise
        return false;
    }
}

In this example, the `onUserBeforeDelete` event is used to check if the user can be deleted. If the `deleteNotAllowed` method returns `true`, the plugin will redirect the user back to the User Manager with an error message.

The `onUserAfterDelete` event is also implemented, which can be used to perform any necessary actions after the user is deleted.

Citations:
[1] https://docs.joomla.org/Plugin/Events/User
[2] https://docs.joomla.org/Plugin/Events/en
[3] https://github.com/joomla/joomla-cms-examples/blob/master/plugins/user/example/example.php
[4] https://docs.joomla.org/accessing_the_current_user_object
[5] https://docs.joomla.org/J1.5:Plugin/Events/User