Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - xml layouts, specify value for ifconfig?

Tags:

magento

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>
like image 481
Marlon Creative Avatar asked Apr 08 '11 13:04

Marlon Creative


1 Answers

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.

like image 117
Alan Storm Avatar answered Sep 29 '22 03:09

Alan Storm