4

Is it possible to trigger a event for specific plugin?

I can load a specific plugin

$loaded = JPluginHelper::importPlugin('my_component', 'my_plugin_1');

But when I trigger an event, all the plugins that have that event are fired:

$results = JEventDispatcher::getInstance()->trigger('my_event');

I get the results not only of plugin1 but all the plugins that listen to that event.

$results = ['my_plugin_1_response', 'my_plugin_2_response'];

Is it possible to limit the plugins listening to the event to my_plugin_1_response only?

Please note that one possible solution would be to order the plugins and get the $results[0] as the result, but I'm not interested in this, I don't want that the other plugins are fired at all.

Thanks!

7

Generally, an event dispatcher does not support this concept. You're better off directly calling that single function versus going through a dispatcher if you really only want one response from one location.

Joomla 4.0 will use the refactored Event package as its base for event management, and the EventInterface contains a stopPropagation() method which a listener (your plugins) can call which will instruct the event dispatcher to stop propagating the event to additional listeners. So you have that to look forward to.

  • Thank you Michael for the info! The problem calling that function directly is that it won't be able to access params through $this->params, so I would need a workaround. Probably the best solution -in my case- will be to prefix the method so the plugins don't share the same event name. I will have to wait for Joomla 4 then! 😓 – xavip Jan 3 '17 at 12:55
  • ``` $plugin = JPluginHelper::getPlugin($type, $pluginName); $params = new Registry($plugin->params); ``` As long as you aren't dynamically setting params in the plugin, that'll give you what's in the database. – Michael Jan 3 '17 at 21:31

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.