3

We have a custom login plugin which extends the CMSPlugin.

I am trying to register the Class of the plugin inside a module but the following scenarios occur.

Case 1

JLoader::register('CLASS_NAME_OF_THE_PLUGIN', 'path_to_plugin');
CLASS_NAME_OF_THE_PLUGIN::method_inside_class();

Does not work because the method is not static

Case 2

JLoader::register('CLASS_NAME_OF_THE_PLUGIN', 'path_to_plugin');
$new_class_instance = CLASS_NAME_OF_THE_PLUGIN();

Does not work and the error is

Too few arguments to function Joomla\CMS\Plugin\CMSPlugin::__construct(), 0 passed at least 1 expected

How I can solve that? I don't really want to change my functions/methods to 'static' because after other problems arise(can't login and so on).Perhaps going with the second approach? But I don't know what should I give as argument and how should I write it

we use the Plugin to override standard Joomla login kinda. So when the user logs in for first a request is sent on the external login "environment" and if the credentials are correct then a joomla user is created without password. After user tries to login in the future a request is being sent on the external login also. In this plugin we also check if the tokens are valid, renew the tokens if they are expired and also fetch the user additional info from the external environment(address, phone etc). So I thought I could use maybe some functions of the plugin inside a module? Like refetch the additional user info?

Thanks :)

4
  • What is the purpose of this plugin? Is it acting on a specific login/logout event? I am asking this because I am suspecting that you maybe do not need a plugin. You might create a helper class inside a module that can handle things. I am also asking this because in Joomla it is not a practice to register and load plugin class inside a module. Plugin in Joomla context is a little different than in other frameworks. Plugins are acting on “hooks” we could say. We should clear this a little bit. Maybe we can help you better if we understand the purpose. Please if you could update on this a bit.
    – Zollie
    Commented Mar 22, 2023 at 15:46
  • This article from the docs can help you understand more how you could call your plugin from your module for example: docs.joomla.org/J4.x:Creating_a_Plugin_for_Joomla
    – Zollie
    Commented Mar 22, 2023 at 15:53
  • So, if you need that plugin, then you should create one or more events and dispatch them in your module, then your plugin methods can and will act on those (events) calls.
    – Zollie
    Commented Mar 22, 2023 at 16:43
  • 1
    edited my original post :) hope that I have explained it good..since I am new to programming
    – anthi_p
    Commented Mar 22, 2023 at 18:05

2 Answers 2

2

For having and working with a plugin in a module of Joomla directly (not via events), for testing this let's say you have your own plugin group called: 'myplug' and your plugin is called: 'testplugin1' and you want to use this plugin in your module or in your view or anywhere else in Joomla. (after installing, do not forget to activate your plugin in the admin plugins manager)

And your plugin Class looks like this:

class PlgMyplugTestplugin1 extends CMSPlugin 
{

    public function doWhatever() {

        $message = 'I am messaging from my testplugin';

        return $message;
        
    }

}

You can instantiate your plugin like:

use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Factory;

PluginHelper::importPlugin('myplug', 'testplugin1'); // returns a boolean
        
$myplugin = PluginHelper::getPlugin('myplug', 'testplugin1'); // returns an Object

$type = $myplugin->type;

$className = 'Plg' . $type . $myplugin->name; 

// $dispatcher = $this->getDispatcher(); // if it is available

// another way of getting the dispatcher
$dispatcher = Factory::getApplication()->getDispatcher();

// Instantiate the Plugin. You will be able to use the plugin Class and its methods:
$activeplugin = new $className($dispatcher, (array) PluginHelper::getPlugin($type, $myplugin->name));

// calling the doWhatever() method from your plugin
$activeplugin->doWhatever();

// or echo $activeplugin->doWhatever(); // if it is returning back a string

If it is a 'system' plugin for example then you will change 'myplug' to 'system' of course, at importPlugin() and getPlugin() methods. I had tested the above in one of my views.

And just giving the above in a short cleaner version, including only the relevant parts:

use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Factory;  

PluginHelper::importPlugin('myplug', 'testplugin1');
    
$myplugin = PluginHelper::getPlugin('myplug', 'testplugin1');

$className = 'PlgMyplugTestplugin1';

$dispatcher = Factory::getApplication()->getDispatcher();

// initiate
$activeplugin = new $className($dispatcher, (array) $myplugin);

$activeplugin->doWhatever();

As I wrote in my comment, I am still not sure that you would need to get and use the plugin in a Login module for example, or you really need a plugin for your Login project, if you have a module for that, unless the plugin is acting on a very specific Event. Since usually a module includes plugins and creating events for the plugins which the plugins could act on. However there are a very few example in Joomla using plugins the other way around like using plugins' classes and methods directly in another extension of Joomla.

1
  • 1
    YOU ARE AMAZING! (-) That is exactly what I was looking for :) Actually the plugin is for authorization and I wanted to use a function of the plugin to set again the user data which are fetched from the external source.
    – anthi_p
    Commented Mar 24, 2023 at 13:09
3

As mentioned in the comments, normally you should not use plugin objects directly. But if you need it for some reason, the correct way is using Joomla\CMS\Extension\ExtensionManagerInterface::bootPlugin() method. The interface is implemented by CMS application classes so you can use this in the module:

$plugin = $app->bootPlugin($group, $name);

Where $group is the plugin group and $name the plugin name.

1
  • Even more simple and obvious solution from Joomla 4, good catch. I missed this line in PluginHelper.
    – Zollie
    Commented Mar 24, 2023 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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