-1

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.

  • May I ask for what purpose do you need this? Maybe there is a better way than placing files in a 3rd party component. – Sharky Mar 4 at 7:23
  • The 3rd party component allows you to add functions, which are placed in a specific folder, but when you update the component those folders are wiped and you have to manually reinstall them. – Healyhatman Mar 4 at 7:26
  • That must be a very weird component. If not a secret, which component is this? – Sharky Mar 4 at 7:29
  • Chronoforms and chronoconnectivity – Healyhatman Mar 4 at 7:33
2

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.

  • I don't own the component in question though, is that a problem? And the plugin I want to make is for other people to use, not just my own use. – Healyhatman Mar 4 at 7:19
  • I do not see it as a problem, until your plugin will work well and not messing up things :). I do not know which component you want to copy a folder into and why, but that’s another question which we could discuss in a separate question. – Zollie Mar 4 at 7:22
  • That's literally all I want it to do though - copy a folder to a specified location, and keep it there after the specified component updates. No other functionality at all. – Healyhatman Mar 4 at 7:24
  • @Healyhatman - i also do not see the valid purpose of copying a folder within another component's folders, maybe the problem you want to solve can be handled other ways too, but if that is your purpose and solution then that is your purpose and solution. In a new, separate question we can discuss that if you would like to,.. – Zollie Mar 4 at 8:01
  • 2
    @Zollie: You have already some experience in this site. Please avoid having extensive discussions in the comments section in the future. – FFrewin Mar 4 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.