0

I have a set of fields that have been defined in an xml file and then I load the fields with $form->loadFile('myFormName'). Before displaying the fields I would like to manipulate the fields based on a series of parameters defined in the config. I was looking at the joomla profile plugin and the onContentPrepareForm function is doing something very similar to what I want to do. However I see that list fields that are to be manipulated are manually defined in an array.

I have looked through the joomla API but I have not found a function that will return a list of fields. Is there a way to generate dynamically the list of fields from the information that has been loaded with $form->loadFile('myForm')?

2
  • You could have a look at manual.joomla.org/docs/next/general-concepts/forms/… (make sure you've got version 5.0 selected, top right) and see if that helps. There's a little example component which you can download and use as the basis for experimenting. Commented Oct 31, 2023 at 20:31
  • Thank you for the link to the new manual. I didn't know it existed. I will read the information to see if there is anything new that's not in the other documentation. Commented Nov 1, 2023 at 8:17

2 Answers 2

2

There is Joomla\CMS\Form\Form::getFieldset() method for returning an array of fields belonging to a fieldset as Joomla\CMS\Form\FormField objects.

$fieldNames = [];

foreach ($form->getFieldset('myFormName') as $field)
{
    $fieldNames[] = $field->fieldname;
}
1
  • Does exactly what I want. Why does the joomla profile plugin hard code the fields rather get them in this way? Commented Nov 9, 2023 at 20:03
0

After further reading of the joomla documentation, unless I've missed something, there isn't a ready made function to get the field names as defined in a xml form field file. There is a [getXml][1] method (function) which returns the XML form object. From there using [SimpleXMLElement::xpath][2] I was able to iterate through the simple xml elements and create dynamically an array of the form field names.

    $fieldNames = array();
    foreach ($form->getXml()->xpath('//fields//fieldset') as $key => $fieldset) {
        if (((string)$fieldset->attributes()->{'name'} == 'myFormName')) {
            foreach ($fieldset->xpath('field') as $field) {
                $fieldNames[] = (string)$field->attributes()->{'name'};
            }
        }
    }

In the first xpath I find all the fieldset objects currently loaded into the form. I then select the fieldset I want by the name. Using xpath again I select the field object and put the names into my $fieldNames array. This way I avoid having to hard-code the fields I want to manipulate. If I update the xml file then the list of fields is automatically updated in the php code. Perhaps something similar could be done in the joomla profile plugin. [1]: https://api.joomla.org/cms-4/#method_getXml [2]: https://www.php.net/manual/en/simplexmlelement.xpath.php

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.