Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I use onFileAfterDelete in a custom Joomla plugin
    Joomla Events


How can I use onFileAfterDelete in a custom Joomla plugin


The `onFileAfterDelete` event in Joomla is triggered after a file has been deleted from the system. Here is an example of how you can use this event in a custom Joomla plugin:

php
<?php
defined('_JEXEC') or die;

class PlgFileExample extends JPlugin
{
    /**
     * Triggered after a file has been deleted
     *
     * @param   string  $context  The context of the file deletion
     * @param   object  $table    The table from which the file was deleted
     *
     * @return  void
     */
    public function onFileAfterDelete($context, $table)
    {
        // Check the context to ensure this is the correct event
        if ($context === 'com_media.file')
        {
            // Perform actions after the file has been deleted
            $this->logFileDelete($table);
        }
    }

    /**
     * Logs the deletion of a file
     *
     * @param   object  $table  The table from which the file was deleted
     *
     * @return  void
     */
    protected function logFileDelete($table)
    {
        $app = JFactory::getApplication();
        $app->enqueueMessage('File deleted: ' . $table->name, 'info');
    }
}
?>

In this example, the `onFileAfterDelete` method is defined in a plugin class called `PlgFileExample`. The method takes two parameters:

1. `$context`: The context in which the file was deleted. In this case, we're checking if the context is `'com_media.file'` to ensure this is the correct event.
2. `$table`: The table from which the file was deleted. We can use this object to access information about the deleted file.

Inside the `onFileAfterDelete` method, we call a helper method `logFileDelete` that logs a message about the deleted file using the Joomla application object.

To use this plugin, you would need to create a new folder in your Joomla installation's `plugins/` directory called `file` and place the `PlgFileExample.php` file inside it. You would also need to create an XML manifest file to describe the plugin and enable it in the Joomla plugin manager.

Remember to replace the `PlgFileExample` class name and file name with your own unique plugin name to avoid conflicts with other plugins.

Citations:
[1] https://kevinsguides.com/guides/webdev/joomla/ref/content-plugins-with-exs
[2] https://forum.joomla.org/viewtopic.php?t=50238
[3] https://joomla.stackexchange.com/questions/31995/custom-plugin-doesnt-even-seem-to-load-on-joomla-4
[4] https://forum.joomla.org/viewtopic.php?f=831&p=3683276
[5] https://forum.joomla.org/viewtopic.php?f=833&p=3719988