1

I would like to find the documentation for the Jooma 4 application object giving subclasses and methods, etc. I have looked hard on the Joomla.org website, but can't find it there. (It seems difficult to navigate that site in a logical way.)

My immediate need is to get/set these two values:

  1. Get the value of an article's custom field for use in a plugin;
  2. Set the template style to be used for displaying an article.

Any guidance would be appreciated, whether on navigating the Joomla 4 documentation or on accessing these particular values.

1 Answer 1

2

Agreed - the documentation is rather haphazard!

You can find classes and APIs at https://api.joomla.org/

There are 2 sides to the APIs:

  • CMS - these relates to classes under libraries/src
  • Framework - these relates to classes under libraries/vendor/joomla/src

There are a number of Application classes and the higher level ones are in the CMS side. You can follow the inheritance chain down to WebApplication, and you can see the methods available.

WebApplication in turn inherits from AbstractWebApplication which is in the Framework side, and you can see the methods of it via the Framework APIs. Unfortunately the methods which AbstractWebApplication has aren't visible to the higher level classes on the CMS side - the documentation tool doesn't enable them to cross over.

For custom fields you can find the value in the jcfields variable of your article structure - see eg https://docs.joomla.org/J3.x:Adding_custom_fields/Overrides/en. If you do a search for "joomla jcfields" you should find a number of web pages dealing with it.

The template style for displaying an article is really on the menuitem, rather than the article. You can have the same article displayed in 2 different menuitems on your site and with different template styles. You can find some documentation on menuitems at this Menus and Menuitems API Guide. The property you want is template_style_id.

Update --------------------------------

If you're looking to set the template style based on the article, then this is a suggested approach which I think will work.

  1. Define hidden menu items for each of the different template styles which you want to use.

  2. Define a custom form field to capture the template style - this will involve writing a plugin similar to those in plugins/fields. See Is there a way I can add field types to com_fields? and https://docs.joomla.org/Templatestyle_form_field_type

  3. Add a custom field to your Content items, and specify it as your template field above. This will allow you to set the template style for each article.

  4. Write a system plugin that fires on onAfterRoute. The router will have set the menuitem in the Itemid JInput field and Joomla will choose the template style based on this menuitem. Unfortunately (for you!) it does this before it renders the component, so you don't have the visibility of the component item, so you can't look at jcfields for example. (So if you wait until com_content is run and has retrieved the values, then it's too late to set the template style).

So in the plugin you have to check a number of things:

  • that you're on the site $application->isClient('site')
  • that the JInput option is "com_content"
  • you may also want to check the view etc

Then you need to get the JInput id and find the associated template style set for that article. To do this I would just do a database query around the content and various fields tables - you'd need to work out how Joomla stores the custom field values in those tables.

Then you need to use the menuitems API to get the template_style_id for the Itemid which the router has set.

If they don't match then you need to use the menuitems API to find the menuitem with the article's preferred template style. Then you'd need to set the JInput Itemid based on that.

As an alternative to defining a template style custom field, you could use a text dropdown for the various styles and set the note field in the hidden menuitems to a particular style, and just match on that.

By the way, when you're developing system plugins, then because they get read all the time, if you have any errors in them your Joomla admin might not start. If this occurs go into phpmyadmin, find your plugin in the extensions table and set the enabled column to 0.

6
  • Thank you for your help, Robbie. Much appreciated. I must confess that the documentation is still quite opaque to me. It also seems that the menuitem approach isn't really going to help because the articles for which I want to assign styles are not all assigned to menu items. It would be so helpful if Joomla could provide documentation which explains in a straightforward way how to perform common tasks using the API. Aug 7, 2023 at 16:30
  • There are several API guides at docs.joomla.org/API_Guides which you may find useful at some point. Also you can configure hidden menu items which don't appear on the menu, but you can style assign a different template style to, and that will be used. Aug 7, 2023 at 18:19
  • Thank you for that API guide link. I'm sure I will find that useful. I am also aware of the possibility of hidden menu items, but I am trying to avoid having to create menu items for each article. I find that method very cumbersome! Thanks again for your help. Aug 7, 2023 at 19:03
  • I added an update with a possible approach. Aug 8, 2023 at 14:10
  • Thank you for all your time, Robbie. I appreciate your help. The method you suggest seems to have potential, but it is quite involved. As far as I can see, it should be simpler. Maybe by specifying the template style in the input object, as if it was entered in the url with ?templateStyle=xx. I will post results here when I have them. Aug 9, 2023 at 19:21

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.