0

I would like to create a plugin to load a specified template style for each article on Joomla 4. The basic idea is as follows:

  1. Use a custom field in the article to specify the desired template style on a per-article basis.

  2. Create a plugin so that whenever the article is loaded it is diplayed with its template, as specified in the custom field.

Based on this post I understand that I need to create a system plugin for the onAfterInitialise event.

Now I have a couple of questions regarding how to implement this in practice.

  1. According to my understanding, the document itself is not loaded by the time the onAfterInitialise event is fired, which means that I would not be able to get the custom field's value directly at this point.

  2. I should be able to get the article id from the the input object. Would it be feasible to do a database query to get the required template name, and then to set it also in the input object?

Perhaps I am missing the boat completely and I need to use a different approach. The main point is that I would like to specify a template style for each article and to load the article with that template style - without having to create menu items for each article (of which there are many). Most of the articles will be accessed without menu items, so creating such menu items and using them to specify the template style is unnecessarily cumbersome.

Thanks in advance for your assistance.

5
  • I think onAfterInitialise is too early to trigger your plugin, at that point you haven't loaded up the article to know what template you want to use. Maybe something closer like onBeforeRender.
    – Irata
    Aug 5, 2023 at 10:10
  • Thank you for that comment, Irata. How would I then get the content of the custom field? What code would I use? Aug 5, 2023 at 10:22
  • I would look in JED for a custom field plugin that gets the custom fields(s) and includes them along with the rest of the articles fields when the record is retrieved. Your plugin would need to fire after that happens to see the value of the field and then respond accordingly.
    – Irata
    Aug 5, 2023 at 12:14
  • I am not familiar with the order of things happening at that point to know if the Template to be used has already been determined before the article data is retrieved in which case you might find your requirement is not easily possible, if at all doable, if the template is set before the article is got.
    – Irata
    Aug 5, 2023 at 12:19
  • Thank you for those comments. I will work on that. Aug 5, 2023 at 13:38

1 Answer 1

0

You can modify the template using the setTemplate (which is a method on the Site Application object). Here's the function definition (the function is defined here: /libraries/src/Application/SiteApplication.php:

public function setTemplate($template, $styleParams = null)

The onAfterInitialise() event should work for you in your plugin.

3
  • Thank you for that suggestion. I have spent some time today looking into the setTemplate function and trying to follow the execution process. However, I need to set the template style as well as the actual template. I haven't been able to find a way of doing that using the setTemplate function, though. Am I missing something? Aug 9, 2023 at 19:17
  • Check this forum.joomla.org/viewtopic.php?t=956403 - I cannot confirm it if works though.
    – itoctopus
    Aug 10, 2023 at 8:24
  • 1
    Thanks for that link. I've been trying something similar, although without the regitry parameter. I will do some more tests and post the results here. Aug 10, 2023 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.