6
votes

I'm sure I saw before somewhere, specifying a value for xml ifconfig statements (as default is just boolean). Anyways, disabling modules in the admin doesn't actually work (only disables module output). But you can add an ifconfig to your layout file, so for example, to set a template only if a module is disabled is the following:

<action method="setTemplate" ifconfig="advanced/modules_disable_output/Myname_Mymodule">
    <template>mytemplate.phtml</template>
</action>

So how could you invert this, so the template is set only if the module is enabled? Something like:

<action method="setTemplate" ifconfig="advanced/modules_disable_output/Myname_Mymodule" value="0">
    <template>mytemplate.phtml</template>
</action>
2

2 Answers

22
votes

This fits in well with something (self-link) I've been working on.

You can't do exactly what you want without a class rewrite to change the behavior of ifconfig. Here's the code that implements the ifconfig feature.

File: app/code/core/Mage/Core/Model/Layout.php
protected function _generateAction($node, $parent)
{
    if (isset($node['ifconfig']) && ($configPath = (string)$node['ifconfig'])) {
        if (!Mage::getStoreConfigFlag($configPath)) {
            return $this;
        }
    }

If the presence of an ifconfig is detected and the config value returns true, the action method will not be called. You could rewrite _generateAction and implement your own conditional, but then the standard burdens of maintaining a rewrite fall you on.

A better approach would be to use a helper method in your action paramater. Something like this

<action method="setTemplate">
    <template helper="mymodule/myhelper/switchTemplateIf"/>
</action>

will call setTemplate with the results of a call to

Mage::helper('mymodule/myhelper')->switchTemplateIf();

Implement your custom logic in switchTemplateIf that either keeps the template or changes it and you'll be good to go.

10
votes

You could create a separate enable setting using nothing but your module's system.xml.

<config>
    <sections>
        <advanced>
            <groups>
                <YOURMODULE>
                    <fields>
                        <enable>
                            <label>YOUR MODULE</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_enabledisable</source_model>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </enable>
                    </fields>
                </YOURMODULE>
            </groups>
        </advanced>
    </sections>
</config>

Then use the new setting in the layout file:

<action method="setTemplate" ifconfig="advanced/YOURMODULE/enable">
    <template>mytemplate.phtml</template>
</action>