Best Practise to Add Custom Tab in Product Details (backend)

Personally I would go for a layout/action approach using the addTab() provided by Mage_Adminhtml_Block_Widget_Tabs

So 2 main action are involved here:

  1. add XML layout changes
  2. create the tab class
  3. ( all this can be accomplish creating a new module, out of the scope here )

-- 1. Layout changes --

<?xml version="1.0"?>
<layout>
     <adminhtml_catalog_product_edit>
        <reference name="product_tabs">
            <block type="MODULENAME/adminhtml_catalog_product_edit_tab" name="custom_tab"/>
            <action method="addTab">
                <name>Custom Tab</name>
                <block>custom_tab</block>
            </action>
        </reference>
    </adminhtml_catalog_product_edit>
</layout>

-- 2. Tab Class --

<?php
class NAMESPACE_MODULENAME_Block_Adminhtml_Catalog_Product_Edit_Tab extends Mage_Adminhtml_Block_Widget
    implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
    public function canShowTab()
    {
        return true;
    }
    public function getTabLabel()
    {
        return $this->__('Custom Tab');
    }
    public function getTabTitle()
    {
        return $this->__('Custom Tab');
    }
    public function isHidden()
    {
        return false;
    }
    public function getTabUrl()
    {
        return $this->getUrl('*/*/customtab', array('_current' => true));
    }
    public function getTabClass()
    {
        return 'ajax';
    }
} 

Note:
There is very few documentation about backend development, I feel like Magento Devs. are kind of shy to share knowledge about this area (and that is why of the above question.)

Source:
This technique can be found here:
- http://www.webspeaks.in/2012/02/create-custom-tab-in-magento-product-addedit-page.html and also in the comments of this Inchoo article:
- http://inchoo.net/ecommerce/magento/how-to-add-custom-product-relations-in-magento/


Here is how I do it.

Create an observer for the event core_block_abstract_prepare_layout_after. Not sure if it's the best event though.

<adminhtml>
    ...
    <events>
       <core_block_abstract_prepare_layout_after>
            <observers>
                <[namespace]_[module]_product>
                    <type>singleton</type>
                    <class>[module]/adminhtml_observer</class>
                    <method>addProductTabBlock</method>
                </[namespace]_[module]_product>
            </observers>
       </core_block_abstract_prepare_layout_after>
    </events>
    ....
</adminhtml>

Then create the observer

class [Namespace]_[Module]_Model_Adminhtml_Observer {
    //this checks if the tab can be added. You don't want to add the tab when selecting the product type and attribute set or when selecting the configurable attributes.
    protected function _canAddTab($product){
        if ($product->getId()){
            return true;
        }
        if (!$product->getAttributeSetId()){
            return false;
        }
        $request = Mage::app()->getRequest();
        if ($request->getParam('type') == 'configurable'){
            if ($request->getParam('attributes')){
                return true;
            }
        }
        return false;
    }
    //the method that actually adds the tab
    public function addProductTabBlock($observer){
        $block = $observer->getEvent()->getBlock();
        $product = Mage::registry('product');
        //if on product tabs block and the tab can be added...
        if ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs && $this->_canAddTab($product)){
            //in case there is an ajax tab
            $block->addTab('some_identifier_here', array(
                'label' => Mage::helper('catalog')->__('Some Label here'),

                'url'   => Mage::helper('adminhtml')->getUrl('adminhtml/some_url/here', array('_current' => true)),
                'class' => 'ajax', 
            ));
            //in case it's a simple content tab
            $block->addTab('other_identifier_here', array(
                 'label'     => Mage::helper('catalog')->__('Label here'),
                'content'   => $this->getLayout()->createBlock('[module]/block_alias')->toHtml(),
            )); 
        }
        return $this;
    }
}

Just make sure you replace [namespace] and [module] with the values you have for your module.


Add the following code to your config.xml file

<blocks>
...
    <modulename>
        <class>Company_ModuleName_Block</class>
    </modulename>
    <adminhtml>
        <rewrite>
             <catalog_product_edit_tabs>Company_ModuleName_Block_Adminhtml_Tabs</catalog_product_edit_tabs>
         </rewrite>
     </adminhtml>
...
</blocks>

After this you should create a new file: Company/ModuleName/Block/Adminhtml/Tabs.php

<?php

class Company_ModuleName_Block_Adminhtml_Tabs extends Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs
{
    private $parent;

    protected function _prepareLayout()
    {
        //get all existing tabs
        $this->parent = parent::_prepareLayout();
        //add new tab
        $this->addTab('tabid', array(
                     'label'     => Mage::helper('catalog')->__('New Tab'),
                     'content'   => $this->getLayout()
             ->createBlock('modulename/adminhtml_tabs_tabid')->toHtml(),
        ));
        return $this->parent;
    }
}

Next, create a file: Company/ModuleName/Block/Adminhtml/Tabs/Tabid.php

<?php

class Company_ModuleName_Block_Adminhtml_Tabs_Tabid extends Mage_Adminhtml_Block_Widget
{
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('modulename/newtab.phtml');
    }
}

app/code/local/Mage/Adminhtml/Block/Catalog/Product/Edit/Tabs.php

) and add the following snippet to the function _prepareLayout()

$this->addTab('tabid', array(
              'label'     => Mage::helper('catalog')->__('New Tab'),
              'content'   => $this->_translateHtml($this->getLayout()
     ->createBlock('modulname/adminhtml_tabs_tabid')->toHtml()),
));

You can always create a local of any core file.