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

  • 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 '13 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. – Mridul Aggarwal May 9 '13 at 13:16
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'
);
| improve this answer | |
  • 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? – Mridul Aggarwal Apr 30 '13 at 19:13
  • I use the event controller_action_layout_load_before – Fabian Blechschmidt May 9 '13 at 11:26
  • 1
    In EE 1.13 loadFile complains of missing parameter (requires file path) – philwinkle Jan 31 '14 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.

| improve this answer | |
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.

| improve this answer | |
  • 1
    That is not an answer. Disabling cache is just wrong. – doc_id May 26 '15 at 4:45
  • I understand that, but it worked. – Mridul Aggarwal May 26 '15 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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