Skip to content

Instantly share code, notes, and snippets.

@neilime
Created February 5, 2014 13:32
Show Gist options
  • Save neilime/8823655 to your computer and use it in GitHub Desktop.
Save neilime/8823655 to your computer and use it in GitHub Desktop.
Override TwbBundle\Form\View\Helper\TwbBundleFormElement
<?php
namespace Test\Form\View\Helper;
class TestTwbBundleFormElement extends \TwbBundle\Form\View\Helper\TwbBundleFormElement
{
/**
* Render an element
* @param \Zend\Form\ElementInterface $oElement
* @return string
*/
public function render(\Zend\Form\ElementInterface $oElement)
{
//Add form-controll class
$sElementType = $oElement->getAttribute('type');
if (!in_array($sElementType, array('file', 'checkbox', 'radio', 'submit', 'multi_checkbox', 'static', 'button'))) {
if ($sElementClass = $oElement->getAttribute('class')) {
if (!preg_match('/(\s|^)form-control(\s|$)/', $sElementClass)) $oElement->setAttribute('class', trim($sElementClass . ' form-control'));
} else $oElement->setAttribute('class', 'form-control');
}
switch(true){
case '\TwbBundle\Form\Element\StaticElement':
$sMarkup = $this->getView()->formStatic()->render($oElement);
break;
case '\Test\Form\Element\MyElement':
$sMarkup = $this->getView()->myElement()->render($oElement);
break;
default :
$sMarkup = parent::render($oElement);
}
//Addon prepend
if ($aAddOnPrepend = $oElement->getOption('add-on-prepend')) $sMarkup = $this->renderAddOn($aAddOnPrepend) . $sMarkup;
//Addon append
if ($aAddOnAppend = $oElement->getOption('add-on-append')) $sMarkup .= $this->renderAddOn($aAddOnAppend);
if ($aAddOnAppend || $aAddOnPrepend) {
$sSpecialClass = '';
//Input size
if ($sElementClass = $oElement->getAttribute('class')) {
if (preg_match('/(\s|^)input-lg(\s|$)/', $sElementClass)) $sSpecialClass .= ' input-group-lg';
elseif (preg_match('/(\s|^)input-sm(\s|$)/', $sElementClass)) $sSpecialClass .= ' input-group-sm';
}
return sprintf(
self::$inputGroupFormat,
trim($sSpecialClass),
$sMarkup
);
}
return $sMarkup;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment