From 25607bc3fb8fd97bac2bfcbc8c96316a50b3feff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matus=20M=C3=A1rk?= Date: Fri, 7 Sep 2018 17:45:07 +0200 Subject: [PATCH] Update FormElement.php There is many JS libraries using input group and add on attributes. I extended your code, so that it can be rendered. --- .../Form/View/Helper/FormElement.php | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/TwbsHelper/Form/View/Helper/FormElement.php b/src/TwbsHelper/Form/View/Helper/FormElement.php index ae96b75c1..62e2fd672 100644 --- a/src/TwbsHelper/Form/View/Helper/FormElement.php +++ b/src/TwbsHelper/Form/View/Helper/FormElement.php @@ -24,10 +24,10 @@ class FormElement extends ZendFormElementViewHelper implements TranslatorAwareInterface { // @var string - protected static $addonFormat = '<%s class="%s">%s'; + protected static $addonFormat = '<%s class="%s" %s>%s'; // @var string - protected static $inputGroupFormat = '
%s
'; + protected static $inputGroupFormat = '
%s
'; // Translator (optional) // @var Translator @@ -114,7 +114,7 @@ public function render(ElementInterface $oElement) // Addon append if ($aAddOnAppend = $oElement->getOption('add-on-append')) { - $sMarkup .= $this->renderAddOn($aAddOnAppend); + $sMarkup .= $this->renderAddOn($aAddOnAppend, 'append'); } if ($aAddOnAppend || $aAddOnPrepend) { @@ -128,11 +128,34 @@ public function render(ElementInterface $oElement) $sSpecialClass .= ' input-group-sm'; } } + + + $sSpecialAttributes = ''; + + // Input group attributes + if (is_array($oElement->getOption('input-group'))){ + $aInputGroup = $oElement->getOption('input-group'); + + // Input group class + if(! empty($aInputGroup['class'])){ + $sSpecialClass .= ' ' . $aInputGroup['class']; + } + + // Input group other attributes + if(is_array($aInputGroup['attributes'])){ + foreach ($aInputGroup['attributes'] as $name => $value){ + $sSpecialAttributes .= $name . '="' . $value . '" '; + } + $sSpecialAttributes = trim($sSpecialAttributes); + } + + } // Add a sprintf directive for correct render of errors return $sMarkup = sprintf( static::$inputGroupFormat, trim($sSpecialClass), + $sSpecialAttributes, $sMarkup . "%s" ); } @@ -222,8 +245,17 @@ protected function renderAddOn($aAddOnOptions, string $sPosition = 'prepend') $sAddonClass .= ' input-group-addon'; } } + + $sAttributes = ''; + + if(! empty($aAddOnOptions['attributes'])){ + foreach($aAddOnOptions['attributes'] as $name => $value){ + $sAttributes .= $name .'="' . $value . '" '; + } + $sAttributes = rtrim($sAttributes); + } - return sprintf(static::$addonFormat, $sAddonTagName, trim($sAddonClass), $sMarkup, $sAddonTagName); + return sprintf(static::$addonFormat, $sAddonTagName, trim($sAddonClass), $sAttributes, $sMarkup, $sAddonTagName); }