J4 Issue ?
avatar germi
germi
3 Jul 2018

Steps to reproduce the issue

Hi guys, I'm not sure if this is the right place to post these kind of questions. I didn't see a "developer Joomla 4" section on Joomla forum.. if there's somewhere else I should be posting this, please let me know.
I'm migrating my custom extensions to Joomla 4. One issue I am facing is that plugins don't seem to be loaded.
I'm using the JPluginHelper::importPlugin('myplugingroup'); syntax to load the plugins, as in Joomla 3, and then JFactory::getApplication->triggerEvent('eventname') to trigger an event (this is the new J4 syntax)
However, importPlugin does not seem to be importing anything.
Is there something I'm missing? is there an easy way to see if the plugins are being loaded into the app? (JDispatcher class does not seem to exist anymore...)

System information (as much as possible)

Joomla 4 Alpha 3

avatar germi germi - open - 3 Jul 2018
avatar joomla-cms-bot joomla-cms-bot - change - 3 Jul 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 3 Jul 2018
avatar germi germi - change - 3 Jul 2018
The description was changed
avatar germi germi - edited - 3 Jul 2018
avatar joomla-cms-bot joomla-cms-bot - edited - 3 Jul 2018
avatar franz-wohlkoenig franz-wohlkoenig - change - 3 Jul 2018
Title
How to load plugins into custom component in Joomla 4
[4.0] How to load plugins into custom component in Joomla 4
avatar franz-wohlkoenig franz-wohlkoenig - change - 3 Jul 2018
Category Plugins
avatar brianteeman brianteeman - change - 5 Jul 2018
Labels Added: J4 Issue
avatar brianteeman brianteeman - labeled - 5 Jul 2018
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 7 Jul 2018

@dgrammatiko any Hint?

avatar franz-wohlkoenig franz-wohlkoenig - change - 7 Jul 2018
Status New Information Required
avatar JoomliC
JoomliC - comment - 7 Jul 2018

Did you test EventDispatcher instead ? (JDispatcher was already deprecated in Joomla 3 i think)

use Joomla\Event\Dispatcher as EventDispatcher;

OR PluginHelper::importPlugin('myplugingroup'); instead of JPluginHelper::importPlugin('myplugingroup');

avatar germi
germi - comment - 9 Jul 2018

Doesn't seem to make a difference...
How would you use EventDispatcher, exaclty? I'm trying this:

$dispatcher = new EventDispatcher();
$results = $dispatcher->triggerEvent(.....

but no results...

avatar mbabker
mbabker - comment - 9 Jul 2018
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;

PluginHelper::importPlugin('myplugingroup');

Factory::getApplication()->triggerEvent();

Instantiating a new dispatcher is going to give you just that, a new (empty) dispatcher. If you want the one loaded with all the plugins, use the dispatcher loaded into the application (and the best part is that snippet is B/C to 3.x).

If importPlugin() isn't doing anything that is a bug unrelated to the event dispatching.

avatar germi
germi - comment - 9 Jul 2018

thanks, this code that you share is practically the same I was already using..
I mean, I guess there's no difference between using

use Joomla\CMS\Factory;

Factory::getApplication()->triggerEvent();

and

JFactory::getApplication()->triggerEvent();

is there?

avatar mbabker
mbabker - comment - 9 Jul 2018

Other than the global class names being deprecated and nothing more than aliases these days, nope.

avatar germi
germi - comment - 9 Jul 2018

Then... the plugins actions are not being called.

now I'm thinking.. is it possible that in J4 you are required to "register" the events before using them?
in J3, I just created a plugin with functions in them (for example, "onContentSave"), and then I just used the triggerEvent('onContentSave') and it would work.

Is there any kind of registerEvent('onContentSave') action required when loading a plugin in order to "register" whose events into the system?

avatar mbabker
mbabker - comment - 9 Jul 2018

There is no registration requirement unless you are very explicitly making changes to not autoregister (the 3.x and earlier behavior which is basically deprecated in 4.0 to be removed in 5.0).

And if importing plugins isn't working, we'd probably notice because that would mean the core plugins wouldn't work either ?

avatar germi
germi - comment - 9 Jul 2018

I know... that's why I'm so lost. I'm using the same syntax that J4 uses (I'm taking the com_content triggers as an example, and within component com_content, plugin events are called like triggerEvent('onContentAfterSave')
I do the same with my component / plugins and it does not work...

and what's more, I have a plugin that contains both functions of "core joomla", like onContentAfterSave and those are called just fine. But my own "propietary" events, in the same file, like "onMyOwnAction" are not being called.

avatar mbabker
mbabker - comment - 9 Jul 2018

Without seeing the exact code it's hard to say where your issue lies, but I can say with certainty that the event dispatcher works and that importing plugins works so either there's something goofy/incorrect in your code or you've hit some extremely edge case that without seeing the code nobody's going to track down.

avatar germi
germi - comment - 9 Jul 2018

yes, I guess...
I'll try to dig deeper...
but again, the same plugin in J3 works fine, and when using it in J4 (using triggerEvent), it doesn't. It only calls the core funcitons, to my own.

avatar germi
germi - comment - 9 Jul 2018

sorry, last thought... maybe in J4, plugin can only be imported "before" some point in app execution cycle?

avatar mbabker
mbabker - comment - 9 Jul 2018

There is no restriction on when plugins can be imported in 4.0. There is no underlying change in 4.0 regarding the import of plugins or registration of events as long as your plugin class is extending Joomla\CMS\Plugin\CMSPlugin in some form (either directly or in the case of Smart Search its plugin subclass).

The app's triggerEvent method in 3.x and 4.0 both refer to a global event dispatcher service (in 3.x the return of JEventDispatcher::getInstance() and in 4.0 the one stored in the DI container). Unless you pass a dispatcher into the plugin helper, in both versions it resolves that global event dispatcher service and registers the events to that dispatcher.

At this point without seeing your exact code there is nothing I can say or do to help your issue. The API works, otherwise core would be broken.

avatar germi
germi - comment - 9 Jul 2018

is there a way I can share the code with you (if you want and can take a look, of course)?

avatar germi
germi - comment - 9 Jul 2018

ok, here's an interesting update...

my event name was "getItemName". it didn't get called.

if I change the name for a string starting with "on", it WORKS"

onItemName gets called, getItemName does not...!
that seems like a new requirement in J4, because this was not like this in J3... event names should start with 'on'.. is that possible? doesn't make much sense to me to force that...

avatar mbabker
mbabker - comment - 9 Jul 2018

The convention has always been that event names start with "on". The dispatcher in 3.x and earlier would take every public method in a plugin class and register those as listeners, so it would allow you to use any name (and it basically worked because the convention was "event name maps to method name"). So the fact it worked before I personally would call a case of "it works by accident but it's not the supported API use".

Personally, I'd take that as a documented B/C break with the instruction of prefixing event names as is convention.

avatar brianteeman brianteeman - change - 9 Jul 2018
Status Information Required Closed
Closed_Date 0000-00-00 00:00:00 2018-07-09 17:40:04
Closed_By brianteeman
avatar brianteeman
brianteeman - comment - 9 Jul 2018

Closed as not a bug

avatar brianteeman brianteeman - close - 9 Jul 2018

Add a Comment

Login with GitHub to post a comment