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!

Debug Translation in magento

When you are dealing with multiple translations then some times you want to know the location, from where this translation is coming, so there are steps to debug it:-

1. Go to  app/code/core/Mage/Core/Model/Translate.php

2. Find function translate()

and then write the below code before "return $result":-

$logModule = str_pad($module, 25, "-", STR_PAD_RIGHT);
$logInString = str_pad($text, 150, "-", STR_PAD_RIGHT);
$logOutString = str_pad($result, 200, " ", STR_PAD_RIGHT);
Mage::log($logModule . '>' . $logInString . '>' . $logOutString, null, 'translate.log');

Now check translate.log file under /var/log.

After debugging rollback the whole code in core file "Translate.php"