0

Is it possible to override the default field template just in my component? How would I go about doing it?

The template I specifically want to override is "joomla.form.renderlabel", which is stored at layouts/joomla/form/renderlabel.php.

I know that I could create a template override, but this would override all admin fields for all components; I just want to override them in my component, or even more specifically, in one of my admin views. So I'm looking for a way to programatically modify which template is used.

From what I can tell, 'joomla.form.renderlabel' is hard coded into the FormField object, so to use a different label I would need to override that. But since it's so high in the inheritance chain I think I would also have to override like... so much of Joomla. Following the chain up, I especially begin to despair as I get to the FormHelper class.

Before I give up, I want to see if any experienced Joomla people know of some easy way to do this.

Right now I have some Javascript that does what I want: extracts descriptions and prints them underneath the labels rather than have them appear on hover. But this script is hacky and runs into problems with subforms. I feel like overriding the field template would be the right way to do it... but I also feel like it could be even more hacky if it means fighting with Joomla's architecture.

2
  • 1
    If you did a template override, then in your override code you could use the Application Input to get the 'option' and execute your special code only if it's your component. Would that be a suitable solution? Oct 30, 2023 at 22:36
  • @RobbieJackson I do really like that solution! I am looking for a way to do it all in the component ideally, but this may be the fallback I go with if that doesn't work out; it's certainly more elegant than the Javascript.
    – Obscerno
    Oct 30, 2023 at 23:21

1 Answer 1

2

JLayouts do support component level overrides (but not view level). Create the override (layouts/joomla/form/renderlabel.php) in your component's directory. In frontend and/or backend, depending on where you want it to take effect. The lookup order is as following:

[0] => templates/mytemplate/html/layouts/com_mycomponent
[1] => components/com_mycomponent/layouts
[2] => templates/mytemplate/html/layouts
[3] => layouts

Documentation: JLayout Improvements for Joomla!.

2
  • Excellent! That worked for me. I did run into an issue where I accidentally put layouts/... at the top level of my component, but it needed to go at admin/layouts/.... If you put it at the top level it will overwrite the core Joomla layouts, so to others: be careful!
    – Obscerno
    Oct 31, 2023 at 16:22
  • Also, Robbie Jackson suggested in a comment that within a template override you could detect the component. This same technique could be used within the component-specific template in this answer, except instead of the component you'd detect the view and display content differently based on that.
    – Obscerno
    Oct 31, 2023 at 16:26

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.