-1

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 '18 at 22:17
  • Because I need to load it in template override, in cases like overrides of this module – Uziel Almeida Oliveira Sep 17 '18 at 3:55
1

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

defined('_JEXEC') or die;

JFormHelper::loadFieldClass('subform');

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';
                break;
            default:
                parent::__set($name, $value);
        }
    }

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

        if ($template === null)
        {
            $db = JFactory::getDbo();
            $query = $db->getQuery(true)
                ->select($db->quoteName('template'))
                ->from($db->quoteName('#__template_styles'))
                ->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, privacy policy and cookie policy

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