I want to change the css class of a admin field's label. As I have found out there is not setter method such as setLabelHtml, I am not able to change the class. At last I override the Varien_Data_Form_Element_Abstract class as suggested by 'AlanStorm' in this post. I changed the 'getLabelHtml' and place the class attribute there and it worked. Is there any other better way to do it ?
Magento – How to change attributes of a magento admin form field label
adminhtmlblocksforms
Best Answer
At last I have found two answers for my question. I wanted to change css class of elements inside the
fieldset
of the form.1. First solution
All of the fieldset created inside a form object is using a
renderer
ofVarien_Data_Form_Element_Renderer_Interface
for rendering the html as a last step (if one is present).The default renderer for a fieldset inside a form is
Mage_Adminhtml_Block_Widget_Form_Renderer_Fieldset_Element
which is extendingMage_Adminhtml_Block_Template
. Hence it has a template object attached to it. The template for our renderer iswidget/form/renderer/fieldset/element.phtml
.The steps to change the default behaviour is as follows:
First of all created a renderer class.
Then I created a template by copying the
element.phtml
fromapp/design/adminhtml/default/default/template/widget/form/element/renderer/fieldset
totemplates/widget/form/fieldset/renderer/
inside my module folder. The content of that file is:As you can see I have used a
getLabelClass()
function which I have set on the renderer object in the_prepareForm()
function in the widget form class.The widget form class I have used is:
Now I have my custom label for admin fields. I hope it will help you and also invite more better answers on this question (Hence not closing it).
2. Second solution
Create a directory
etc
insideapp/code/local
. Then insideapp/code/local/etc
create the whole directory structure needed forVarien_Data_Form_Element_Abstract
class which is the parent class for all form elements. So there directory structure you need to create insideapp/code/local/etc/
isVarien/Data/Form/Element
and then copyAbstract.php
fromlib/Varien/Data/Form/Element/
to the directory you created. After that change thegetLabelHtml()
method as follows:As you can see we have placed a
getLabelClass()
on the current object. So you have to usesetLabelClass($labelClass)
by iterating throughVarien_Form
object or thefieldset
object inside you have put the field.