Day: November 17, 2010

  • How is customer_logged_in/out handle added to Magento layout?

    I was wondering how Magento merge or handle into handle in layout.

    I was expecting something like

    <default>
        <update handle="customer_logged_in" if="logged_in"/>
        <update handle="customer_logged_out" if="logged_out"/>
    </default>
    

    Obviously, there is not such codes in layouts.

    I searched within Magento folder for keyword “customer_logged_in”, but only occurrences I could find in layout/customer.xml.

    So, how is customer_logged_in/out handle added to Magento layout?

    At last, I found

    class Mage_Customer_Model_Observer
    {
        public function beforeLoadLayout($observer)
        {
            $loggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
    
            $observer->getEvent()->getLayout()->getUpdate()
               ->addHandle('customer_logged_'.($loggedIn?'in':'out'));
        }
    }
    

    by tracing addHandle() in app/core/Mage/Core/Model/Layout/Update.php.

    I must say using observer adding layout handle makes very easy for other modules to detect customer login status.