Every HTTP request in Magento results with a few layout handles which can be used to customize layout of desired pages. If you ever tried to dump layout handles in Magento, you would see many of them. These handles are calculated differently, based on different variables.
Depending on whether the user is logged in or not, Magento uses customer_logged_in and customer_logged_out handles. Different stores have also different layout updates (e.g. STORE_default, STORE_cars, STORE_fashion…).
Themes have their own layout handles, e.g. THEME_frontend_default_default, THEME_frontend_enterprise_default.
While we were working on a website that uses “Shop by Brand” extension, client asked us if we could change layout for one brand page. In order to provide that functionality we had to extend this extension in a way that every brand page has it’s own layout update handle.
You might noticed that Magento uses this functionality to create unique layout handles for different categories (e.g. CATEGORY_96, CATEGORY_35) and products (e.g. PRODUCT_22735, PRODUCT_225).
Here I’ll show you how you can create object-specific layout handle functionality for your custom modules. Handle will be composed of string concatenated to object’s id (like OUR_COOL_OBJECT_535, OUR_COOL_OBJECT_863).
1. Create Observer
In order to add handle to Magento’s layout update object before it’s too late, we have to observe controller_action_layout_load_before event.
<config>
<frontend>
<events>
<controller_action_layout_load_before>
<observers>
<inchoo_controller_action_layout_load_before>
<class>inchoo_layouthandle/observer</class>
<method>controllerActionLayoutLoadBefore</method>
</inchoo_controller_action_layout_load_before>
</observers>
</controller_action_layout_load_before>
</events>
</frontend>
</config>
2. Add Handle
Now we have to calculate layout update handle and add it to the layout update object.
<?php
class Inchoo_LayoutHandle_Model_Observer
{
public function controllerActionLayoutLoadBefore(Varien_Event_Observer $observer)
{
/** @var $layout Mage_Core_Model_Layout */
$layout = $observer->getEvent()->getLayout();
$id = Mage::app()->getRequest()->getParam('id');
/* or */
if($ourCoolObject = Mage::registry('our_cool_object'))
{
$id = $ourCoolObject->getId();
}
$layout->getUpdate()->addHandle('OUR_COOL_OBJECT_'.$id);
}
}
That’s it!
Hope this helps! 🙂