4

I am wondering, is there any way to add a custom fieldset to an admin form, without overwriting Form container class of that admin-form.

I will explain it little bit. Suppose we need to add a new fieldset in CMS_Block. We can achieve this by rewrite _preapreForm() method of class Mage_Adminhtml_Block_Cms_Block_Edit_Form. Here we need to do somewhat this in order to achieve our goal

class Namespace_Modulename_Block_Adminhtml_Cms_Block_Edit_Form extends Mage_Adminhtml_Block_Cms_Block_Edit_Form{
    
    protected function _prepareForm()
    {
        parent::_prepareForm();

        /*
           add new fieldset and fields here
        */
    }
}

However I don't like to do rewrite of a core class (Since Its not a good practice). Is there any other approach exist to achieve the same result. I just want to add a new field sets with a custom module on an admin form with two conditions

  1. Without touching core files

  2. Without doing rewrite

2
  • What is about layout update? Or using event for specific controller/action render?
    – Amasty
    Commented Aug 13, 2014 at 10:27
  • @Amasty layout update wont work. Please review the answer that marius made Commented Aug 13, 2014 at 10:28

1 Answer 1

3

You can try to observe the event adminhtml_block_html_before.
but that gets dispatched for every block that is a child of Mage_Adminhtml_Block_Template.
So you will need to to a check first to see if the block matches your need.
something like this:

public function addFields($observer) {
    $block = $observer->getEvent()->getBlock();
    if ($block instanceof Mage_Adminhtml_Block_Cms_Block_Edit_Form) {
         //do your magic here
    }
}
11
  • observers are better than rewrite. But still its not a clean method. Coz almost every admin blocks are child of Mage_Admin_Html_Template. Is there any way to achieve this through layout ? But when I deeply thinking about it, it gave me a feeling that, my block would include outside of the form. @marius Commented Aug 13, 2014 at 10:24
  • @programmer_rkt I don't think there is a way to do it through layout files. The block in question does not appear in the layout files. It is instantiated via the parent block.
    – Marius
    Commented Aug 13, 2014 at 10:26
  • and secondly I have a friendly question to you. Which method you normally adopt to do this job? Commented Aug 13, 2014 at 10:26
  • So as per your suggestion, there is probably these two ways exist to do this right ? Commented Aug 13, 2014 at 10:29
  • i suggest that this as event every time for admin block,if you add controller full action name as ccondtion then it it be better.....$action = Mage::app()->getFrontController()->getAction(); $action->getFullActionName() if($action->getFullActionName()='adminhtml_cms_block_edit){ // marius code }
    – Amit Bera
    Commented Aug 13, 2014 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.