Difference between revisions of "Creating a custom form field type"

From Joomla! Documentation

(Update 3.1 to 3.x)
(Several markup changes.)
 
(42 intermediate revisions by 18 users not shown)
Line 1: Line 1:
{{version/tutor|2.5,3.x}}
+
<noinclude><languages /></noinclude>
  
[[JForm]], a feature introduced in Joomla 1.6, lets you easily create HTML forms (<code><form></code>). Forms created using JForm consist of [[Form field|form fields]], implemented as [[JFormField|JFormFields]]. There is a JFormField for each different field type you can find in a form, such as a text field type and a date field type. JForm supports a large selection of standard field types. For a full list, see [[Standard form field types]].
+
<noinclude>{{Joomla version|version=3.x|comment=<translate><!--T:1-->
 +
series</translate>}}</noinclude>
 +
{{-}}
 +
{{page|needs review}}
  
Joomla 1.6 makes it possible to extend standard field types or define your own. For example, if your component manages phone book entries, you might want to define a form field type that outputs a select list of cities. There are several advantages to defining a custom form field type:
+
<translate>
 +
<!--T:2-->
 +
[https://api.joomla.org/cms-3/classes/Joomla.CMS.Form.Form.html JForm], a feature introduced in Joomla! 2.5, lets you easily create HTML forms (''<form>''). Forms created using JForm consist of [[S:MyLanguage/Form field|form fields]], implemented as [https://api.joomla.org/cms-3/classes/Joomla.CMS.Form.FormField.html JFormField]. There is a JFormField for each different field type you can find in a form, such as a text field type and a date field type. JForm supports a large selection of standard field types. For a full list, see [[S:MyLanguage/Standard form field types|Standard form field types]].
 +
</translate>
  
 +
<translate>
 +
<!--T:3-->
 +
Joomla! makes it possible to extend standard field types or define your own. For example, if your component manages phone book entries, you might want to define a form field type that outputs a select list of cities. There are several advantages to defining a custom form field type:
 +
</translate>
 +
 +
<translate>
 +
<!--T:4-->
 
* You will be able to mix standard field types with your custom field type in a JForm-based form.
 
* You will be able to mix standard field types with your custom field type in a JForm-based form.
 
* You will eventually have a reusable code package that can be used easily throughout your code.
 
* You will eventually have a reusable code package that can be used easily throughout your code.
 
* Extensions that collaborate with your extension will be able to create form fields without meddling with your database tables and other internals.
 
* Extensions that collaborate with your extension will be able to create form fields without meddling with your database tables and other internals.
 +
</translate>
  
== Form field type class requirements ==
+
<translate>
 +
== Form Field Type Class Requirements == <!--T:5-->
 +
</translate>
  
A form field type is defined in a [[wikipedia:Class (computer programming)|class]] that must be a (not necessarily direct) subclass of JFormField. To work correctly, the class must define at least two methods:
+
<translate>
 +
<!--T:6-->
 +
A form field type is defined in a [[wikipedia:Class (computer programming)|class]] that must be a (not necessarily direct) subclass of JFormField. To work correctly, the class must define at least three methods:
 +
</translate>
  
* <code>public function getLabel()</code>
+
* ''public function getLabel()''
*: This function will be called to create the label that belongs to your field and must return a HTML string containing it. Since JFormField defines a ready-to-use <code>getLabel()</code> implementation, custom form field types usually do not define their own <code>getLabel()</code>. If you leave it out, the inherited method of creating labels will be used. It is recommended to leave out the <code>getLabel()</code> method for consistency and speed unless you actually want to modify the label's HTML.
+
<translate><!--T:7-->
* <code>public function getInput()</code>
+
*: This function will be called to create the label that belongs to your field and must return an HTML string containing it. Since JFormField defines a ready-to-use ''getLabel()'' implementation, custom form field types usually do not define their own ''getLabel()''. If you leave it out, the inherited method of creating labels will be used. It is recommended to leave out the ''getLabel()'' method for consistency and speed unless you actually want to modify the label's HTML.</translate>
*: This function will be called to create the field itself and must return a HTML string containing it. This is also where most of the processing usually happens. In our phone book City field example, this function will have to retrieve a list of available cities and return a HTML <code><select></code> with the cities inserted as <code><option></code>s.
+
* ''public function getInput()''
 +
<translate><!--T:8-->
 +
*: This function will be called to create the field itself and must return an HTML string containing it. This is also where most of the processing usually happens. In our phone book City field example, this function will have to retrieve a list of available cities and return an HTML ''<select>'' with the cities inserted as ''<option>''s.</translate>
 +
* ''public function getValue()''
 +
<translate><!--T:9-->
 +
*: This function will be called to get the field value. The value is obtained from the function LoadFormData in the Model</translate>
 +
<translate>
 +
<!--T:10-->
 +
Inside your code, you will have to process the attributes set by the field's user in the XML form definition. Some of those attributes are accessible via protected member variables of JFormField. For example, the ''name'' attribute is available in your code as ''$this->name''. Similarly, ''label'', ''description'', ''default'', ''multiple'' and ''class'' are also available as properties of ''$this''. Other parameters you might have defined can be accessed through the ''$this->element'' array: the attribute ''size'' will be in ''$this->element['size']''.
 +
</translate>
  
Inside your code, you will have to process the attributes set by the field's user in the XML form definition. Some of those attributes are accessible via protected member variables of JFormField. For example, the <code>name</code> attribute is available in your code as <code>$this->name</code>. Similarly, <code>label</code>, <code>description</code>, <code>default</code>, <code>multiple</code> and <code>class</code> are also available as properties of <code>$this</code>. Other parameters you might have defined can be accessed through the <code>$this->element</code> array: the attribute <code>size</code> will be in <code>$this->element['size']</code>.
+
<translate>
 +
== Which Class to Subclass? == <!--T:65-->
 +
</translate>
  
== Which class to subclass? ==
+
<translate>
 +
<!--T:11-->
 +
For a form field type to be usable in JForm, it needs to be a subclass of JFormField. However, it does not have to be a direct child of that class. You can also subclass an existing (standard or custom) form field type and thereby inherit useful code.
 +
</translate>
  
For a form field type to be usable in JForm, it needs do be a subclass of JFormField. However, it does not have to be a direct child of that class: you can also subclass an existing (standard or custom) form field type and thereby inherit useful code.
+
<translate>
 +
<!--T:12-->
 +
If your form field type is similar to an existing type, you '''should''' subclass that type. Especially if your form field type is a ''list'', please subclass [https://api.joomla.org/cms-3/classes/JFormFieldList.html JFormFieldList]. You only have to override ''getOptions()'' method to return the options to be shown. The ''getInput()'' method will convert those options to HTML.
 +
</translate>
  
'''If your form field type is quite similar to an existing type,''' you should subclass that type. Especially if your form field type is a '''list''', please subclass [[JFormFieldList]]. You only have to override <code>getOptions()</code> method to return the options to be shown; the <code>getInput()</code> method will convert those options to HTML.
+
<translate><!--T:13-->
 +
To subclass an existing type, for example JFormFieldList, load it by adding the following after ''jimport('joomla.form.formfield');'':</translate>
  
To subclass an existing type, for example JFormFieldList, load it by adding the following to after <code>jimport('joomla.form.formfield');</code>:
+
<syntaxhighlight lang="php">
 
 
<source lang="php">
 
 
jimport('joomla.form.helper');
 
jimport('joomla.form.helper');
 
JFormHelper::loadFieldClass('list');
 
JFormHelper::loadFieldClass('list');
</source>
+
</syntaxhighlight>
  
'''If your form field type is unlike any existing type,''' subclass JFormField directly.
+
<translate><!--T:14-->
 +
If your form field type is unlike any existing type, subclass ''JFormField'' directly.</translate>
  
== Location of files ==
+
<translate>
 +
== Location of Files == <!--T:15-->
 +
</translate>
  
* The standard form field types are located in <code>joomla/libraries/joomla/form/fields/</code>. You should not store custom fields there, nor should you have to use this path in your own code, but the standard types are usually good examples.
+
<translate><!--T:16-->
* The custom field types that belong to your component are usually located in <code>administrator/components/<name of your component>/models/fields</code>. You can specify this or another path in your code:
+
* The standard form field types are located in ''libraries/joomla/form/fields/''. You should not store custom fields there, nor should you have to use this path in your own code. The standard types are usually good examples.</translate>
<source lang="php">JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');</source>
+
<translate><!--T:17-->
* The XML files that define forms are usually located in <code>administrator/components/<name of your component>/models/forms</code>. Use something like the following snippet to specify a path to your forms:
+
* The custom field types that belong to your component are usually located in ''administrator/components/<name of your component>/models/fields''. You can specify this or another path in your code:</translate>
<source lang="php">JForm::addFormPath(JPATH_COMPONENT . '/models/forms');</source>
+
<syntaxhighlight lang="php">JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');</syntaxhighlight>
 +
<translate><!--T:18-->
 +
* The XML files that define forms are usually located in ''administrator/components/<name of your component>/models/forms''. Use something like the following snippet to specify a path to your forms:</translate>
 +
<syntaxhighlight lang="php">JForm::addFormPath(JPATH_COMPONENT . '/models/forms');</syntaxhighlight>
  
== Naming conventions and skeleton ==
+
<translate>
 +
== Naming Conventions and Skeleton == <!--T:19-->
 +
</translate>
  
In this section, <ComponentName> represents the camel-cased name of your component and <TypeName> represents the camel-cased name of your form field type. The field's class should be placed in <code>administrator/components/<name of your component>/models/fields/<name of your field>.php</code>, and look like this:
+
<translate><!--T:20-->
 +
In this section, <ComponentName> represents the camel-cased name of your component and <FieldName> represents the camel-cased name of your form field type. The field's class should be placed in ''administrator/components/<name of your component>/models/fields/<name of your field>.php'', and look like this:</translate>
  
<source lang="php"><?php
+
<syntaxhighlight lang="php"><?php
 
// Check to ensure this file is included in Joomla!
 
// Check to ensure this file is included in Joomla!
 
defined('_JEXEC') or die('Restricted access');
 
defined('_JEXEC') or die('Restricted access');
Line 66: Line 110:
 
// code that returns HTML that will be shown as the form field
 
// code that returns HTML that will be shown as the form field
 
}
 
}
}  
+
}
</source>
+
</syntaxhighlight>
  
=== Grouping custom field types ===
+
<translate>
 +
=== Grouping Custom Field Types === <!--T:21-->
 +
</translate>
  
'''Warning: this information is partially incorrect and needs to be improved.'''
+
<translate><!--T:22-->
 +
'''Warning: this information is partially incorrect and needs to be improved.'''</translate>
  
Custom field types can be grouped by using an underscore in the field name. A field class with a name for example like "JFormFieldMy_randomField" must be stored in <code>administrator/components/<name of your component>/models/fields/my/randomField.php</code>. We can prefix our form field names with some group name, then we put an underscore and then a name of a field.
+
<translate><!--T:23-->
 +
Custom field types can be grouped by using an underscore in the field name. A field class with a name for example like "JFormFieldMy_randomField" must be stored in ''administrator/components/<name of your component>/models/fields/my/randomField.php''. We can prefix our form field names with some group name, then we put an underscore and then a name of a field.</translate>
  
== An example custom field type ==
+
<translate>
 +
== An Example Custom Field Type == <!--T:24-->
 +
</translate>
  
Suppose you're working on your component named ''com_phonebook'' and you want to define a field that contains cities. Create the file <code>administrator/components/com_phonebook/models/fields/city.php</code> and write something similar to the following:
+
<translate><!--T:25-->
 +
Suppose you're working on your component named ''com_phonebook'' and you want to define a field that contains cities. Create the file ''administrator/components/com_phonebook/models/fields/city.php'' and write something similar to the following:</translate>
  
<source lang="php"><?php
+
<syntaxhighlight lang="php"><?php
 
// Check to ensure this file is included in Joomla!
 
// Check to ensure this file is included in Joomla!
 
defined('_JEXEC') or die('Restricted access');
 
defined('_JEXEC') or die('Restricted access');
Line 86: Line 137:
  
 
class JFormFieldCity extends JFormField {
 
class JFormFieldCity extends JFormField {
+
 
 
protected $type = 'City';
 
protected $type = 'City';
  
Line 98: Line 149:
 
      '</select>';
 
      '</select>';
 
}
 
}
}  
+
}
</source>
+
</syntaxhighlight>
 +
 
 +
<translate>
 +
<!--T:53-->
 +
A more advanced approach is extending the ''JFormFieldList'' class. Suppose you want to create a drop-down of cities dynamically from database based on a dynamic condition. You can do this in the following way:</translate>
 +
 
 +
<syntaxhighlight lang="php"><?php
 +
// Check to ensure this file is included in Joomla!
 +
defined('_JEXEC') or die('Restricted access');
  
=== Using the custom field type ===
+
JFormHelper::loadFieldClass('list');
==== Linked with a form ====
 
To use the field type City, we need to update the XML file that contains the form fields. Open your XML file located in <code>administrator/components/com_phonebook/models/forms</code> and add the field in the usual way:
 
  
<source lang="xml"><field name="title" type="City" label="JGLOBAL_TITLE"
+
class JFormFieldCity extends JFormFieldList {
 +
 
 +
protected $type = 'City';
 +
 
 +
public function getOptions() {
 +
                $app = JFactory::getApplication();
 +
                $country = $app->input->get('country'); //country is the dynamic value which is being used in the view
 +
                $db = JFactory::getDbo();
 +
                $query = $db->getQuery(true);
 +
                $query->select('a.cityname')->from('`#__tablename` AS a')->where('a.country = "'.$country.'" ');
 +
$rows = $db->setQuery($query)->loadObjectlist();
 +
                foreach($rows as $row){
 +
                    $cities[] = $row->cityname;
 +
                }
 +
                // Merge any additional options in the XML definition.
 +
$options = array_merge(parent::getOptions(), $cities);
 +
                return $options;
 +
}
 +
}
 +
</syntaxhighlight>
 +
 
 +
<translate><!--T:54-->
 +
The above example shows a simple query which will show a list of cities from a table where city name belongs to its country respectively. You can create a drop-down based on more complex queries.</translate>
 +
 
 +
<translate>
 +
== Setting the Values of a List Option and Using JSON or an API Instead of a Database Call == <!--T:66-->
 +
</translate>
 +
<translate>
 +
<!--T:67-->
 +
If you want to use an API call instead of a database call to build a custom list item, use the following code.
 +
 
 +
<!--T:68-->
 +
This field was created in a module. To get it to work it needs to be saved to
 +
''mod_modulename/models/fields/stackexchangesites.php''.
 +
The naming convention is important as it is used within our function name.</translate>
 +
<syntaxhighlight lang="php">
 +
<?php
 +
// Check to ensure this file is included in Joomla!
 +
defined('_JEXEC') or die('Restricted access');
 +
 
 +
// call the list field type
 +
JFormHelper::loadFieldClass('list');
 +
 
 +
// The class name must always be the same as the filename (in camel case)
 +
// extend the list field type
 +
class JFormFieldStackexchangesites extends JFormFieldList
 +
{
 +
    //The field class must know its own type through the variable $type.
 +
    protected $type = 'Stackexchangesites';
 +
 
 +
    // get the options for the list field
 +
    public function getOptions()
 +
    {
 +
      // insert your JSON here or else call an API
 +
        $json = {"api_site_parameter":"meta.stackoverflow","site_url":"https://meta.stackoverflow.com"~}
 +
        // decode the JSON
 +
        $sites = json_decode($json, true);
 +
 
 +
        // use a for each to iterate over the JSON
 +
        foreach($sites['items'] as $site)
 +
        {
 +
          // choose the element of the JSON we want, and set it as a variable so we can use it in our array.
 +
            $site = $site['api_site_parameter'];
 +
            $site_url = $site['site_url'];
 +
            // set an array and start adding values to it.  Set another array within our array to set our value / text items.
 +
            $stackExchangesSitesOptions[] = array("value" => $site, "text" => $site_url);
 +
        }
 +
 
 +
        // Merge any additional options in the XML definition.
 +
        $options = array_merge(parent::getOptions(), $stackExchangesSitesOptions);
 +
        return $options;
 +
    }
 +
}</syntaxhighlight>
 +
<translate>
 +
<!--T:69-->
 +
On the Frontend simply calling the parameter gets us the value.</translate>
 +
<syntaxhighlight lang="php">
 +
$stackexchangesites = $params->get('stackexchangesites');
 +
</syntaxhighlight>
 +
 
 +
<translate>
 +
=== Pitfalls === <!--T:55-->
 +
</translate>
 +
<translate><!--T:56-->
 +
Loading a custom field can result in a Fatal Error if a core field exists with the same filename and the custom field extends the core field.</translate>
 +
 
 +
<translate><!--T:57-->
 +
Consider a file ''testfields/radio.php'' containing</translate>
 +
<syntaxhighlight lang="php">
 +
<?php
 +
class TestFormFieldRadio extends JFormFieldRadio {}
 +
</syntaxhighlight>
 +
 
 +
<translate><!--T:58-->
 +
Calling ''JFormHelper::loadFieldClass('radio')'' will yield a Fatal error: Class 'JFormFieldRadio' not found.</translate>
 +
 
 +
<translate><!--T:59-->
 +
There are two reasons for this.</translate>
 +
 
 +
<translate><!--T:60-->
 +
# JLoader cannot autoload ''JFormFieldRadio'' because the class name (''JFormField*'') does not match the path name (''joomla/form/fields/*'' - notice the plural on fields).</translate>
 +
<translate><!--T:61-->
 +
# JFormHelper cannot load ''JFormFieldRadio'' because custom paths are scanned first and the requested field type('radio') gets resolved before the core classes are reached.</translate>
 +
 
 +
<translate><!--T:62-->
 +
'''Solution'''</translate>
 +
 
 +
<translate><!--T:63-->
 +
Require the core field file directly:</translate>
 +
 
 +
<syntaxhighlight lang="php">
 +
<?php
 +
require_once JPATH_LIBRARIES . '/joomla/form/fields/radio.php';
 +
 
 +
class TestFormFieldRadio extends JFormFieldRadio {}
 +
</syntaxhighlight>
 +
 
 +
<translate><!--T:64-->
 +
and use ''JFormHelper::loadFieldClass'' properly with 'test.radio' instead of 'radio'.</translate>
 +
 
 +
<translate>
 +
=== Using the Custom Field Type === <!--T:26-->
 +
</translate>
 +
<translate>
 +
==== Linked with a Form ==== <!--T:27-->
 +
</translate>
 +
<translate><!--T:28-->
 +
To use the field type City, we need to update the XML file that contains the form fields. Open your XML file located in ''administrator/components/com_phonebook/models/forms'' and add the field in the usual way:</translate>
 +
 
 +
<syntaxhighlight lang="xml"><field name="title" type="City" label="JGLOBAL_TITLE"
 
description="JFIELD_TITLE_DESC"
 
description="JFIELD_TITLE_DESC"
required="true" /></source>
+
required="true" /></syntaxhighlight>
The type name is cAsE-sEnSiTiVe.
 
  
 +
<translate><!--T:29-->
 +
The attribute name is cAsE-sEnSiTiVe.</translate>
  
In addition, you may need to add the field path to the parent <fieldset>:
+
<translate><!--T:30-->
 +
In addition, you may need to add the field path to the parent <fieldset>:</translate>
  
<source lang="xml">
+
<syntaxhighlight lang="xml">
 
<fieldset addfieldpath="/administrator/components/<component name>/models/fields">
 
<fieldset addfieldpath="/administrator/components/<component name>/models/fields">
</source>
+
</syntaxhighlight>
 +
 
 +
<translate>
 +
==== Not Linked with a Form ==== <!--T:31-->
 +
</translate>
 +
<translate><!--T:32-->
 +
For example, when you need the field as a dropdown in a component as admin/site filter.</translate>
  
==== Not linked with a form ====
+
<syntaxhighlight lang="php">
E.g. when you need the field as a dropdown in a component as admin/site filter.
 
<source lang="php">
 
 
//Get custom field
 
//Get custom field
 
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
 
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
 
$cities = JFormHelper::loadFieldType('City', false);
 
$cities = JFormHelper::loadFieldType('City', false);
 
$cityOptions=$cities->getOptions(); // works only if you set your field getOptions on public!!
 
$cityOptions=$cities->getOptions(); // works only if you set your field getOptions on public!!
</source>
+
</syntaxhighlight>
  
== Overriding <code>getLabel()</code> ==
+
<translate>
 +
== Overriding ''getLabel()'' == <!--T:33-->
 +
</translate>
  
As mentioned in the section [[#Form field type class requirements|Form field type class requirements]], custom form field types usually do not define their own <code>getLabel()</code>. If you do want to create a custom label, you can still make use of the <code>getLabel()</code> that every field type class inherits from JFormField, for example by defining it as follows:
+
<translate><!--T:34-->
 +
As mentioned in the section [[#Form field type class requirements|Form field type class requirements]], custom form field types usually do not define their own ''getLabel()''. If you do want to create a custom label, you can still make use of the ''getLabel()'' that every field type class inherits from JFormField by defining it as follows:</translate>
  
<source lang="php">public function getLabel() {
+
<syntaxhighlight lang="php">public function getLabel() {
 
     return '<span style="text-decoration: underline;">' . parent::getLabel() . '</span>';
 
     return '<span style="text-decoration: underline;">' . parent::getLabel() . '</span>';
} </source>
+
} </syntaxhighlight>
  
 +
<translate><!--T:35-->
 
This code will underline your form labels. (Please note that if your goal is to underline form labels, using [[CSS]] is the preferred way.)
 
This code will underline your form labels. (Please note that if your goal is to underline form labels, using [[CSS]] is the preferred way.)
  
If you want to do something completely different, you can of course also override it completely:
+
<!--T:36-->
 +
If you want to do something completely different, you can of course also override it completely:</translate>
  
<source lang="php">public function getLabel() {
+
<syntaxhighlight lang="php">public function getLabel() {
 
// Initialize variables.
 
// Initialize variables.
 
$label = '';
 
$label = '';
Line 149: Line 346:
 
$class = !empty($this->description) ? 'hasTip' : '';
 
$class = !empty($this->description) ? 'hasTip' : '';
 
$class = $this->required == true ? $class.' required' : $class;
 
$class = $this->required == true ? $class.' required' : $class;
+
 
 
// Add replace checkbox
 
// Add replace checkbox
 
$replace = '<input type="checkbox" name="update['.$this->name.']" value="1" />';
 
$replace = '<input type="checkbox" name="update['.$this->name.']" value="1" />';
+
 
 
// Add the opening label tag and main attributes attributes.
 
// Add the opening label tag and main attributes attributes.
 
$label .= '<label id="'.$this->id.'-lbl" for="'.$this->id.'" class="'.$class.'"';
 
$label .= '<label id="'.$this->id.'-lbl" for="'.$this->id.'" class="'.$class.'"';
Line 164: Line 361:
 
// Add the label text and closing tag.
 
// Add the label text and closing tag.
 
$label .= '>'.$replace.JText::_($text).'</label>';
 
$label .= '>'.$replace.JText::_($text).'</label>';
 
return $label;
 
}</source>
 
  
This example will add a checkbox before the label.
+
return $label;
 +
}</syntaxhighlight>
 +
 
 +
<translate><!--T:37-->
 +
This example will add a checkbox inside the label.</translate>
 +
 
 +
== Sample Component Code ==
 +
Below is the code for a small component which you can install and run and can adapt to experiment further with custom fields. The component includes two custom fields
 +
# A "City" field as described above (except that the options are hard-coded rather than being selected from a database). We'll also allow multiple Cities to be selected and pre-select two of them as default.
 +
# A custom "time" field which maps to the HTML "time" input type and includes underlining the label and support for setting a minimum and maximum time.
 +
 
 +
Create the following five files in a folder called com_custom_fields. Then zip up this folder to create com_custom_fields.zip. Install this component on your Joomla instance. Once installed, navigate on your browser to your Joomla site and add to the URL the parameter ''&option=com_custom_fields''. This should display the form with the two custom fields. You can submit the form and see the HTTP POST parameters using the browser development tools but the component doesn't contain any code which handles those parameters.
 +
 
 +
(As described in [[Basic_form_guide#MVC_and_other_considerations|Basic form guide]], the approach below isn't the recommended way to design Joomla components but it's written in this minimalist fashion to focus on the custom fields aspects).
 +
 
 +
''com_custom_fields.xml'' The manifest file for the component.
 +
<syntaxhighlight lang="xml">
 +
<?xml version="1.0" encoding="utf-8"?>
 +
<extension type="component" version="3.1.0" method="upgrade">
 +
 
 +
<name>com_custom_fields</name>
 +
<version>1.0.0</version>
 +
<description>Custom Fields demo component</description>
 +
 
 +
<administration>
 +
</administration>
 +
 
 +
<files folder="site">
 +
<filename>custom_fields.php</filename>
 +
<filename>form_definition.xml</filename>
 +
<filename>City.php</filename>
 +
<filename>Time.php</filename>
 +
</files>
 +
</extension>
 +
</syntaxhighlight>
 +
 
 +
''custom_fields.php'' The main code file which is run when an HTTP GET or POST is directed towards this component.
 +
<syntaxhighlight lang="php">
 +
<?php
 +
defined('_JEXEC') or die('Restricted Access');
 +
 
 +
use Joomla\CMS\Form\Form;
 +
 
 +
$form = Form::getInstance("sample", __DIR__ . "/form_definition.xml", array("control" => "jform"));
 +
Form::addFieldPath(__DIR__);
 +
?>
 +
<form action="<?php echo JRoute::_('index.php?option=com_custom_fields'); ?>"
 +
    method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">
 +
 
 +
<?php echo $form->renderField('mytime');  ?>
 +
 
 +
<?php echo $form->renderField('mycity');  ?>
 +
 
 +
<button type="submit">Submit</button>
 +
</form>
 +
</syntaxhighlight>
 +
 
 +
''form_definition.xml'' File containing the XML for the form definition, basically the two custom fields.
 +
<syntaxhighlight lang="xml">
 +
<?xml version="1.0" encoding="utf-8"?>
 +
<form name="myForm">
 +
<field
 +
name="mytime"
 +
type="time"
 +
label="Time:"
 +
class="inputbox"
 +
min="09:00"
 +
max="17:30"
 +
required="true" />
 +
<field name="mycity"
 +
type="City"
 +
label="City:"
 +
required="true"
 +
multiple="true"
 +
class="inputbox" />
 +
</form>
 +
</syntaxhighlight>
 +
 
 +
''City.php'' PHP code for the City custom field.
 +
<syntaxhighlight lang="php">
 +
<?php
 +
defined('_JEXEC') or die('Restricted access');
 +
use Joomla\CMS\Form\FormHelper;
 +
 
 +
FormHelper::loadFieldClass('list');
 +
 
 +
class JFormFieldCity extends JFormFieldList {
 +
 
 +
protected $type = 'City';
 +
 
 +
public function getOptions() {
 +
$cities = array(
 +
array('value' => 1, 'text' => 'New York'),
 +
array('value' => 2, 'text' => 'Chicago'),
 +
array('value' => 3, 'text' => 'San Francisco'),
 +
);
 +
// Merge any additional options in the XML definition.
 +
$options = array_merge(parent::getOptions(), $cities);
 +
 
 +
// pre-select values 2 and 3 by setting the protected $value property
 +
$this->value = array(2, 3);
 +
 
 +
return $options;
 +
}
 +
}
 +
</syntaxhighlight>
 +
 
 +
''Time.php'' PHP code for the time custom field.
 +
<syntaxhighlight lang="php">
 +
<?php
 +
defined('JPATH_PLATFORM') or die;
 +
 
 +
use Joomla\CMS\Factory;
 +
use Joomla\CMS\Form\FormField;
 +
 
 +
class JFormFieldTime extends FormField
 +
{
 +
protected $type = 'time';
 +
 
 +
protected function getInput()
 +
{
 +
// get relevant attributes which were defined in the XML form definition
 +
$attr = !empty($this->class) ? ' class="' . $this->class . '"' : '';
 +
$attr .= !empty($this->element['min']) ? ' min="' . $this->element['min'] . '"' : '';
 +
$attr .= !empty($this->element['max']) ? ' max="' . $this->element['max'] . '"' : '';
 +
 
 +
// set up html, including the value and other attributes
 +
$html = '<input type="time" name="' . $this->name . '" value="' . $this->value . '"' . $attr . '/>';
 +
 
 +
return $html;
 +
}
 +
 
 +
public function getLabel() {
 +
return '<span style="text-decoration: underline;">' . parent::getLabel() . '</span>';
 +
}
 +
}
 +
</syntaxhighlight>
  
<noinclude>[[Category:Development]] [[Category:Form fields]] [[Category:Joomla! 2.5]] [[Category:Joomla! 3.1]]</noinclude>
+
<noinclude>
 +
<translate>
 +
<!--T:52-->
 +
[[Category:Development]]
 +
[[Category:Extension development]]
 +
[[Category:Form fields]]
 +
[[Category:Joomla! 2.5]]
 +
[[Category:Joomla! 3.x]]
 +
</translate>
 +
</noinclude>

Latest revision as of 10:03, 9 October 2022

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français
Joomla! 
3.x
series
Copyedit.png
This Article Needs Your Help

This article is tagged because it NEEDS REVIEW. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.


JForm, a feature introduced in Joomla! 2.5, lets you easily create HTML forms (<form>). Forms created using JForm consist of form fields, implemented as JFormField. There is a JFormField for each different field type you can find in a form, such as a text field type and a date field type. JForm supports a large selection of standard field types. For a full list, see Standard form field types.

Joomla! makes it possible to extend standard field types or define your own. For example, if your component manages phone book entries, you might want to define a form field type that outputs a select list of cities. There are several advantages to defining a custom form field type:

  • You will be able to mix standard field types with your custom field type in a JForm-based form.
  • You will eventually have a reusable code package that can be used easily throughout your code.
  • Extensions that collaborate with your extension will be able to create form fields without meddling with your database tables and other internals.

Form Field Type Class Requirements[edit]

A form field type is defined in a class that must be a (not necessarily direct) subclass of JFormField. To work correctly, the class must define at least three methods:

  • public function getLabel()
    This function will be called to create the label that belongs to your field and must return an HTML string containing it. Since JFormField defines a ready-to-use getLabel() implementation, custom form field types usually do not define their own getLabel(). If you leave it out, the inherited method of creating labels will be used. It is recommended to leave out the getLabel() method for consistency and speed unless you actually want to modify the label's HTML.
  • public function getInput()
    This function will be called to create the field itself and must return an HTML string containing it. This is also where most of the processing usually happens. In our phone book City field example, this function will have to retrieve a list of available cities and return an HTML <select> with the cities inserted as <option>s.
  • public function getValue()
    This function will be called to get the field value. The value is obtained from the function LoadFormData in the Model

Inside your code, you will have to process the attributes set by the field's user in the XML form definition. Some of those attributes are accessible via protected member variables of JFormField. For example, the name attribute is available in your code as $this->name. Similarly, label, description, default, multiple and class are also available as properties of $this. Other parameters you might have defined can be accessed through the $this->element array: the attribute size will be in $this->element['size'].

Which Class to Subclass?[edit]

For a form field type to be usable in JForm, it needs to be a subclass of JFormField. However, it does not have to be a direct child of that class. You can also subclass an existing (standard or custom) form field type and thereby inherit useful code.

If your form field type is similar to an existing type, you should subclass that type. Especially if your form field type is a list, please subclass JFormFieldList. You only have to override getOptions() method to return the options to be shown. The getInput() method will convert those options to HTML.

To subclass an existing type, for example JFormFieldList, load it by adding the following after jimport('joomla.form.formfield');:

jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');

If your form field type is unlike any existing type, subclass JFormField directly.

Location of Files[edit]

  • The standard form field types are located in libraries/joomla/form/fields/. You should not store custom fields there, nor should you have to use this path in your own code. The standard types are usually good examples.
  • The custom field types that belong to your component are usually located in administrator/components/<name of your component>/models/fields. You can specify this or another path in your code:
JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');
  • The XML files that define forms are usually located in administrator/components/<name of your component>/models/forms. Use something like the following snippet to specify a path to your forms:
JForm::addFormPath(JPATH_COMPONENT . '/models/forms');

Naming Conventions and Skeleton[edit]

In this section, <ComponentName> represents the camel-cased name of your component and <FieldName> represents the camel-cased name of your form field type. The field's class should be placed in administrator/components/<name of your component>/models/fields/<name of your field>.php, and look like this:

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.form.formfield');

// The class name must always be the same as the filename (in camel case)
class JFormField<FieldName> extends JFormField {

	//The field class must know its own type through the variable $type.
	protected $type = '<FieldName>';

	public function getLabel() {
		// code that returns HTML that will be shown as the label
	}

	public function getInput() {
		// code that returns HTML that will be shown as the form field
	}
}

Grouping Custom Field Types[edit]

Warning: this information is partially incorrect and needs to be improved.

Custom field types can be grouped by using an underscore in the field name. A field class with a name for example like "JFormFieldMy_randomField" must be stored in administrator/components/<name of your component>/models/fields/my/randomField.php. We can prefix our form field names with some group name, then we put an underscore and then a name of a field.

An Example Custom Field Type[edit]

Suppose you're working on your component named com_phonebook and you want to define a field that contains cities. Create the file administrator/components/com_phonebook/models/fields/city.php and write something similar to the following:

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.form.formfield');

class JFormFieldCity extends JFormField {

	protected $type = 'City';

	// getLabel() left out

	public function getInput() {
		return '<select id="'.$this->id.'" name="'.$this->name.'">'.
		       '<option value="1" >New York</option>'.
		       '<option value="2" >Chicago</option>'.
		       '<option value="3" >San Francisco</option>'.
		       '</select>';
	}
}

A more advanced approach is extending the JFormFieldList class. Suppose you want to create a drop-down of cities dynamically from database based on a dynamic condition. You can do this in the following way:

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

JFormHelper::loadFieldClass('list');

class JFormFieldCity extends JFormFieldList {

	protected $type = 'City';

	public function getOptions() {
                $app = JFactory::getApplication();
                $country = $app->input->get('country'); //country is the dynamic value which is being used in the view
                $db = JFactory::getDbo();
                $query = $db->getQuery(true);
                $query->select('a.cityname')->from('`#__tablename` AS a')->where('a.country = "'.$country.'" ');
		$rows = $db->setQuery($query)->loadObjectlist();
                foreach($rows as $row){
                    $cities[] = $row->cityname;
                }
                // Merge any additional options in the XML definition.
		$options = array_merge(parent::getOptions(), $cities);
                return $options;
	}
}

The above example shows a simple query which will show a list of cities from a table where city name belongs to its country respectively. You can create a drop-down based on more complex queries.

Setting the Values of a List Option and Using JSON or an API Instead of a Database Call[edit]

If you want to use an API call instead of a database call to build a custom list item, use the following code.

This field was created in a module. To get it to work it needs to be saved to mod_modulename/models/fields/stackexchangesites.php. The naming convention is important as it is used within our function name.

<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

// call the list field type
JFormHelper::loadFieldClass('list');

// The class name must always be the same as the filename (in camel case)
// extend the list field type
class JFormFieldStackexchangesites extends JFormFieldList
{
    //The field class must know its own type through the variable $type.
    protected $type = 'Stackexchangesites';

    // get the options for the list field
    public function getOptions()
    {
       // insert your JSON here or else call an API
        $json = {"api_site_parameter":"meta.stackoverflow","site_url":"https://meta.stackoverflow.com"~}
        // decode the JSON
        $sites = json_decode($json, true);

        // use a for each to iterate over the JSON
        foreach($sites['items'] as $site)
        {
           // choose the element of the JSON we want, and set it as a variable so we can use it in our array.
            $site = $site['api_site_parameter'];
            $site_url = $site['site_url'];
            // set an array and start adding values to it.  Set another array within our array to set our value / text items.
            $stackExchangesSitesOptions[] = array("value" => $site, "text" => $site_url);
        }

        // Merge any additional options in the XML definition.
        $options = array_merge(parent::getOptions(), $stackExchangesSitesOptions);
        return $options;
    }
}

On the Frontend simply calling the parameter gets us the value.

$stackexchangesites = $params->get('stackexchangesites');

Pitfalls[edit]

Loading a custom field can result in a Fatal Error if a core field exists with the same filename and the custom field extends the core field.

Consider a file testfields/radio.php containing

<?php
class TestFormFieldRadio extends JFormFieldRadio {}

Calling JFormHelper::loadFieldClass('radio') will yield a Fatal error: Class 'JFormFieldRadio' not found.

There are two reasons for this.

  1. JLoader cannot autoload JFormFieldRadio because the class name (JFormField*) does not match the path name (joomla/form/fields/* - notice the plural on fields).
  2. JFormHelper cannot load JFormFieldRadio because custom paths are scanned first and the requested field type('radio') gets resolved before the core classes are reached.

Solution

Require the core field file directly:

<?php
require_once JPATH_LIBRARIES . '/joomla/form/fields/radio.php';

class TestFormFieldRadio extends JFormFieldRadio {}

and use JFormHelper::loadFieldClass properly with 'test.radio' instead of 'radio'.

Using the Custom Field Type[edit]

Linked with a Form[edit]

To use the field type City, we need to update the XML file that contains the form fields. Open your XML file located in administrator/components/com_phonebook/models/forms and add the field in the usual way:

<field name="title" type="City" label="JGLOBAL_TITLE"
	description="JFIELD_TITLE_DESC"
	required="true" />

The attribute name is cAsE-sEnSiTiVe.

In addition, you may need to add the field path to the parent <fieldset>:

<fieldset addfieldpath="/administrator/components/<component name>/models/fields">

Not Linked with a Form[edit]

For example, when you need the field as a dropdown in a component as admin/site filter.

//Get custom field
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields');
$cities = JFormHelper::loadFieldType('City', false);
$cityOptions=$cities->getOptions(); // works only if you set your field getOptions on public!!

Overriding getLabel()[edit]

As mentioned in the section Form field type class requirements, custom form field types usually do not define their own getLabel(). If you do want to create a custom label, you can still make use of the getLabel() that every field type class inherits from JFormField by defining it as follows:

public function getLabel() {
     return '<span style="text-decoration: underline;">' . parent::getLabel() . '</span>';
}

This code will underline your form labels. (Please note that if your goal is to underline form labels, using CSS is the preferred way.)

If you want to do something completely different, you can of course also override it completely:

public function getLabel() {
	// Initialize variables.
	$label = '';
	$replace = '';

	// Get the label text from the XML element, defaulting to the element name.
	$text = $this->element['label'] ? (string) $this->element['label'] : (string) $this->element['name'];

	// Build the class for the label.
	$class = !empty($this->description) ? 'hasTip' : '';
	$class = $this->required == true ? $class.' required' : $class;

	// Add replace checkbox
	$replace = '<input type="checkbox" name="update['.$this->name.']" value="1" />';

	// Add the opening label tag and main attributes attributes.
	$label .= '<label id="'.$this->id.'-lbl" for="'.$this->id.'" class="'.$class.'"';

	// If a description is specified, use it to build a tooltip.
	if (!empty($this->description)) {
		$label .= ' title="'.htmlspecialchars(trim(JText::_($text), ':').'::' .
				JText::_($this->description), ENT_COMPAT, 'UTF-8').'"';
	}

	// Add the label text and closing tag.
	$label .= '>'.$replace.JText::_($text).'</label>';

	return $label;
}

This example will add a checkbox inside the label.

Sample Component Code[edit]

Below is the code for a small component which you can install and run and can adapt to experiment further with custom fields. The component includes two custom fields

  1. A "City" field as described above (except that the options are hard-coded rather than being selected from a database). We'll also allow multiple Cities to be selected and pre-select two of them as default.
  2. A custom "time" field which maps to the HTML "time" input type and includes underlining the label and support for setting a minimum and maximum time.

Create the following five files in a folder called com_custom_fields. Then zip up this folder to create com_custom_fields.zip. Install this component on your Joomla instance. Once installed, navigate on your browser to your Joomla site and add to the URL the parameter &option=com_custom_fields. This should display the form with the two custom fields. You can submit the form and see the HTTP POST parameters using the browser development tools but the component doesn't contain any code which handles those parameters.

(As described in Basic form guide, the approach below isn't the recommended way to design Joomla components but it's written in this minimalist fashion to focus on the custom fields aspects).

com_custom_fields.xml The manifest file for the component.

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.1.0" method="upgrade">

	<name>com_custom_fields</name>
	<version>1.0.0</version>
	<description>Custom Fields demo component</description>

	<administration>
	</administration>

	<files folder="site">
		<filename>custom_fields.php</filename>
		<filename>form_definition.xml</filename>
		<filename>City.php</filename>
		<filename>Time.php</filename>
	</files>
</extension>

custom_fields.php The main code file which is run when an HTTP GET or POST is directed towards this component.

<?php
defined('_JEXEC') or die('Restricted Access');

use Joomla\CMS\Form\Form;

$form = Form::getInstance("sample", __DIR__ . "/form_definition.xml", array("control" => "jform"));
Form::addFieldPath(__DIR__);
?>
<form action="<?php echo JRoute::_('index.php?option=com_custom_fields'); ?>"
    method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">

	<?php echo $form->renderField('mytime');  ?>

	<?php echo $form->renderField('mycity');  ?>

	<button type="submit">Submit</button>
</form>

form_definition.xml File containing the XML for the form definition, basically the two custom fields.

<?xml version="1.0" encoding="utf-8"?>
<form name="myForm">
	<field
		name="mytime"
		type="time"
		label="Time:"
		class="inputbox"
		min="09:00"
		max="17:30"
		required="true" />
	<field name="mycity"
		type="City"
		label="City:"
		required="true"
		multiple="true"
		class="inputbox" />
</form>

City.php PHP code for the City custom field.

<?php
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Form\FormHelper;

FormHelper::loadFieldClass('list');

class JFormFieldCity extends JFormFieldList {

	protected $type = 'City';

	public function getOptions() {
		$cities = array(
					array('value' => 1, 'text' => 'New York'),
					array('value' => 2, 'text' => 'Chicago'),
					array('value' => 3, 'text' => 'San Francisco'),
					);
		// Merge any additional options in the XML definition.
		$options = array_merge(parent::getOptions(), $cities);

		// pre-select values 2 and 3 by setting the protected $value property
		$this->value = array(2, 3);

		return $options;
	}
}

Time.php PHP code for the time custom field.

<?php
defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormField;

class JFormFieldTime extends FormField
{
	protected $type = 'time';

	protected function getInput()
	{
		// get relevant attributes which were defined in the XML form definition
		$attr = !empty($this->class) ? ' class="' . $this->class . '"' : '';
		$attr .= !empty($this->element['min']) ? ' min="' . $this->element['min'] . '"' : '';
		$attr .= !empty($this->element['max']) ? ' max="' . $this->element['max'] . '"' : '';

		// set up html, including the value and other attributes
		$html = '<input type="time" name="' . $this->name . '" value="' . $this->value . '"' . $attr . '/>';

		return $html;
	}

	public function getLabel() {
		return '<span style="text-decoration: underline;">' . parent::getLabel() . '</span>';
	}
}