Skip to content
This repository has been archived by the owner on Jul 10, 2020. It is now read-only.

Add option for different label rendering for radio buttons #102

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/TwbBundle/Form/View/Helper/TwbBundleFormRadio.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ protected function renderOptions(\Zend\Form\Element\MultiCheckbox $oElement, arr
//Option label
$sLabel = isset($aOptionspec['label']) ? $aOptionspec['label'] : '';
if ($sLabel) {
$aLabelAttributes = $aGlobalLabelAttributes;
if (isset($aOptionspec['label_attributes'])) {
$aLabelAttributes = isset($aLabelAttributes) ? array_merge($aLabelAttributes, $aOptionspec['label_attributes']) : $aOptionspec['label_attributes'];
$aLabelAttributes = array();
if (true === $oElement->getOption('global-label-attributes')) {
$aLabelAttributes = isset($aOptionspec['label_attributes']) ? $aOptionspec['label_attributes'] : array();
} elseif (isset($aOptionspec['label_attributes'])) {
$aLabelAttributes = isset($aGlobalLabelAttributes) ? array_merge($aGlobalLabelAttributes, $aOptionspec['label_attributes']) : $aOptionspec['label_attributes'];
} else {
$aLabelAttributes = $aGlobalLabelAttributes;
}
if (null !== ($oTranslator = $this->getTranslator())) {
$sLabel = $oTranslator->translate($sLabel, $this->getTranslatorTextDomain());
Expand Down
87 changes: 53 additions & 34 deletions src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,49 @@ protected function renderLabel(\Zend\Form\ElementInterface $oElement) {
}
return $sLabel;
}



/**
* Parse label attributes
* @param \Zend\Form\ElementInterface $oElement
* @param array $aLabelAttributes
*/
protected function parseLabelAttributes(\Zend\Form\ElementInterface $oElement, $aLabelAttributes)
{
//Validation state
if ($oElement->getOption('validation-state') || count($oElement->getMessages())) {
if (empty($aLabelAttributes['class'])) {
$aLabelAttributes['class'] = 'control-label';
} elseif (!preg_match('/(\s|^)control-label(\s|$)/', $aLabelAttributes['class'])) {
$aLabelAttributes['class'] = trim($aLabelAttributes['class'] . ' control-label');
}
}

switch ($oElement->getOption('twb-layout')) {
//Hide label for "inline" layout
case \TwbBundle\Form\View\Helper\TwbBundleForm::LAYOUT_INLINE:
if ($oElement->getAttribute('type') !== 'checkbox') {
if (empty($aLabelAttributes['class'])) {
$aLabelAttributes['class'] = 'sr-only';
} elseif (!preg_match('/(\s|^)sr-only(\s|$)/', $aLabelAttributes['class'])) {
$aLabelAttributes['class'] = trim($aLabelAttributes['class'] . ' sr-only');
}
}
break;

case \TwbBundle\Form\View\Helper\TwbBundleForm::LAYOUT_HORIZONTAL:
if (empty($aLabelAttributes['class'])) {
$aLabelAttributes['class'] = 'control-label';
} else {
if (!preg_match('/(\s|^)control-label(\s|$)/', $aLabelAttributes['class'])) {
$aLabelAttributes['class'] = trim($aLabelAttributes['class'] . ' control-label');
}
}
break;
}
return $aLabelAttributes;
}

/**
* Render element
Expand All @@ -139,44 +182,20 @@ protected function renderElement(\Zend\Form\ElementInterface $oElement) {
$sLabelContent = '';
} else {
$aLabelAttributes = $oElement->getLabelAttributes() ? : $this->labelAttributes;

//Validation state
if ($oElement->getOption('validation-state') || $oElement->getMessages()) {
if (empty($aLabelAttributes['class'])) {
$aLabelAttributes['class'] = 'control-label';
} elseif (!preg_match('/(\s|^)control-label(\s|$)/', $aLabelAttributes['class'])) {
$aLabelAttributes['class'] = trim($aLabelAttributes['class'] . ' control-label');
}
}

$oLabelHelper = $this->getLabelHelper();
switch ($sLayout) {
//Hide label for "inline" layout
case \TwbBundle\Form\View\Helper\TwbBundleForm::LAYOUT_INLINE:
if ($sElementType !== 'checkbox') {
if (empty($aLabelAttributes['class'])) {
$aLabelAttributes['class'] = 'sr-only';
} elseif (!preg_match('/(\s|^)sr-only(\s|$)/', $aLabelAttributes['class'])) {
$aLabelAttributes['class'] = trim($aLabelAttributes['class'] . ' sr-only');
}
}
break;

case \TwbBundle\Form\View\Helper\TwbBundleForm::LAYOUT_HORIZONTAL:
if (empty($aLabelAttributes['class'])) {
$aLabelAttributes['class'] = 'control-label';
} else {
if (!preg_match('/(\s|^)control-label(\s|$)/', $aLabelAttributes['class'])) {
$aLabelAttributes['class'] = trim($aLabelAttributes['class'] . ' control-label');
}
}
break;
}
//Parse radio label attributes
$aLabelAttributes = $this->parseLabelAttributes($oElement, $aLabelAttributes);
if ($aLabelAttributes) {
$oElement->setLabelAttributes($aLabelAttributes);
}
if (false === $oElement->getOption('global-label-attributes')) {
//Parse global label attributes
$aGlobalLabelAttributes = $this->parseLabelAttributes($oElement, array());
} else {
$aGlobalLabelAttributes = $aLabelAttributes;
}

$sLabelOpen = $oLabelHelper->openTag($oElement->getAttribute('id') ? $oElement : $aLabelAttributes);
$oLabelHelper = $this->getLabelHelper();
$sLabelOpen = $oLabelHelper->openTag($oElement->getAttribute('id') ? $oElement : $aGlobalLabelAttributes);
$sLabelClose = $oLabelHelper->closeTag();

// Allow label html escape desable
Expand Down