4

I have long wondered how to change my admin particularly for my custom modules. For many cases using an XML file is fine and I can use the built-in Joomla! form fields.

In some cases, I'd like to style them, which I can do by adding classes and then adding the content to the template file. Which is 99.9% of the time Isis.

But I have always wondered how I would achieve this on any admin template. I would need to load a CSS or JS file somehow. I could do this with an addScript declaration, but I do not have a PHP file to add this to. I could use an Admin Plugin, but that doesn't seem like the right solution.

And finally, how would I add an image, or some HTML e.g. an explanation or instructions. I can use the description panel on some occasions, but on others, it would make sense to be next to the field itself.

1 Answer 1

6

I personally do this in my extensions by creating a custom field:

XML:

<fieldset name="something" label="Something" addfieldpath="/modules/mod_xxx/fields">
    <field type="testfield" />
</fieldset>

mod_xxx/fields/testfield.php:

<?php

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\HTML\HTMLHelper;

class JFormFieldTest extends Joomla\CMS\Form\FormField
{

    protected $type = 'Test';

    protected function getLabel()
    {
        return;
    }

    protected function getInput()
    {
        // Custom JS
        HTMLHelper::_('script', 'mod_xxx/custom.js', ['version' => 'auto', 'relative' => true]);

        // Custom CSS
        HTMLHelper::_('stylesheet', 'mod_xxx/custom.css', ['version' => 'auto', 'relative' => true]);

        // Custom HTML
        $html = '<p>Hello</p>';

        return $html;
    }
}
14
  • And then you use the class to position it? Seems like this may be a difficult thing to position if it's a custom field. Or is it reasonably straight forward?
    – Eoin
    Commented Jan 11, 2019 at 19:53
  • I found it interesting that you use HTMLHelper which I haven't encountered before and also getInput when I would have used docs.joomla.org/Adding_JavaScript. Is there any reason you decided to go this way?
    – Eoin
    Commented Jan 11, 2019 at 19:58
  • 1
    Yesh use a class to position it. I used some Javascript myself aswell but you may not need to. HTMLHelper is just the namespaced version of JHtml...still works the same way. I just never use addScript()
    – Lodder
    Commented Jan 11, 2019 at 20:28
  • 1
    @Eoin, HTMLHelper is a namespaced version of JHtml. These namespaced classes were added in J4 and backported to J3. The original classes will be removed in J5 but will still work in J4, so I would personally use the new approach which will save you time in the future.
    – Lodder
    Commented Jun 1, 2019 at 9:32
  • 1
    Can you share your code?
    – Lodder
    Commented Oct 13, 2020 at 4:49

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.