Jawaban:
Secara pribadi saya akan pergi untuk pendekatan tata letak / tindakan menggunakan yang addTab()
disediakan olehMage_Adminhtml_Block_Widget_Tabs
Jadi 2 tindakan utama terlibat di sini:
- 1. Perubahan tata letak -
<?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. Kelas Tab -
<?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';
}
}
Catatan:
Ada sangat sedikit dokumentasi tentang pengembangan backend, saya merasa seperti Magento Devs. agak malu untuk berbagi pengetahuan tentang bidang ini (dan itulah sebabnya pertanyaan di atas.)
Sumber:
Teknik ini dapat ditemukan di sini:
- http://www.webspeaks.in/2012/02/create-custom-tab-in-magento-product-addedit-page.html
dan juga di komentar artikel Inchoo ini :
- http://inchoo.net/ecommerce/magento/how-to-add-custom-product-relations-in-magento/
Inilah cara saya melakukannya.
Buat pengamat untuk acara tersebut core_block_abstract_prepare_layout_after
. Tidak yakin apakah ini acara terbaik.
<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>
Lalu buat pengamat
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;
}
}
Pastikan Anda mengganti [namespace]
dan [module]
dengan nilai yang Anda miliki untuk modul Anda.
Tambahkan kode berikut ke config.xml
file Anda
<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>
Setelah ini, Anda harus membuat file baru: 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;
}
}
Selanjutnya, buat 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 / Katalog / Produk / Edit / Tabs.php
) dan tambahkan potongan berikut ke fungsi _prepareLayout()
$this->addTab('tabid', array(
'label' => Mage::helper('catalog')->__('New Tab'),
'content' => $this->_translateHtml($this->getLayout()
->createBlock('modulname/adminhtml_tabs_tabid')->toHtml()),
));
Anda selalu dapat membuat file inti lokal apa pun.