Magento 1

Programatically add a Layout Update File in Magento

We sometimes need logic to decide whether an update file should be loaded or not, and need to do this outside of a module’s config.xml area/layout/update node. Instead, we can use an observer and programatically decide whether to add the layout file or not.


<?xml version="1.0"?>

The Observer

Because the updates object extends a SimpleXML Object, we can add our file to it here

class Namespace_Module_Model_Observer
    public function loadXML(Varien_Event_Observer $observer)
        // Conditional to decide whether to load the XML file
        if(Mage::helper('mymodule')->isEnabled()) {
            $xml = $observer->getUpdates()
            /* @var $xml SimpleXMLElement */
            $xml->addAttribute('module', 'Namespace_Module');
            $xml->addChild('file', 'mymodule/layoutfile.xml');