Magento – How to add Custom Model Grid Into Custom Tab in Product Edit page in admin panel in magento

admin-panelgridmagento-1.8producttabs

I have created a Custom Grid. I have one table with grid generated. I
successfully added the tab in my custom grid.

But I am a little bit confused on how to add Custom Tab in "Product Edit Page"
and in Custom Tab how to display Custom Model Grid.

Please guide me.

Best Answer

So my understanding is you want to add a grid to the product edit/ new product page.

First thing you will need to do is add a adminhtml XML file to you modules config.xml if it does not allready have one.

in etc/config.xml

 <adminhtml>
        <layout>
            <updates>
                <theextensionlab_producttab>
                    <file>theextensionlab/producttab.xml</file>
                </theextensionlab_producttab>
            </updates>
        </layout>
    </adminhtml>

The in that layout file add to the reference "product_tabs" we need to call the addTab function as pass in our tabs name and the Block:

<all_products>
    <reference name="product_tabs">
        <action method="addTab"><name>product_sitemap_grid</name><block>theextensionlab_producttab/adminhtml_catalog_product_edit_tab_sitemap</block></action>
    </reference>
</all_products>

<adminhtml_catalog_product_new>
    <update handle="all_products"/>
</adminhtml_catalog_product_new>

<adminhtml_catalog_product_edit>
    <update handle="all_products"/>
</adminhtml_catalog_product_edit>

Note I don't know if there is a handle for new and edit products so I just created a custom handle and created it for both. If you wanted to just have this on all simple products you could have used adminhtml_catalog_product_simple

For our new block to show it needs to implement Mage_Adminhtml_Block_Widget_Tab_Interface and have the functions getTabLabel,getTabTitle,canShowTab,isHidden

For simplicty I have just used the sitemap collection from Magento and copied some of that default code, this is similar to the default grid apart from it implements Mage_Adminhtml_Block_Widget_Tab_Interface.

<?php class TheExtensionLab_ProductTab_Block_Adminhtml_Catalog_Product_Edit_Tab_Sitemap
    extends Mage_Adminhtml_Block_Widget_Grid implements Mage_Adminhtml_Block_Widget_Tab_Interface
{

    //Our tab needs the 4 functions below in order to show on the product page
    public function getTabLabel(){
        return Mage::helper('catalog')->__('Sitemap');
    }

    public function getTabTitle(){
        return Mage::helper('catalog')->__('Sitemap');
    }

public function canShowTab(){
    //Check we have selected a type for our new product
    //If not lets not show the tab
    if($this->getRequest()->getParam( 'type' ) != null
        || $this->getRequest()->getParam( 'id' ) != null){
        return true;
    }
    return false;
}

    public function isHidden(){
        return false;
    }

    public function __construct()
    {
        parent::__construct();
        $this->setId('sitemapGrid');
        $this->setDefaultSort('sitemap_id');
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('sitemap/sitemap')->getCollection();
        /* @var $collection Mage_Sitemap_Model_Mysql4_Sitemap_Collection */
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        $this->addColumn('sitemap_id', array(
            'header'    => Mage::helper('sitemap')->__('ID'),
            'width'     => '50px',
            'index'     => 'sitemap_id'
        ));

        $this->addColumn('sitemap_filename', array(
            'header'    => Mage::helper('sitemap')->__('Filename'),
            'index'     => 'sitemap_filename'
        ));

        $this->addColumn('sitemap_path', array(
            'header'    => Mage::helper('sitemap')->__('Path'),
            'index'     => 'sitemap_path'
        ));

        $this->addColumn('link', array(
            'header'    => Mage::helper('sitemap')->__('Link for Google'),
            'index'     => 'concat(sitemap_path, sitemap_filename)',
            'renderer'  => 'adminhtml/sitemap_grid_renderer_link',
        ));

        $this->addColumn('sitemap_time', array(
            'header'    => Mage::helper('sitemap')->__('Last Time Generated'),
            'width'     => '150px',
            'index'     => 'sitemap_time',
            'type'      => 'datetime',
        ));


        if (!Mage::app()->isSingleStoreMode()) {
            $this->addColumn('store_id', array(
                'header'    => Mage::helper('sitemap')->__('Store View'),
                'index'     => 'store_id',
                'type'      => 'store',
            ));
        }

        $this->addColumn('action', array(
            'header'   => Mage::helper('sitemap')->__('Action'),
            'filter'   => false,
            'sortable' => false,
            'width'    => '100',
            'renderer' => 'adminhtml/sitemap_grid_renderer_action'
        ));

        return parent::_prepareColumns();
    }

}

You will then see in this case the sitemap in a grid on your product edit page or in your case your custom grid.

Related Topic