3

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

2
  • Can you give more info on "activate that layout update acc. to some conditions" and the kinds of changes you want to make?
    – benmarks
    May 9, 2013 at 11:54
  • Basically my end target is something along the lines of if(/*site viewed in mobile*/) /*don't load my extension*/ else /*load my extension*/. But since magento is caching on so many levels, i had to go through all the other ways. Right now i've managed to revert all the things my module adds to global config except for the layout file. The layout file is just like any other in "base/default/layout". It's added via the config.xml. Now even if i revert the config, magento shows the cached version of the site, which has the layout modified. May 9, 2013 at 13:16

3 Answers 3

10

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'
);
3
  • tried putting in both controller & controller_action_layout_generate_xml_before event but doesn't seem to work. cache is also disabled. any other idea? Apr 30, 2013 at 19:13
  • I use the event controller_action_layout_load_before May 9, 2013 at 11:26
  • 1
    In EE 1.13 loadFile complains of missing parameter (requires file path)
    – philwinkle
    Jan 31, 2014 at 22:57
2

Whereas your logic can be achieved via layout XML, there are two clear options: implement a responsive design (may not work based on your needs), or use a separate subdomain with its own theme settings. The conditional approach which you are currently using will require you to add your condition to many cache keys, and then you're in the same boat as before.

1

The solution i finally used is to undo all the models & blocks rewrites i had in module's config.xml in the controller_action_layout_load_before observer & after that go to magento admin & disable the Block HTML & Layout cache.

2
  • 1
    That is not an answer. Disabling cache is just wrong.
    – doc_id
    May 26, 2015 at 4:45
  • I understand that, but it worked. May 26, 2015 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.