Magento Admin – How to Add a Button for a Particular Tab

adminadminhtmlmagento-1magento-1.7

I tried a lot and searched a lot. But couldn't find a way to add a button in a particular tab in the admin section.
when examined the core files,I have found out that the following code is somehow using for adding button in a particular tab. Here is my second tab defining file

<?php
     class Karaokeshop_Banner_Block_Adminhtml_Banner_Edit_Tab_Image extends
     Mage_Adminhtml_Block_Widget_Form
     {
         protected function _prepareForm()
         {
             $form = new Varien_Data_Form();
             $this->setForm($form);
             $fieldset = $form->addFieldset('banner_image', 
              array('legend'=>Mage::helper('banner')->__('Banner Image')));

             $this->setChild('add_banner_img',
             $this->getLayout()->createBlock('adminhtml/widget_button')
            ->setData(array(
                'label'  => Mage::helper('banner')->__('Add Banner Image'),
                'id'     => 'add_banner_img',
                'name'   => 'add_banner_img',
                'element_name' => 'add_banner_img',                  
                'class'  => 'add' , 
                'onclick'=> 'bannerImage.addNewImage()'
            ))
        ); 
         return parent::_prepareLayout();                                                      
    }  
    public function getBannerImgButtonHtml()
    {
        return $this->getChildHtml('add_banner_img');
    }    
}

But it does not add any button in my tab. How can I add a button in a particular tab? Note that i need to add this button along with the title of the tab .

Best Answer

Hope you know the how to work with observer in magento.

you have to create observer for that for this and dynamically add tab in your admin product view.

so first thing I will give explain in detail first you have to create your custom module for that

you have to create a two observer

Observer code which you have to write in config.xml file

-----
<adminhtml>
    <events>
        <core_block_abstract_prepare_layout_after>
            <observers>
                <namespace_productupload_injectTabs>
                    <type>singleton</type>
                    <class>productupload/observer_product</class>
                    <method>injectTabs</method>
                </namespace_productupload_injectTabs>
            </observers>
        </core_block_abstract_prepare_layout_after>
    </events>
</adminhtml>
-----

1) core_block_abstract_prepare_layout_after

when this event will call this method setTab() called. which I have mention below.

public function setTab(Varien_Event_Observer $observer)
{
    $block = $observer->getEvent()->getBlock();

    if (Mage::getStoreConfig('productupload/general/enabled',Mage::app()->getStore()) && $block instanceof Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs) {
        if ($this->_getRequest()->getActionName() == 'edit' || $this->_getRequest()->getParam('type')) {
            $block->addTab('custom-product-tab-01', array(
                'label'     => 'Upload Product Files',
                'content'   => $block->getLayout()->createBlock('adminhtml/template', 'custom-tab-content', array('template' => 'productupload/content.phtml'))->toHtml()   
            ));
        }
    }
}

you have to create the phtml file. it will call each time when you open your product open as edit or new mode.

app\design\adminhtml\default\default\template\productupload\content.phtml

2) catalog_product_save_after

in this observer you have to write the code when you save your product. because Magento will not provide the file attribute. so we have to create the save logic via code.