Simple plugin to check folder contents on component update

by Healyhatman   Last Updated March 04, 2019 03:10 AM - source

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

  1. On install, copy a folder to another component's folders
  2. When said component 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.



Answers 1


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: https://docs.joomla.org/J3.x:Creating_a_simple_module/Adding_an_install-uninstall-update_script_file

SHORT NOTES ABOUT PLUGIN INSTALLER SCRIPT

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 -->
<scriptfile>script.php</scriptfile>

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";
    }

IF IT IS NOT ON YOUR EXTENSION - PLUGIN EVENT ON OTHER COMPONENT INSTALL

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: https://api.joomla.org/cms-3/classes/JFolder.html

And you can study Joomla Plugin creation more here: https://docs.joomla.org/J3.x:Creating_a_Plugin_for_Joomla

ACTING ON A COMPONENT WHEN IT IS FIRST INSTALLED

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.

Zollie
Zollie
March 04, 2019 07:17 AM

Related Questions


onUserAfterLogin popup - Joomla 3.6.4 Plugin

Updated March 08, 2018 11:10 AM

Joomla and Eventbrite Integration

Updated July 08, 2019 11:10 AM


Can components / non-plugins react to events?

Updated May 13, 2015 15:04 PM

How to call a model method from a system plugin?

Updated July 09, 2016 08:04 AM