User tests: Successful: Unsuccessful:
Pull Request is a redo of #39521 with the introduction of an XML tag for the Helper name.
The PR #39011 introduced namespace to the templates. This obviously solves the Fields problem but there is one more that needs to be tackled: com_ajax
.
This PR is using the existing conventions but allows to load the namespaced helper
helper.php
in the templates\cassiopeia
folder with the following content<?php
defined('_JEXEC') || die;
class TplCassiopeiaHelper
{
public static function templateAjax(): array
{
return ['is' => true, 'namespaced' => false];
}
}
/index.php?option=com_ajax&type=template&format=json&template=cassiopeia&method=template
and you should have a message: {"success":true,"message":null,"messages":null,"data":{"is":true,"namespaced":false}}
<namespace path="src">Joomla\Template\Cassiopeia</namespace>
to the templateDetails.xml of Cassiopeia<ajaxhelper>Ajax</ajaxhelper>
to the templateDetails.xml of Cassiopeiaadministrator/cache/autoload_psr4.php
templates/cassiopeia/src/Helper/Ajax.php
with the following contents:<?php
namespace Joomla\Template\Cassiopeia\Site\Helper;
defined('_JEXEC') || die;
class Ajax
{
public static function getAjax(): array
{
return ['is' => true, 'namespaced' => true, 'from' => 'getAjax'];
}
public static function testAjax(): array
{
return ['is' => true, 'namespaced' => true, 'from' => 'testAjax'];
}
}
visit again the same URL as before index.php?option=com_ajax&type=template&format=json&template=cassiopeia
and you should have a message: {"success":true,"message":null,"messages":null,"data":{"is":true,"namespaced":true,"from":"getAjax"}}
visit again the same URL as before index.php?option=com_ajax&type=template&format=json&template=cassiopeia&method=test
and you should have a message: {"success":true,"message":null,"messages":null,"data":{"is":true,"namespaced":true,"from":"testAjax"}}
helper.php
in the administrator/templates/atum
folder<?php
defined('_JEXEC') || die;
class TplAtumHelper
{
public static function templateAjax(): array
{
return ['is' => true, 'namespaced' => false];
}
}
administrator/index.php?option=com_ajax&type=template&method=template&format=json&template=atum
and you should have a message: {"success":true,"message":null,"messages":null,"data":{"is":true,"namespaced":false}}
<namespace path="src">Joomla\Template\Atum</namespace>
to the templateDetails.xml of the Atum template<ajaxhelper>Ajax</ajaxhelper>
to the templateDetails.xml of Atumadministrator/cache/autoload_psr4.php
administrator/templates/atum/src/Helper/Ajax.php
with the following contents:<?php
namespace Joomla\Template\Atum\Administrator\Helper;
defined('_JEXEC') || die;
class Ajax
{
public static function getAjax(): array
{
return ['is' => true, 'namespaced' => true, 'from' => 'getAjax'];
}
public static function testAjax(): array
{
return ['is' => true, 'namespaced' => true, 'from' => 'testAjax'];
}
}
visit again the same URL as before administrator/index.php?option=com_ajax&type=template&format=json&template=atum
and you should have a message: {"success":true,"message":null,"messages":null,"data":{"is":true,"namespaced":true,"from":"getAjax"}}
visit again the same URL as before administrator/index.php?option=com_ajax&type=template&format=json&template=atum&method=test
and you should have a message: {"success":true,"message":null,"messages":null,"data":{"is":true,"namespaced":true,"from":"testAjax"}}
No way to use the namespaced helper
Works
The additional part that is introduced here is the XML tag <ajaxhelper>
that defines the name of the Helper file in the namespaced Helper directory.
Please select:
Documentation link for docs.joomla.org:
No documentation changes for docs.joomla.org needed
Pull Request link for manual.joomla.org:
No documentation changes for manual.joomla.org needed
@HLeithner could you review this one?
Category | ⇒ | Front End com_ajax |
Status | New | ⇒ | Pending |
Labels |
Added:
PR-5.0-dev
|
@HLeithner in general I would also agree that a plugin is the way to go BUT NOT FOR A TEMPLATE! If a template needs a plugin to function then the whole stack needs rethinking. (ie the plugin might get disabled, the template will malfunction and it's extremely hard for end users to figure out the correlation).
A better approach was suggested by @laoneo to boot the template in a similar way as the modules, plugins,components, etc using the service provider but I guess it needs way more code to achieve the exact same thing.
Anyways for me this is an essential missing part since the introduction of the namespaces for templates (and quite honestly a make or break
thing).
Well, never mind
Status | Pending | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2023-07-30 17:11:12 |
Closed_By | ⇒ | dgrammatiko |
Hello Dimitris @dgrammatiko, do you have a use case for this? I understand the idea but I don't see yet how I could use this in a template (for what purpose).
So, templates up till #39011 couldn't have src/Fields/OwnField.php
because we couldn't use namespaces. With 39011 that part was solved. If your Fields are heavily using Ajax to fill drop-downs or have similar functionality (mine are complete client side apps in the templates case) then you either have to rely on the old helper.php
in the root of the template or introduce a plugin. My problem is that the root helper.php
is NOT namespaced and obviously I'm not an idiot to ship a plugin for a template functionality...
Thanks for the explanation, Dimitris. It makes total sense to me. I do use the namespaces now as well for my template's specific fields. I have not had to use Ajax yet, so I did not see that what you stated was needed...
@obuisard if you want to see an actual use case for this check my proposal from 3 years ago: #30914. Of course since in my templates I cannot go randomly add controller(s) to the com_templates but I still need an end point for my Ajax requests the only option is com_ajax, which by the way was created exactly for this purpose: as an escape hatch for the fields of modules, plugins and templates which DO NOT have an end point...
don't know really if this is useful at all, a template shouldn't have logic in it, if you want to do such things like saving content per ajax a plugin would fit much better or tbh the component should support this directly for example with webservices...
but maybe other maintainers has another opinion on this topic.