I wish to add a formsource to a subform in my custom maded module. The example code:

<field type="subform" formsource= "templates/DEFAULT_TEMPLATE/MY_MODULE/SUBFORM_NAME.xml" >
  • why don't you put your subform.xml inside your module?
    – FFrewin
    Sep 16, 2018 at 22:17
  • Because I need to load it in template override, in cases like overrides of this module Sep 17, 2018 at 3:55

1 Answer 1


Create a field type based on subform. Change formsource handling in __set method.

defined('_JEXEC') or die;


class JFormFieldMysubform extends JFormFieldSubform
    protected $type = 'Mysubform';

    public function __set($name, $value)
        switch ($name)
            case 'formsource':
                $this->formsource = JPATH_SITE . '/templates/' . $this->getDefaultTemplate() . '/html/mod_mymodule/mysubform.xml';
                parent::__set($name, $value);

    private function getDefaultTemplate()
        // Avoid duplicate queries
        static $template;

        if ($template === null)
            $db = JFactory::getDbo();
            $query = $db->getQuery(true)
                ->where($db->quoteName('client_id') . ' = 0')
                ->where($db->quoteName('home') . ' = 1');
            $template = $db->setQuery($query)->loadResult();

        return $template;

Above is just a basic example. You need to add checks to ensure that the form exists. Otherwise you'll get an error:

Joomla\CMS\Form\Form::getInstance() could not load file

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.