Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Joomla onFileBeforeDelete usage example
    Joomla onFileBeforeDelete Joomla plugin file deletion custom actions plugin implementation


Joomla onFileBeforeDelete usage example


Here is an example of how to use the onFileBeforeDelete event in Joomla to perform custom actions before a file is deleted:

Implementing onFileBeforeDelete

To use the onFileBeforeDelete event, create a plugin that implements the onFileBeforeDelete method. Here's an example plugin class:

php
class plgSystemMyPlugin extends JPlugin
{
    public function onFileBeforeDelete($args)
    {
        // Get the file path from the event arguments
        $file = $args['file'];

        // Perform custom actions before deleting the file
        $this->customActionBeforeDelete($file);

        // Return true to allow the file deletion to proceed
        return true;
    }

    protected function customActionBeforeDelete($file)
    {
        // Implement your custom logic here
        // For example, log the file deletion event
        JLog::add('Deleting file: ' . $file, JLog::INFO, 'plg_system_myplugin');
    }
}

In this example:

1. The plugin class `plgSystemMyPlugin` implements the `onFileBeforeDelete` method.
2. The `onFileBeforeDelete` method receives the file path as an argument in the `$args` array.
3. Inside the method, you can perform any custom actions before the file is deleted, such as logging the event.
4. Finally, the method returns `true` to allow the file deletion to proceed.

Registering the Plugin

After creating the plugin class, you need to register it in the plugin's XML manifest file. Here's an example `myplugin.xml` file:

xml
<?xml version="1.0" encoding="utf-8"?>
<extension version="3.9" type="plugin" group="system" method="upgrade">
    <name>plg_system_myplugin</name>
    <author>Your Name</author>
    <creationDate>YYYY-MM-DD</creationDate>
    <copyright>Copyright (C) YYYY Your Name. All rights reserved.</copyright>
    <license>GNU General Public License version 2 or later; see LICENSE.txt</license>
    <version>1.0</version>
    <description>PLG_SYSTEM_MYPLUGIN_XML_DESCRIPTION</description>
    <files>
        <filename plugin="myplugin">myplugin.php</filename>
    </files>
    <languages>
        <language tag="en-GB">language/en-GB/en-GB.plg_system_myplugin.ini</language>
        <language tag="en-GB">language/en-GB/en-GB.plg_system_myplugin.sys.ini</language>
    </languages>
</extension>

Make sure to replace the relevant information, such as the plugin name, author, creation date, and language files.

Usage

Once the plugin is installed and enabled in Joomla, it will automatically trigger the `onFileBeforeDelete` event whenever a file is about to be deleted. The custom actions defined in the `customActionBeforeDelete` method will be executed before the file deletion proceeds[1][2].

This example demonstrates how to use the `onFileBeforeDelete` event to perform additional logic, such as logging the file deletion event, before allowing the file deletion to continue in Joomla.

Citations:
[1] https://docs.joomla.org/how_to_use_the_filesystem_package
[2] https://forum.joomla.org/viewtopic.php?t=1001864
[3] https://forum.joomla.org/viewtopic.php?t=239324
[4] https://forum.joomla.org/viewtopic.php?t=966103
[5] https://learn.microsoft.com/en-us/iis/develop/windows-web-application-gallery/joomla-sample-files