Magento – dynamically adding layout updates xml files

configurationevent-observerlayout

In an extension i am working on, i have this section in the module's config.xml

<config>
    <frontend>
        <layout>
            <updates>
                <packagename_modulename module="packagename_modulename">
                    <file>packagename_modulename</file>
                </packagename_modulename>
            </updates>
        </layout>
    </frontend>
</config>

This works correctly as expected but now i have another situation where i want to activate that layout update acc. to some conditions. I want to achieve the same end result but achieve it dynamically from php code. Is there some way to dynamically insert that node in the global config from either the controller or observer?

BTW there are some more config too that i want to add to that. It is used to rewrite some classes of magento blocks & models. But if i can understand how to insert to the global config programmatically, i think i will be able to add that config too in the same way.

EDIT

Just to clarify, i want to make my extension work exactly as if this config is present in the config.xml file, but i want to add it from php code instead of the config.xml.

For rewriting the blocks & models, i'm going like-

<config>
    <global>
        <blocks>
            <payment>
                <rewrite>
                    <form_cc>PackageName_ModuleName_Block_Form_Cc</form_cc>
                </rewrite>
            </payment>
        </blocks>
    </global>
</config>

UPDATE

So using Fabian's suggestion, i made some more progress. I didn't removed anything from the config.xml, i used Mage::app()->getConfig()->setNode('path_to_config', '') inside the observer controller_action_layout_load_before & it's correctly setting the configuration. This allowed me to undo all the models & blocks rewrites that i did from my module's config. Next from the router, i was able to undo the controller rewrite that was added by the module.

Now there's only one problem remaining here. The layout file once loaded is getting cached. The above solution works as long as the layout cache has been disabled from magento's admin. So is there any way to "not cache my particular extension"? This is only for a particular use case, so it would be ok if i can do it by editing a core file

Best Answer

To add a layout file:

Mage::app()->getLayout()->loadFile($file);

To add a handle:

Mage::app()->getLayout()->getUpdate()->addHandle('your_handle_name');

To change settings in the config:

Mage::getConfig()->setNode(
    'global/helpers/core/encryption_model',
    'Ikonoshirt_Pbkdf2_Model_Stub_EE'
);