I've created an extended subform field type called families.php
, basically I want to have my own custom layout for this custom field, it makes sense I don't want to mess up everything just for this case.
jimport('joomla.form.formfield');
require_once JPATH_LIBRARIES . '/joomla/form/fields/subform.php';
class JFormFieldFamilies extends JFormFieldSubForm {
/**
* Field specs
*/
protected $type = 'Families';
protected $formsource;
protected $layout = 'joomla.form.field.families'; // THIS IS IMPORTANT PART, see below
protected $groupByFieldset = false;
/**
* Cached large array of the families.
*/
public static $families = array();
/**
* Method to attach a JForm object to the field.
*/
public function setup(SimpleXMLElement $element, $value, $group = null)
{
if (!parent::setup($element, $value, $group))
{
return false;
}
// Get the field families/options, a cache of ::getFamilies();
if ( empty(static::$families) ) {
$cache = JFactory::getCache();
static::$families = $cache->call( array( 'JFormFieldFamilies', 'getFamilies' ) );
}
foreach (array('formsource', 'layout', 'groupByFieldset') as $attributeName)
{
$this->__set($attributeName, $element[$attributeName]);
}
if ($this->value && is_string($this->value))
{
// Guess here is the JSON string from 'default' attribute
$this->value = json_decode($this->value, true);
}
if ( !$this->formsource && is_array($this->value) )
{
// Set the formsource the content of the static::$families
$this->formsource = '
<form>
<field name="family" type="list" label="Family" default="" value="Johnsons">
'. // a list of static::$families .'
</field>
<field name="girls" type="list" multiple="true" label="Girls" default="400">
'. // a list of static::$families[$family][$girls] .'
</field>
<field name="boys" type="list" multiple="true" label="Boys" default="Robert">
'. // a list of static::$families[$family][$boys] .'
</field>
</form>';
}
// these function are unchanged, perhaps I can remove from this file
protected function __get() {}
protected function getInput() {}
protected function getName($fieldName) {}
// this one is probably most important for me, changed
public function __set($name, $value)
{
switch ($name)
{
case 'formsource':
$this->formsource = (string) $value;
break;
case 'groupByFieldset':
if ($value !== null)
{
$value = (string) $value;
$this->groupByFieldset = !($value === 'false' || $value === 'off' || $value === '0');
}
break;
case 'layout':
$this->layout = (string) $value;
// Make sure the layout is not empty.
if (!$this->layout)
{
// HERE'S THE TRICKY PART
// $this->layout = 'templates.MYTEMPLATE.html.layouts.joomla.form.field.families';
$this->layout = 'joomla.form.field.families';
}
break;
default:
parent::__set($name, $value);
}
}
// a full array of families
static function getFamilies() {}
}
In my templates/MYTEMPLATE/html/layouts/joomla/form/field
folder I have families.php
, basically a copy of the subform/repeatable-table.php
. So far, if I use joomla.form.field.subform.repeatable-table
it's working fine, however, my layout file isn't working and I don't know how to handle this, it's beyond me.
Basically I see an empty <div class="controls">
inside my form.
Thanks for any inputs/suggestions.