0

So when doing this i can import a Plugin and run an event on it:

PluginHelper::importPlugin('mygroup', 'myplugin');
Factory::getApplication()->triggerEvent('onMyEvent', ['some' => 'stuff']);

If i do this in a loop and trigger different events the prior loaded plugins also get triggered.
I only want the plugin method loaded just now to get triggered, even if prior loaded have the same eventname.
This is all a bit more complex, but here I am minimizing the question, so please don't suggest a different architecture of simple functions and similar.

So is there a way to unload or unboot a once loaded plugin?
I am using Joomla 4.3.4

2
  • What version of Joomla are you working with?
    – Irata
    Commented Sep 1, 2023 at 23:09
  • I updated the title
    – maidan
    Commented Sep 2, 2023 at 12:10

3 Answers 3

1

Without knowing what you're trying to accomplish I can only suggest using a custom dispatcher instance instead of relying on the global instance. You can then pass it to PluginHelper::importPlugin(). Event listeners of the imported plugins will be registered with your instance. You can then dispatch the event directly.

use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Event\Dispatcher;

$dispatcher = new Dispatcher;
PluginHelper::importPlugin('mygroup', 'myplugin', true, $dispatcher);
$event = $dispatcher->dispatch('onMyEvent', new MyEvent('onMyEvent', 'stuff'));
1

So this is what i ended up doing. In a loop the simple call of the plugin methods can be done without using the event system. $job is an object containing plugin attributes and some more.

$plugin = Factory::getApplication()->bootPlugin($job->element, $job->group));
// load plugin language file
Factory::getApplication()->getLanguage()->load('plg_' . trim($job->folder) . '_' . trim($job->element), JPATH_PLUGINS . '/' . trim($job->folder) . '/' . trim($job->element));
// add job params to plugin
$plugin->params->loadString($job->params);
$plugin->onMyEvent();

A custom dispatcher like sharky suggested would probably also be an approach. Thanks for

0

I can't find any or much documentation on the subject but looking through the code it seems to me that you don't need or want to unload the Plugin(s). In Joomla 4 you just need to remove the Listener(s) that the other plugins have registered so that they no longer are triggered by the event(s) that your plugin is listening to.

The ..\libraries\vendor\joomla\event\src\Dispatcher.php has the important bits where a plugin 'subscribes' to an event by adding a Listener to an event in the addSubscriber() function.

I would think that your Plugin needs to be loaded last, after the Plugin that you don't want to be involved, and its initial task would be to removeListener() or removeSubscriber() for the events that were set by the previously loaded Plugins.

Whether you remove the Subscriber or Listener would depend on whether the other Plugins are listening to more than just the event(s) you don't want them too.

I couldn't find any examples of code removing a listener to help you but looking at the dispatcher you have a few other methods to find out which listeners are active etc.

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.