Generally for adding a new field to a magento admin form we need to add by method addField().For that we need to get prepareForm method and then need to override the block of form .But if we want to do it without overriding any block and controller simple by taking the blockType and fieldset name. adminhtml_block_html_before event for creating new custom field magento admin form by event observer.

So here I am going to show you how to add new field in magento admin form by event observer.
First of all I have to catch the event “adminhtml_block_html_before” which create the form object then to check whether the block is creating by form object is my block name or not then need to do all steps for adding new field inside that condition.

Here I have used two files one is config.xml and other one is my Observer.php file which describe how to add new field in magento admin form by event observer.

If you know little about magento module structure then you get it easily.Beacuse I going to describe about only those two files.Those are

1) config.xml :

This file will be at directory : app/code/local/NameSpace/ModuleName/etc/config.xml
here I have use codepool as local you can use the code pool as per your module declaration code pool like community or local.

.........
.........
<events>
<adminhtml_block_html_before>
    <observers>
        <event_column_append>
            <type>model</type>
            <class>NameSpace_ModuleName_Model_Observer</class>
            <method>appendMyNewCustomFiled</method>
        </event_column_append>
    </observers>
</adminhtml_block_html_before>
</events>
..........
..........

I have just shown only those parts which is neccessary for this article.The observer I want capture is “adminhtml_block_html_before” because it create the form objects where I can get the details about my desire form.Then after I have called the method “appendMyNewCustomFiled” which in the class “NameSpace_ModuleName_Model_Observer”.

So lets see what the method doing and where that is ?

2) Observer.php :

This file will be at directory : app/code/local/NameSpace/ModuleName/Model/Observer.php

<?php

    class NameSpace_ModuleName_Model_Observer{
        
        public function appendMyNewCustomFiled(Varien_Event_Observer $observer)
        {
            $block = $observer->getEvent()->getBlock();
            if (!isset($block)) {
                return $this;
            }
            if ($block->getType() == 'adminhtml/permissions_tab_roleinfo') {
                $form = $block->getForm();
                //create new custom fieldset 'website'
                 $fieldset = $form->addFieldset('website_field', array(
                        'legend' => 'Website Extras',
                        'class' => 'fieldset-wide'
                    )
                );
                //add new website field
                $fieldset->addField('website', 'text', array(
                    'name'      => 'website',
                    'label'     => Mage::helper('adminhtml')->__('Website'),
                    'title'     => Mage::helper('adminhtml')->__('Website'),
                    'disabled'  => false,
                ));
            }
        }
    }
?>

Here I have to create a new field called website in admin form .So I checked which block is creating the form element.So the block was “adminhtml/permissions_tab_roleinfo”.Through this block I have accessed the form element and did what I wanted.
Now I can show you another example which may clear you little more.

Example for above explanation:

config.xml :

This file will be at directory : app/code/local/JRB/Pricerules/etc/config.xml

    
    ........
    ........
    <events>
        <adminhtml_block_html_before> <!-- identifier of the event we want to catch -->
            <observers>
                <jrb_pricerules> <!-- identifier of the event handler -->
                    <type>model</type> <!-- class method call type; valid are model, object and singleton -->
                    <class>jrb_pricerules/observer</class> <!-- observers class alias -->
                    <method>addCustomFields</method>  <!-- observer's method to be called -->
                    <args></args> <!-- additional arguments passed to observer -->
                </jrb_pricerules>
            </observers>
        </adminhtml_block_html_before>
    </events>
    .........
    .........

Observer.php :

This file will be at directory : app/code/local/JRB/Pricerules/Model/Observer.php

<?php
class JRB_Pricerules_Model_Observer{
    public function addCustomFields(Varien_Event_Observer $observer){
        $action = Mage::app()->getFrontController()->getAction();
        $ruleId = $action->getRequest()->getParam('id');
        $acionName = $action->getFullActionName();
        if($acionName === 'adminhtml_pricerules_jrbpricerules_edit'){
            $block = $observer->getEvent()->getBlock();
            if (!isset($block)) {
                return $this;
            }
            if ($block->getType() == 'jrb_pricerules/adminhtml_jrbpricerules_edit_tab_form') {
                $form = $block->getForm();
                $fieldset = $form->getElement('jrbpricerules_form');
                $fieldset->addField('custom_field', 'text', array(
                    'name'      => 'custom_field',
                    'label'     => Mage::helper('adminhtml')->__('Custom Field'),
                    'title'     => Mage::helper('adminhtml')->__('Custom Field'),
                    'disabled'  => false,
                ));
            }
        }
    }
}
?>

In the above example I can simply explain only for config.xml and Observer.php file .That is I have a module called “JRB_Pricerules” .In that module I have a admin form having block element “adminhtml_jrbpricerules_edit_tab_form” So I wanted to add a new field witout editing the form but through observer that is to add new field in magento admin form by event observer.So IJust simply catch the event “adminhtml_block_html_before” and called the Observer method “addCustomFields”.In that method I check my form block type and add a ew form field.

add new field in magento admin form by event observer
Tagged on:                 

One thought on “add new field in magento admin form by event observer

  • March 26, 2015 at 4:37 am
    Permalink

    Excellent solution. thanks for the article. tested and #Bookmarked

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *