0

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 .

2

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.

  • i didnt get your point. i have a fully functional banner module with me and in that I have already displayed two tabs. I have done coding for the first tab and it works fine now. Now when i Click the second tab, currently it is empty. I need to add a button there which will provide an image uploader input every time it get clicked. sorry i didnt use any observer before – Rajeev K Tomy Dec 27 '13 at 13:20
  • yes for that you have to use this observer event "core_block_abstract_prepare_layout_after" and call the phtml file via code – Keyul Shah Dec 27 '13 at 13:23
  • i have read your answer several time. But due to some spelling mistake, it doesn't convey the actual message that you are tried to provide. Can you please go through it and make appropriate changes? – Rajeev K Tomy Dec 27 '13 at 13:27
  • I have change this – Keyul Shah Dec 27 '13 at 13:32
1

Ok finally I found a way to add button in a particular tab. Here is the magic. What we are going to do is, adding a button as custom defined form field. A custom defined form field can be add into the tab file just like default form fields.

We need to add the custom form field first, in the tab file Example_Buttonadder_Block_Adminhtml_Buttonadder_Edit_Tab_Form

 <?php
   $fieldset->addType('add_button', 'Example_Buttonadder_Block_Adminhtml_Buttonadder_Edit_Tab_Field_Custom'); 

next we need to define our custom form field into our tab file

<?php
     $fieldset->addField('buttonadder_add_button', 'add_button', array(
         'title' => Mage::helper('buttonadder')->__('My Button Name'),
         'id' => 'buttonadder_id',
         'class' => 'buttonadder_class',
         'style' => 'color:white;height:50px',  //just an example
         'onclick' => 'addbutton.add(this)',
         'type' => 'button',                                       
    ));

Now we need to define our custom form field in the file Example/Buttonadder/Block/Adminhtml/Buttonadder/Edit/Tab/Field/Custom.php

<?php
class Example_Buttonadder_Block_Adminhtml_Buttonadder_Edit_Tab_Field_Custom extends Varien_Data_Form_Element_Abstract
{
    public function __construct($attributes=array())
    {
       parent::__construct($attributes);
    }

    public function getElementHtml()
    {               
        $value = $this->getTitle();
        $onclick=$this->getOnclick();
        $class=$this->getClass();
        $id=$this->getId();
        $style=$this->getStyle();
        $type=$this->getType();
        $html='<button id="'.$id.'" class="'.$class.'" style="'.$style.'" onclick="'.$onclick.'" type="'.$type.'" title="'.$value.'">'.$value.' </button>';
        $html .= '<p id="' . $this->getHtmlId() . '"'. $this->serialize($this->getHtmlAttributes()) .'>
                <script type="text/javascript">
                //<![CDATA[                        
                    var addbutton =  
                    {                                                      
                         add : function(obj) {  
                             alert("yeeha");
                                                                                                         },                            
                    };
                //]]>
                </script>
            </p>';             
    return $html;
    }
}

That's it. Your button will appear in the tab. You can define the action of button in that script file. You should keep two important thing in mind. Your class extension and the definition of the method getElementHtml() should not alter. Hope this will help every one who face this problem in future. Best of luck !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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