Friday, 12 September 2014

Update handle on a certain condition programatically

Follow below steps in order to update a handle on a certain event-

1. Create a observer on a events

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>
                    <observer_unique_name>
                        <class><Parckagename>_<Modulename>_Model_Observer</class>
                        <method>controllerActionLayoutLoadBefore</method>
                    </observer_unique_name>
                </observers>
            </controller_action_layout_load_before>
        </events>
    </frontend>

</config>

2. Create Observer


<?php
    class <Parckagename>_<Modulename>_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(<write any condition> is true)
            {
                $layout->getUpdate()->addHandle(‘OUR_COOL_OBJECT_’.$id);
            }
            
        }
    }
That’s it!

No comments:

Post a Comment