Skip to content

[4.0] Support namespace module for com_ajax#32291

Closed
joomdonation wants to merge 4 commits intojoomla:4.0-devfrom
joomdonation:patch-7
Closed

[4.0] Support namespace module for com_ajax#32291
joomdonation wants to merge 4 commits intojoomla:4.0-devfrom
joomdonation:patch-7

Conversation

@joomdonation
Copy link
Copy Markdown
Contributor

@joomdonation joomdonation commented Feb 3, 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

@Quy Quy added the PR-4.0-dev label Feb 6, 2021
@laoneo
Copy link
Copy Markdown
Member

laoneo commented Feb 8, 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.

@Fedik
Copy link
Copy Markdown
Member

Fedik commented Feb 18, 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

@Fedik
Copy link
Copy Markdown
Member

Fedik commented Feb 18, 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

@joomdonation
Copy link
Copy Markdown
Contributor Author

@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.

@ghazal
Copy link
Copy Markdown
Contributor

ghazal commented Feb 21, 2021

Tested as explained.
Works as expected in the description.

@laoneo
Copy link
Copy Markdown
Member

laoneo commented Feb 22, 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.

@joomdonation
Copy link
Copy Markdown
Contributor Author

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 ?

@laoneo
Copy link
Copy Markdown
Member

laoneo commented Feb 22, 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.

@Fedik
Copy link
Copy Markdown
Member

Fedik commented Feb 22, 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

@laoneo
Copy link
Copy Markdown
Member

laoneo commented Feb 22, 2021

Perhaps we need to get rid of static then.

@joomdonation
Copy link
Copy Markdown
Contributor Author

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?

@Fedik
Copy link
Copy Markdown
Member

Fedik commented Feb 22, 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 😉

@thednp
Copy link
Copy Markdown
Contributor

thednp commented Mar 5, 2021

Why not add same logic to templates as for modules?

Follow #30816

@thednp
Copy link
Copy Markdown
Contributor

thednp commented Mar 6, 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.

@laoneo
Copy link
Copy Markdown
Member

laoneo commented Mar 8, 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.

@thednp
Copy link
Copy Markdown
Contributor

thednp commented Mar 10, 2021

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

@laoneo laoneo mentioned this pull request Mar 10, 2021
@laoneo
Copy link
Copy Markdown
Member

laoneo commented Mar 10, 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.

@laoneo
Copy link
Copy Markdown
Member

laoneo commented Mar 10, 2021

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

@thednp
Copy link
Copy Markdown
Contributor

thednp commented Mar 10, 2021

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

@joomdonation
Copy link
Copy Markdown
Contributor Author

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

@joomdonation
Copy link
Copy Markdown
Contributor Author

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

Was waiting for your PR, hehe.

@joomdonation joomdonation deleted the patch-7 branch March 19, 2021 08:33
@webchun
Copy link
Copy Markdown

webchun commented Oct 5, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants