PR-5.0-dev Pending

User tests: Successful: Unsuccessful:

avatar dgrammatiko
dgrammatiko
29 Jul 2023

Pull Request is a redo of #39521 with the introduction of an XML tag for the Helper name.

Summary of Changes

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

Testing Instructions

Existing functionality Front end templates

  • Add a file named 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];
    }
}
  • point your browser to /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}}

Proposed code

  • Add <namespace path="src">Joomla\Template\Cassiopeia</namespace> to the templateDetails.xml of Cassiopeia
  • Add <ajaxhelper>Ajax</ajaxhelper> to the templateDetails.xml of Cassiopeia
  • Delete the file administrator/cache/autoload_psr4.php
  • create a file named 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"}}

Existing functionality Back end templates

  • Add a file named helper.php in the administrator/templates/atum folder
  • Add the following content
<?php

defined('_JEXEC') || die;


class TplAtumHelper
{
    public static function templateAjax(): array
    {
        return ['is' => true, 'namespaced' => false];
    }
}
  • point your browser to 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}}

Proposed code

  • Add <namespace path="src">Joomla\Template\Atum</namespace> to the templateDetails.xml of the Atum template
  • Add <ajaxhelper>Ajax</ajaxhelper> to the templateDetails.xml of Atum
  • Delete the file administrator/cache/autoload_psr4.php
  • create a file named 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"}}

Actual result BEFORE applying this Pull Request

No way to use the namespaced helper

Expected result AFTER applying this Pull Request

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.

Link to documentations

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?

avatar joomla-cms-bot joomla-cms-bot - change - 29 Jul 2023
Category Front End com_ajax
avatar dgrammatiko dgrammatiko - open - 29 Jul 2023
avatar dgrammatiko dgrammatiko - change - 29 Jul 2023
Status New Pending
avatar dgrammatiko dgrammatiko - change - 29 Jul 2023
Labels Added: PR-5.0-dev
avatar dgrammatiko dgrammatiko - change - 29 Jul 2023
The description was changed
avatar dgrammatiko dgrammatiko - edited - 29 Jul 2023
avatar dgrammatiko dgrammatiko - change - 29 Jul 2023
The description was changed
avatar dgrammatiko dgrammatiko - edited - 29 Jul 2023
avatar dgrammatiko dgrammatiko - change - 29 Jul 2023
The description was changed
avatar dgrammatiko dgrammatiko - edited - 29 Jul 2023
9a6aaef 29 Jul 2023 avatar dgrammatiko oops
avatar HLeithner
HLeithner - comment - 29 Jul 2023

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.

avatar dgrammatiko
dgrammatiko - comment - 29 Jul 2023

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

avatar dgrammatiko
dgrammatiko - comment - 30 Jul 2023

Well, never mind

avatar dgrammatiko dgrammatiko - change - 30 Jul 2023
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2023-07-30 17:11:12
Closed_By dgrammatiko
avatar dgrammatiko dgrammatiko - close - 30 Jul 2023
avatar obuisard
obuisard - comment - 30 Jul 2023

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

avatar dgrammatiko
dgrammatiko - comment - 30 Jul 2023

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

avatar obuisard
obuisard - comment - 31 Jul 2023

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

avatar dgrammatiko
dgrammatiko - comment - 31 Jul 2023

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

Add a Comment

Login with GitHub to post a comment