How to add custom tab and field in Magento backend

Lots of developers use rewrite technique to add custom tabs or fields into form of Magento backend. However, it can bring us problems if more than one module override the same block. So, using events technique may be a very helpful way. In this post, I will show you how to add custom fields or tab into Edit form or Grid page in Magento.

We will use this event "core_block_abstract_prepare_layout_after". We also use event "eventPrefix_save_after" to save new data.

Step 1:

Define the event in config.xml.


  
    
      
        model
        custommodule /observer
        magebuzz_add_custom_tab 
      
    
  
  
    
      
        custommodule/observer
        magebuzz_save_custiom_value 
      
    
  

Step 2:

Create file app/code/local/Magebuzz/Custommodule/Model/Observer.php

Here is a sample to add custom tab into Edit Form

// Add Custom Tab In Edit Form
public function magebuzz_add_custom_tab($observer){
    $block = $observer->getEvent()->getBlock();    
      if ($block->getType() == 'BLOCK_NAME')
      {
          $block->addTab('custom_tab', array(
          'label' => Mage::helper('custommodule')->__('Custom Tab'),
          'title' => Mage::helper('custommodule')->__('Custom Tab'),
          'url'   => $block->getUrl('custommodule/adminhtml_custom/index', array('_current' => true)),  
'class' =>'ajax',     ));
    } 
} 

// Add Custom field to Block form Edit //
public function magebuzz_add_custom_tab($observer){
    $block = $observer->getEvent()->getBlock();    
if ($block->getType() == 'BLOCK_NAME') {               
       $form = $block->getForm();      
        $fieldset = $form->getElement('base_fieldset');            
        $fieldset->addField('user_status', 'select', array(
        'name'      => 'user_status',
        'label'     => Mage::helper('custommodule')->__('Customer Status'),
        'title'     => Mage::helper('custommodule')->__('Customer Status'),
        'values'     =>array('1'=>'Enable','2'=>'Disable'),      
        ));   
} 

// Add Customer Field to Block Grid //
public function magebuzz_add_custom_tab($observer){
    $block = $observer->getEvent()->getBlock();    
      if ($block->getType() == 'BLOCK_NAME'){      
        $block->addColumnAfter('user_status', array(
        'header'    => 'User Status',
        'type'      => 'text',
  'index'     => 'user_status',
        'filter'    => false,
        'sort'      => false
        ), 'website_id');
      }
}

NOTE:  “BLOCK NAME” is replaced by the block you want to add custom tab or custom field
Example:  adminhtml/customer_customer_edit_tabs or adminhtml/customer_grid

Step 3: create save function in Observer.php

public function magebuzz_save_custiom_value($observer) {
    $data = Mage::app()->getRequest()->getPost();   
// please writecontentshere
    }

P/S :
This tutorial will inject a custom tab into your Magento product edit page. Wish you success!

One thought on “How to add custom tab and field in Magento backend”

1 Response to How to add custom tab and field in Magento backend

  • Altaf
    Altaf on June 10, 2014 at 9:33 am said:

    Thanks for your nice tutorial,
    i followed your tutorial, it helped me in adding tabs to cms edit page, but i can't add fields to the tab using this technique, one function can't be defined twice and calling two or more function through a single event also gives error, how could i do this??? will you please help me out? Thanks again

Comments