I need to be able to add styling class to form fields parent wrappers when using the xml attribute method, with a new attribute cgClass (as in control-group) to achieve <div class="control-group myclass">
<field name="name" type="text" cgClass="myclass" />
The current method adds a class attrib to the field element using the xml attribute class, but that is useless for styling the parent container and children.
The desired output should be
<div class="control-group myclass">
<div class="control-label">
<label id="jform_params" for="jform_params">Label</label>
</div>
<div class="controls">
<input type="text" name="jform[params]" id="jform_params" value="">
</div>
</div>
The goal is to be able to group fields into columns
This screen capture shows 2 columns
I need to do this without using a layout override.
I can achieve the desired output when I edit \libraries\src\Form\FormField.php within the renderField() method and add the following condition, however that's not appropriate since it is overwritten on updates.
public function renderField($options = array())
{
// added condition
if( $this->getAttribute('cgClass') ) {
$options['class'] = $this->getAttribute('cgClass');
}
}
How can this be done?
Additional Information
The core layout file layouts/joomla/form/renderfield.php is constructed to do the task as desired.
<?php defined('JPATH_BASE') or die;
extract($displayData);
if (!empty($options['showonEnabled']))
{
JHtml::_('jquery.framework');
JHtml::_('script', 'jui/cms.js', array('version' => 'auto', 'relative' => true));
}
$class = empty($options['class']) ? '' : ' ' . $options['class'];
$rel = empty($options['rel']) ? '' : ' ' . $options['rel'];
?>
<div class="control-group<?php echo $class; ?>"<?php echo $rel; ?>>
<?php if (empty($options['hiddenLabel'])) : ?>
<div class="control-label"><?php echo $label; ?></div>
<?php endif; ?>
<div class="controls"><?php echo $input; ?></div>
</div>
However the renderField() method in libraries/src/FormFormField.php does not send a value to the $options['class'] variable. As if someone forgot to complete the condition.
public function renderField($options = array())
{
if ($this->hidden)
{
return $this->getInput();
}
if (!isset($options['class']))
{
$options['class'] = '';
}
$options['rel'] = '';
if (empty($options['hiddenLabel']) && $this->getAttribute('hiddenLabel'))
{
$options['hiddenLabel'] = true;
}
if ($this->showon)
{
$options['rel'] = ' data-showon=\'' .
json_encode(FormHelper::parseShowOnConditions($this->showon, $this->formControl, $this->group)) . '\'';
$options['showonEnabled'] = true;
}
$data = array(
'input' => $this->getInput(),
'label' => $this->getLabel(),
'options' => $options,
);
return $this->getRenderer($this->renderLayout)->render($data);
}