Magento - xml layouts, specify value for ifconfig?
Asked Answered
M

2

6

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>
Mandelbaum answered 8/4, 2011 at 13:58 Comment(0)
L
22

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.

Lab answered 8/4, 2011 at 22:54 Comment(3)
i just want to check in layout.xml to check the value of a field, which is two possible value -> top or -> below. So tho check if top is selected then show my products top of product_list either below of product_list in catalog page.. Thanx..Mezzosoprano
Can we call helper method instead of ifConfig where our custom logic return true or false.Mezzosoprano
Hi Alan, can we use this approach to set the template layout of a certain Magento page by using our own module's system variable? I've opened a new question for it. Would be glad if you could chekc it out here #11593840Tout
P
10

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>
Patrick answered 8/4, 2011 at 23:26 Comment(5)
Yeah, this is how I'm currently doing it, which does work, but just would've been nice to make use of the default module disabler, and save me having to add this to every layout.Mandelbaum
How to check the value of that field.. => ifvalue="...."... i want to check the value of a field and then decide to show my grid on top or below of product_list...Thanks...Mezzosoprano
@ime, There is no ifvalue currently in the layout system, only a ifconfig which checks for a non-zero value. You could use that with a pair of yes/no settings, one for 'above product list' and one for 'below product list'. (That would allow the user to have both on or both off which you might not want to do.) The only other way is to write the PHP yourself.Patrick
Hmmm, thats ok but it if write code logic in a helper method and which return true or false value, so can i call that helper method from layout xml like ifconfig. ? which return true or false....Mezzosoprano
See Alan's answer for an example of using a helper. If you have further questions then open a new question.Patrick

© 2022 - 2024 — McMap. All rights reserved.