I'm trying to add a class (the same) to each label in a group of radio buttons.
This is my code:
$linkedin_share = new Zend_Form_Element_Radio('linkedin_share', array('escape' => false));
$linkedin_share->setDecorators(array('ViewHelper','Errors', array('Label', array('class' => 'TEST'))))
->addMultiOption('none', $this->getView()->translate('None'))
->addMultiOption('icon', '<img src="'.$this->getView()->baseUrl().'/images/admin/icons/social_media_share/linkedin.png'.'"/>')
->addMultiOption('counter', '<img src="'.$this->getView()->baseUrl().'/images/admin/icons/social_media_share/linkedin_share.jpg'.'"/>')
->setSeparator('')
->setAttrib('class', 'item_small_checkbox');
And this is my output:
<label for="linkedin_share-none">
<label for="linkedin_share-icon">
<label for="linkedin_share-counter">
This is my desired output:
<label for="linkedin_share-none" class="share_label_class">
<label for="linkedin_share-icon" class="share_label_class">
<label for="linkedin_share-counter" class="share_label_class">
The stupid thing is that it works for all my other form elements so far. I tried a million combinations and searched my ass off, but no matter what I try I can't add class to the label.
Ideas, solutions, suggestions are all very welcome! Thanks in advance!
It took me two hours to find the answer and it's infuriatingly simple! Add this line to your declaration:-
->setAttrib('label_class', 'share_label_class')
Zend/View/Helper/FormRadio.php line 79 - 95 gave me the clue.
$label_attribs = array();
foreach ($attribs as $key => $val) {
$tmp = false;
$keyLen = strlen($key);
if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) {
$tmp = substr($key, 6);
} elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) {
$tmp = substr($key, 5);
}
if ($tmp) {
// make sure first char is lowercase
$tmp[0] = strtolower($tmp[0]);
$label_attribs[$tmp] = $val;
unset($attribs[$key]);
}
}
This works on my system, I hope it does for you too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With