i have created a dropdown select list custom field as what is taught in joomla document, but i do not get the idea of calling this field in a component template.
the code suggested in the document is like this,
//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!!
but how can it show like a select form? like
<select>
<option></option>
<option></option>
<option></option>
</select>
The field definition code is 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 JFormFieldRank extends JFormField {
//The field class must know its own type through the variable $type.
protected $type = 'Rank';
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
return '<select id="'.$this->id.'" name="'.$this->name.'">'.
'<option value="0" >00 President</option>'.
'<option value="1" >01 Vice President</option>'.
'<option value="10" >10 General Secretary</option>'.
'<option value="20" >20 Financial Chair</option>'.
'<option value="21" >21 Financial Deputy Chair</option>'.
'<option value="30" >30 Academics Chair</option>'.
'<option value="31" >31 Academics Deputy Chair</option>'.
'<option value="40" >40 Public Relation Chair</option>'.
'<option value="41" >41 Public Relation Deputy Chair</option>'.
'<option value="50" >50 Publicity Chair</option>'.
'<option value="51" >51 Publicity Deputy Chair</option>'.
'<option value="60" >60 Social and Sports Chair</option>'.
'<option value="61" >61 Social and Sports Deputy Chair</option>'.
'<option value="70" >70 Logistics and Welfare Chair</option>'.
'<option value="71" >71 Logistics and Welfare Deputy Chair</option>'.
'<option value="80" >80 IPSSSCB Advisor</option>'.
'</select>';
}
}
Second, the xml file in the forms folder, what should be its name? the same with the custom field name? which is rank.xml?
and in the xml file, like following: can i change the name, label, description tag?
<field name="title" type="Rank" label="JGLOBAL_TITLE"
description="JFIELD_TITLE_DESC"
required="true" />
<fieldset addfieldpath="/administrator/components/com_committee/models/fields">
Thank you!
This looks like a totally normal list field to me, why are you defining it as a custom field? That is say type="list" and then put your xml in the xml file just as for any list field (there are dozens of examples in the core forms).
<field name="title" type="list" label="COM_MYCOMPONENT_FIELD_TITLE_LABEL"
description="COM_MYCOMPONNET_TITLE_DESC"
required="true" >
<option value="0" >00 President</option>
<option value="1" >01 Vice President</option>
<option value="10" >10 General Secretary</option>
<option value="20" >20 Financial Chair</option>
<option value="21" >21 Financial Deputy Chair</option>
<option value="30" >30 Academics Chair</option>
...
<option value="80" >80 IPSSSCB Advisor</option>
</field>
Of course you can change the text, that's the whole point of having those as string keys, you make your own keys in your component language file. Again look at any form field in a core form. Best practice would be to use language keys for the values also.