Dan7
Dan7

Reputation: 1737

Joomla 2.5: A custom form field type that renders more fields and supports form session

In my component I want to have an admin form that has a custom field type. This custom field actually loads another xml form definition file and renders these fields defined in it. In the custom field class something like this:

<?php

class CustomField extends JFormField
{
    protected function getInput()
    {
        $form = JForm::getInstance("another_form", "path/to/xml");
        $field_names = array_keys($form->getFieldset());

        $html = "";
        foreach ($field_names as $name) {
            $field = $form->getField($name);
            $html .= "<li>" . $field->getLabel() . $field->getInput() . "</li>";
        }

        return $html;
    }

}

It renders fine but how can I make these extra fields integrate with the admin form smoothly so that:

Thanks!

Upvotes: 0

Views: 720

Answers (1)

Riccardo Zorn
Riccardo Zorn

Reputation: 5615

First of all, I would suggest to avoid using the external file altogether and define standard elements for your configuration.

If however you wish to proceed:

When you create a custom element the value is read / written using the name property: i.e.

protected function getInput() {
    return "<input type='hidden' name='$this->name' />";
}

So if you want Joomla to handle the storage of the values, you would need to add an input (hidden) like the one above to hold all your custom input values. You could achieve this binding two scripts to your custom element: one that packs all the values from the custom input fields into a json string, and sets the hidden field's value to this json string (remove \n!); the other that onload restores the values. Make sure you use field names in the json so your configuration will survive if you add / change your xml structure.

This will however produce an ugly result, since the parameters of a component are already in json format, you will have your component configuration with json within json. Ugly, but it shouldn't create too many problems.

Upvotes: 2

Related Questions