Simple plugin to check folder contents on component update

Lucy: 2 weeks ago

I'm wanting to make a very simple plugin. All it needs to do is

  1. On installation of the plugin, copy a folder to a particular component's library folder.
  2. When a particular component library is updated (which deletes all folders and rebuilds), check for the folder again and copy it if it was removed.

That's it. I'm not sure how to achieve it though, as the documentation is a bit sparse and the Joomla Facebook group just told me to read that same documentation page or buy a book.

Leah: 2 weeks ago

There is the install script there for this task in Joomla which can fire on different points of the extension install or uninstall processes, like install, uninstall, preflight, postflight and on update. You can use that script with your extension (plugin, component or module). You can run any kind of functions at the different events.

You have to study the documentation of this here:


Of course if you install a plugin or component then you cannot use mod_ as a prefix for your installer script, that is obvious for someone who is not lazy to study and understand a bit about Joomla. And the question of how to create a plugin installer script had already been answered hundreds of times in the last 10 years. These are couple of things you should keep attention on though when you create a plugin installer script.

In your plugin XML manifest file you should give the name of your installer script whatever it will be, so:

<!-- Scripts to run on installation -->

The script filename could be simply just script.php (many give unique names for that though).

On the top of your plugin file you should include some classes (not in the script file!):

use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Database\DatabaseDriver; 

defined('_JEXEC') or die;

Your plugin class name is built up like this: Plg[plugingroupname][pluginname], so in this case when you build an extension plugin for example: class PlgExtensionMybrilliantshit extends CMSPlugin

Then your InstallerScript class name should be the same as your plugin class name otherwise the installer script will not run:

So it is like: class PlgExtensionMybrilliantshitInstallerScript

And the functions are running with arguments (with a passed PluginAdapter instance), so for example if you want to run your function on install event, then your function is going to be like this:

public function install($adapter)
        // here the folder checking and copying lines of the function...
        echo "Hey, I'm installed and checked your folders";


Extensions are installed using the Installation Manager which also has events when your plugin functions could fire, at the time an extension is installed, updated or uninstalled, for acting on these events you can use an extension plugin:

plg_extension_custom plugin

Which plugin has to have a function on event for example:

public function onExtensionAfterInstall($installer, $eid)

Where $installer is an instance of JInstaller and $eid is the ID of the newly installed component (or other extension).

Similar event when an extension is updated and you can act on it with your plugin and for that you create a function like:

public function onExtensionAfterUpdate($installer, $eid)
    $comp_id = 956; // you define the component id which you want to act upon
    if ($eid === $comp_id)
        // all the things you want to do... for example:
        $src = ""; // you define any folder path here where you want to copy the folder from.
        $dest = ""; // you define the destination folder path where you want to copy to
        if(! JFolder::exists(JPATH_ROOT . "libraries/cegcore2")) // any folder path and folder you want to check the existence of... after it's deleted on update
            JFolder::copy($src, $dest, $path = '', $force = false, $use_streams = false); // you can copy the deleted folder back

I have not checked or tried the above particular code (it is just a fast example) so it has to be finetuned when you use it but that would be a skeleton of a simple folder checking and copying plugin, after another component is updated for example and your folder had been deleted by that update.

You can study the use of Joomla JFolder class more here:

And you can study Joomla Plugin creation more here:


The question has also come up that what if the component is just installed and in that case we do not yet know the component ID and we cannot really use our plugin function the way we used it on the update event. We cannot identify our component yet by ID.

Now in that case we have to use the $installer argument of the event function, since via the passed installer object we can get the manifest file of the extension, thus we can get the name of the installed extension right there. So we can ignite our function only if our extension of interest is installed (by name. In this particular case it is: ChronoForms6)

So the function will look like this:

public function onExtensionAfterInstall($installer, $eid)
    $xmlobj = $installer->getManifest(); // we get the manifest Object as SimpleXMLElement
    $ext_name = (string) $xmlobj->name; // we get the name of the extension just installed
    if ($ext_name === "ChronoForms6")
        // echo 'this is the extension I want to be ignited on';
        // do the folder copying here or anything you would like to

Of course you can use this latter method on update event too and that way you do not have to base your plugin event function on extension ID.