? ?
avatar joomdonation
joomdonation
3 Feb 2021

Pull Request for Issue #32136 .

Summary of Changes

Modify com_ajax to support namespace module.

Testing Instructions

  1. Create an instance of module Articles - Categories, make sure it is enabled on all pages. Publish the module
  2. Modify the file modules/mod_articles_categories/src/Helper/ArticlesCategoriesHelper.php, add a simple method for testing com_ajax
public static function getDataAjax()
{
	$data = ['test'];

	return $data;
}
  1. Access to this URL to trigger com_ajax for the module

http://localhost/joomla4/index.php?option=com_ajax&module=articles_categories&method=getData&format=json

  1. Before patch, you will get this error
    {"success":false,"message":"The file at mod_articles_categories/helper.php does not exist.","messages":null,"data":null}

  2. After patch, you will get this success response:
    {"success":true,"message":null,"messages":null,"data":["test"]}

You can install the modified mod_articles_categories.zip which I attached here so that you don't have do the step #2 (modify code)
mod_articles_categories.zip

avatar joomdonation joomdonation - open - 3 Feb 2021
981ae91 3 Feb 2021 avatar joomdonation CS
5eed1a0 3 Feb 2021 avatar joomdonation CS
avatar laoneo
laoneo - comment - 8 Feb 2021

Better to use the Joomla 4 way and boot the module. Then you don't have to manually include the file. How to load the helper can then be done with extending the module interface. But manually construction namespaces and class names should be something of the past.

avatar Fedik
Fedik - comment - 18 Feb 2021

Better to use the Joomla 4 way and boot the module.

@laoneo please show an example,
it is not obvious how to do that, all I can find is ModuleHelper::getModule that does not tell anything about namespace

avatar Fedik
Fedik - comment - 18 Feb 2021

If you mean $app->bootModule($module->module, $app->getName()).
It does not help here, because it does not allow to get namespace even if module have a namespace.

So there no other way than just parse XML directly.
Same issue with Template #30816

avatar joomdonation
joomdonation - comment - 21 Feb 2021

@laoneo Look at how boot the module works for Joomla 4, I still don't understand how it would help here. Could you please explain more details? Or if you could work on this Release Blocker item, that would be better.

avatar ghazal
ghazal - comment - 21 Feb 2021

Tested as explained.
Works as expected in the description.

avatar laoneo
laoneo - comment - 22 Feb 2021

But you can extend the module interface to get the module helper class. SOmething like $app->bootModule($module->module, $app->getName())->getHelper(); for example. Then getHelper does all the internal loading and returns the helper object. I mean it is just an idea but much better than compiling class names and loading files through require which we not should do anymore in J4.

avatar joomdonation
joomdonation - comment - 22 Feb 2021

So basically, move the logic to load helper class to https://github.com/joomla/joomla-cms/blob/4.0-dev/libraries/src/Extension/Module.php ?

avatar laoneo
laoneo - comment - 22 Feb 2021

You can either move your logic there. Better and cleaner way would be to introduce a helper factory, similar to what we did with the dispatcher factory https://github.com/joomla/joomla-cms/blob/4.0-dev/libraries/src/Dispatcher/ModuleDispatcherFactory.php. Guess @wilsonge needs to give here his input what he would accept for 4.0.

avatar Fedik
Fedik - comment - 22 Feb 2021

Something like $app->bootModule($module->module, $app->getName())->getHelper(); for example.

@laoneo problem with Helper, they all "static", I mean they is "abstract",
so in current state the factory cannot build "helper instance", only return the class name of the helper

avatar laoneo
laoneo - comment - 22 Feb 2021

Perhaps we need to get rid of static then.

avatar joomdonation
joomdonation - comment - 22 Feb 2021

For the time being, how about adding a method to ModuleHelper https://github.com/joomla/joomla-cms/blob/4.0-dev/libraries/src/Helper/ModuleHelper.php to return the helper class?

avatar Fedik
Fedik - comment - 22 Feb 2021

hm, what if we try Anonymous classes? ->getHelper() will return :

new class extends $helperClass{}

but it a bit dirty ?

Another idea, something like:

$dispatcher = $app->bootModule($module->module, $app->getName())->getDispatcher($module, $app);
$dispatcher->dispatchHelper('onAjaxFooBar');

Because a loot of helpers use Helper::doFooBar($module, $params) it could be useful when it will be a part of dispatcher.
But maybe also not a very good idea.

For the time being, how about adding a method to ModuleHelper

Would be better if we find solution now, because it will stay forever ?

avatar thednp
thednp - comment - 5 Mar 2021

Why not add same logic to templates as for modules?

Follow #30816

avatar thednp
thednp - comment - 6 Mar 2021

@laoneo perhaps at this point we need to do something about a getNamespace utility. Either save it in the manifest JSON part of the extension at install, or read it from XML the declaration. Either way I'm fine with any decision and hopeful of things moving forward.

I say let's start with my commit #30816 (RTC now) since it will be required by this commit and child templates features.

avatar laoneo
laoneo - comment - 8 Mar 2021

@thednp getNamespace is the wrong direction. Because we want to get away from manually compiling class names all over Joomla. The right approach would be to use some factories and inject them wherever possible to load the helpers. I know it is a bit more effort but it will payout at the end. Testing will be much more easier on a long term.

avatar thednp
thednp - comment - 10 Mar 2021

@laoneo please lay out a plan and lets get to work, I'm ready to take some work.

avatar laoneo
laoneo - comment - 10 Mar 2021

What do you guys think about #32633?
@thednp if we can bring #32633 somehow to fly, then the same pattern can be applied to templates.

avatar joomdonation joomdonation - change - 10 Mar 2021
Status New Closed
Closed_Date 0000-00-00 00:00:00 2021-03-10 15:51:32
Closed_By joomdonation
Labels Added: ? ?
avatar joomdonation joomdonation - close - 10 Mar 2021
avatar joomla-cms-bot joomla-cms-bot - change - 10 Mar 2021
Category Front End com_ajax
avatar laoneo
laoneo - comment - 10 Mar 2021

@joomdonation you shouldn't close it that fast. Perhaps I'm on the wrong path too ?

avatar thednp
thednp - comment - 10 Mar 2021

@joomdonation I hope you re-commit this PR with updates for #32633

avatar joomdonation
joomdonation - comment - 10 Mar 2021

@thednp #32633 should be better and is replacement for this PR, so this PR is closed now.

avatar joomdonation
joomdonation - comment - 10 Mar 2021

@joomdonation you shouldn't close it that fast. Perhaps I'm on the wrong path too ?

Was waiting for your PR, hehe.

avatar webchun
webchun - comment - 5 Oct 2021

Hi guys, I know this issue is already closed but has this issue really been resolved? It's still returning The file at mod_articles_categories/helper.php does not exist in Joomla 4.0.3. and com_ajax still looks for Mod class prefix in helper class name.

Add a Comment

Login with GitHub to post a comment