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.

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

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

Observer.php :

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

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:                 

4 thoughts 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
  • November 28, 2019 at 12:46 pm
    Permalink

    seems very helpful, but i can’t see the images

    Reply
    • May 5, 2020 at 7:26 am
      Permalink

      Hi Khaled,
      site has some of issue, I am working on this issue and will update you once fixed.

      Reply
  • October 22, 2020 at 12:08 am
    Permalink

    Thanks so much for this! I actually used this observer to edit existing fields, I needed to update a core form to make the email field required.

    For anybody wondering how, just change the observer code like so:

     

    Reply

Leave a Reply

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