Adminhtml Scope – Use Website/Default Checkbox for Mage_Adminhtml_Block_Widget_Form Field

adminformadminhtmlscope

I have a form in the admin section that is dealing with a custom entity. This entity has a main table and then a table to store options on a store level. I am wondering if it is possible to use the same check-boxes on my form as are in the config or product edit page when the scope is changed.

My form extends Mage_Adminhtml_Block_Widget_Form and I add the fields as follows.

$fieldSet->addField(
        'field_code',
        'text',
        array(
            'label'    => Mage::helper('myhelper')->__('Field Label'),
            'class'    => '',
            'required' => false,
            'name'     => 'field_code',
        )
    );

I have already added the scope switcher via layout.xml and save the entity values for the scope in a separate table, which all works well.

But it would be great if the user did not have to enter these values if they wanted to use the default scope entry.

NOTE: I have seen some people use after_element_html before to add the elements but I thought there may be a core function that would do this.

Best Answer

Not sure if this a solution to your problem, but it can be a hint in the right direction.
You can specify a custom element rendered in any one of your form tabs like this (the example is from the catalog module):

protected function _prepareLayout()
{
    Varien_Data_Form::setElementRenderer(
        $this->getLayout()->createBlock('adminhtml/widget_form_renderer_element')
    );
    Varien_Data_Form::setFieldsetRenderer(
        $this->getLayout()->createBlock('adminhtml/widget_form_renderer_fieldset')
    );
    Varien_Data_Form::setFieldsetElementRenderer(
        $this->getLayout()->createBlock('adminhtml/catalog_form_renderer_fieldset_element')
    );
}

The last method sets the element renderer block. You can try to use that but I think you will get some errors because that is destined for EAV entities.
You can create a block that extends Mage_Adminhtml_Block_Catalog_Form_Renderer_Fieldset_Element and add a custom template to it similar to catalog/form/renderer/fieldset/element.phtml where you can add your checkboxes or anything else.

Related Topic