From 78be984c5db3f20002c4f44dd048850caf001825 Mon Sep 17 00:00:00 2001 From: BreyndotEchse Date: Wed, 3 Sep 2014 11:58:12 +0200 Subject: [PATCH 1/4] Update TwbBundleFormRadio.php Don't use element label options if `global-label-attributes` option is FALSE --- src/TwbBundle/Form/View/Helper/TwbBundleFormRadio.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleFormRadio.php b/src/TwbBundle/Form/View/Helper/TwbBundleFormRadio.php index 3283a42..2b381f3 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleFormRadio.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleFormRadio.php @@ -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()); From 4356931444a6827dc6a95030e77fd552682a7197 Mon Sep 17 00:00:00 2001 From: BreyndotEchse Date: Wed, 3 Sep 2014 12:00:03 +0200 Subject: [PATCH 2/4] Update TwbBundleFormRow.php Don't use element label options if `global-label-attributes` option is not FALSE --- src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php b/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php index 36acb47..d5b8b20 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php @@ -138,7 +138,11 @@ protected function renderElement(\Zend\Form\ElementInterface $oElement) { if ($oElement instanceof \Zend\Form\Element\Button) { $sLabelContent = ''; } else { - $aLabelAttributes = $oElement->getLabelAttributes() ? : $this->labelAttributes; + if (false !== $oElement->getOption('global-label-attributes')) { + $aLabelAttributes = $oElement->getLabelAttributes() ? : $this->labelAttributes; + } else { + $aLabelAttributes = array(); + } //Validation state if ($oElement->getOption('validation-state') || $oElement->getMessages()) { From 355203fa29cfc4b4057b5b385b5a74d298d5354b Mon Sep 17 00:00:00 2001 From: BreyndotEchse Date: Wed, 3 Sep 2014 13:22:47 +0200 Subject: [PATCH 3/4] Update TwbBundleFormRow.php Revert changes and change attribute parsing for proper rendering --- .../Form/View/Helper/TwbBundleFormRow.php | 91 +++++++++++-------- 1 file changed, 52 insertions(+), 39 deletions(-) diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php b/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php index d5b8b20..27e8bf5 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php @@ -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 @@ -138,49 +181,19 @@ protected function renderElement(\Zend\Form\ElementInterface $oElement) { if ($oElement instanceof \Zend\Form\Element\Button) { $sLabelContent = ''; } else { - if (false !== $oElement->getOption('global-label-attributes')) { - $aLabelAttributes = $oElement->getLabelAttributes() ? : $this->labelAttributes; + $aLabelAttributes = $oElement->getLabelAttributes() ? : $this->labelAttributes; + //Parse radio label attributes + $aLabelAttributes = $this->parseLabelAttributes($oElement, $aLabelAttributes); + $oElement->setLabelAttributes($aLabelAttributes); + if (false === $oElement->getOption('global-label-attributes')) { + //Parse global label attributes + $aGlobalLabelAttributes = $this->parseLabelAttributes($oElement, array()); } else { - $aLabelAttributes = array(); - } - - //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'); - } + $aGlobalLabelAttributes = $aLabelAttributes; } $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; - } - if ($aLabelAttributes) { - $oElement->setLabelAttributes($aLabelAttributes); - } - - $sLabelOpen = $oLabelHelper->openTag($oElement->getAttribute('id') ? $oElement : $aLabelAttributes); + $sLabelOpen = $oLabelHelper->openTag($oElement->getAttribute('id') ? $oElement : $aGlobalLabelAttributes); $sLabelClose = $oLabelHelper->closeTag(); // Allow label html escape desable From b5d36e45a7baf12e3fdb1cd1e634dfb0847f27eb Mon Sep 17 00:00:00 2001 From: BreyndotEchse Date: Fri, 5 Sep 2014 10:58:26 +0200 Subject: [PATCH 4/4] Update TwbBundleFormRow.php Don't call \Zend\Form\LabelAwareInterface::setLabelAttributes, if attributes are empty --- src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php b/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php index 27e8bf5..b0a412b 100644 --- a/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php +++ b/src/TwbBundle/Form/View/Helper/TwbBundleFormRow.php @@ -184,7 +184,9 @@ protected function renderElement(\Zend\Form\ElementInterface $oElement) { $aLabelAttributes = $oElement->getLabelAttributes() ? : $this->labelAttributes; //Parse radio label attributes $aLabelAttributes = $this->parseLabelAttributes($oElement, $aLabelAttributes); - $oElement->setLabelAttributes($aLabelAttributes); + if ($aLabelAttributes) { + $oElement->setLabelAttributes($aLabelAttributes); + } if (false === $oElement->getOption('global-label-attributes')) { //Parse global label attributes $aGlobalLabelAttributes = $this->parseLabelAttributes($oElement, array());