File plugins can add custom functionality that you can apply to files and folders.
Generally, they are being accessed through the contextual menu called “Open with..” or the “New” button.
A plugin can also be set to be used as the default method or opening a particular type of files. This can be set from the control panel section Files
→ Plugins
→ Defaults
.
In this example we'll create a plugin which calculates a folder's total size.
Start by creating a new empty folder inside customizables/plugins/
. For this example we'll create customizables/plugins/folder_size
.
Create the PHP file customizables/plugins/folder_size/app.php
.
Plugins can have multiple files, but this is the only required one. It needs to follow a particular template:
<?php use FileRun\Files; use FileRun\UI; class custom_folder_size extends Files\Plugin { static $localeSection = 'Custom Actions'; function init(): void { $this->JSconfig = [ "title" => self::t('Calculate folder size'), 'iconCls' => 'fa-hdd-o', "popup" => true, 'width' => 400, 'height' => 400, 'folder' => true, 'requires' => ['isFolder'] ]; } function run(): void { $data = $this->prepareRead(['expect' => 'folder']); $size = Files\FM\Size::getFolderSize($data['fullPath']); if (!$size) { UI\Utils::displayError('Error: '.Files\FM\Size::$errMsg); } UI\Utils::displayMessage( Files\FM\Size::formatFileSize($size) ); } }
Note that the class namecustom_folder_size
follows the plugin's folder name.
The init()
method defines some properties for the plugin, like the fact that it applies only to folders and it opens in a popup.
The run()
method is what gets executed inside the opened popup.
To test the plugin, you need to clear the browser's cache and reload FileRun in your browser, otherwise the user interface will not refresh and you will not see the option in the contextual menu.