For an extension I was recently working on, I wondered if there was a built-in way to link directly to a tab on a backend page. My research didn’t result in anything useful (read: my Google foo had failed me), so I dug into the core code to see where to start. I’ll share what I found out.
The Problem
In particular, I wanted to link to the “Manage Label / Options” tab on the “Edit Product Attribute Page”:
The Solution
Actually it’s possible with a URL parameter ?active_tab=$id
.
How To Find The Right Tab ID
Find the responsible tab container class. This is a child class of Mage_Adminhtml_Block_Widget_Tabs
, in my case, Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tabs
.
You’ll find calls to $this->addTab()
, usually in the methods _beforeToHtml()
, or _construct()
. The first parameter of addTab()
is the tab id:
$this->addTab('labels', array( 'label' => Mage::helper('catalog')->__('Manage Label / Options'), 'title' => Mage::helper('catalog')->__('Manage Label / Options'), 'content' => $this->getLayout()->createBlock('adminhtml/catalog_product_attribute_edit_tab_options')->toHtml(), ));
So, the URL is /admin/catalog_product_attribute/edit/attribute_id/123/?active_tab=labels
, generated with this code (within an adminhtml block):
$this->getUrl('adminhtml/catalog_product_attribute/edit', array('attribute_id' => 123, '_query' => array('active_tab' => 'labels'));
How it works
Let’s have a look at the responsible code:
Mage_Adminhtml_Block_Widget_Tabs::_beforeToHtml()
Sets the active tab based on URL parameter or session value. If setActiveTab()
is not called, the _activeTab
property defaults to the first added tab:
if ($activeTab = $this->getRequest()->getParam('active_tab')) { $this->setActiveTab($activeTab); } elseif ($activeTabId = Mage::getSingleton('admin/session')->getActiveTabId()) { $this->_setActiveTab($activeTabId); }
app/design/adminhtml/default/default/template/widget/tabs.phtml
Here, the result of getActiveTabId()
is passed to the varienTabs instantiation in JavaScript:
<script type="text/javascript"> <?php echo $this->getJsObjectName() ?> = new varienTabs( '<?php echo $this->getId() ?>', '<?php echo $this->getDestElementId() ?>', '<?php echo $this->getActiveTabId() ?>', <?php echo $this->getAllShadowTabs()?>); </script>
The varienTabs
constructor takes the active tab id as parameter and shows it. For the curious, the code is in js/mage/adminhtml/tabs.js