0

I have a system plugin that uses the onContentPrepareForm event to load an xml form fieldset definition into the Joomal CMS Form Object.

        FormHelper::addFieldPrefix('DeAnima\\Plugin\\System\\UserExtendedProfile\\Field');
        FormHelper::addFormPath(JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name . '/forms');
        $form->loadFile('userextendedprofile');

Using a single definition of the xml file and relative data I can load, display and save the extra information. Then in the past I needed to associate a single Joomla user with multiple versions of my xml form fieldset definition. Loading multiple versions of the data is not a problem (into array) and there are no issues with saving. However I didn't find a way to load multiple definitions of the xml file to display the information so I manually created the form layout of for my xml form definition and then used of foreach to display the same layout multiple times.

Updating the code for Joomla 4/5 I wanted to revisit the code. Is there a way to create multiple versions of the the form field definition as loaded in the plugin, which can then be used to display the multiple sets information from the data array.

1 Answer 1

1

Standard way is using Subform field with multiple="true" attribute:

<field
    name="field-name"
    type="subform"
    formsource="path/to/exampleform.xml"
    multiple="true"
    label="Subform Field"
/>

Note this will change your data layout. To keep current data layout you may have to create a custom field type similar to Subform but with field names matching your data.

Another option is running a loop, modifying the XML in PHP to make elements unique and loading it using Joomla\CMS\Form\Form::load() method.

1
  • Hi Sharky. I see that subforms are really powerful and I can see great potential for creating one-to-many data relationships. The Joomla documentation that I have found is a bit sketching but once I have got this working I'll write a complete answer for reference. Feb 13 at 9:09

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.