Hello, today I needed to add a custom field to the Joomla 1.6 administration panel for a component I was developing, and decided to share the “recipe” here on my blog.
This tutorial was developed based on the Official Joomla Documentation.
To create a custom field type for Joomla 1.6, you must create a class that extends a field type already defined by Joomla itself (media, text, etc), or else the base class for this functionality, which is “JFormField”.
The field types defined by Joomla itself are in the following directory:
- joomla/libraries/joomla/form/fields/
We’ll create a specific field to retrieve three cities, to do this, create a file called “cidade.php” inside administrator/components/nome-do-seu-componente/models/fields/cidade.php and add the following code:
The class name should be prefixed with “JFormField” followed by the physical file name, which in this case is “cidade.php”, so the class name will be “JFormFieldCidade” using the CamelCase convention.
Two methods can be implemented by our “Cidade” class, they are:
getLabel()getInput()
Both should return HTML.
The protected getLabel() method will return the label for our custom field, in many cases it isn’t
needed because the class we extend already implements this functionality. On the other hand, the getInput() method
should return the field’s html, which could be an input (text, radio, etc), or a select, for example.
See how our class turned out:
| |
Within your code, you’ll have to process the attributes defined by the user in the XML form definition. Some of these attributes are accessible through protected member variables of JFormField.
For example, the name attribute is available in your code as $this->name. The same goes for: label, description, default, multiple and class.
Other parameters you may have defined can be accessed through $this->element, which is an array, the keys of this array are associative and take the property’s name. For example, the size attribute would be accessed like this: $this->element[‘size’].
Your XML would look like this:
| |