0

I'm wondering if you can use Magento's ifconfig in layout xml for string comparrison? Something like the below:

<action method="addCss" ifconfig="mdloptioncolor/genral_theme_setting/style_option==3"><stylesheet>'css/skyblue-color.css</stylesheet></action>

I've tried this but it does not work, is there a way of doing this?

Basically, instead of checking for a Boolean condition set by the user from the admin panel with a yes/no drop down, can check a for a string comparison in a drop down with multiple options

1

This is checked for in class Mage_Core_Model_Layout and method _generateAction(). At the top of this action you will see:

if (isset($node['ifconfig']) && ($configPath = (string)$node['ifconfig'])) {
    if (!Mage::getStoreConfigFlag($configPath)) {
        return $this;
    }
}

So the value of ifconfig is cast to (string) and then fed to Mage::getStoreConfigFlag(). This method is as follows:

public static function getStoreConfigFlag($path, $store = null)
{
    $flag = strtolower(self::getStoreConfig($path, $store));
    if (!empty($flag) && 'false' !== $flag) {
        return true;
    } else {
        return false;
    }
}

So the string is just passed to the standard Mage::getStoreConfig() method and if the returned value is not empty and is not the string false then true is returned. In all other cases false is returned in which case further processing of the action method is halted.

So the long and short of it is no, you can't do any kind of string comparison inside ifconfig however you could rewrite and extend the getStoreConfigFlag() to consider the returned value from getStoreConfig(), for instance the following would add string comparison:

<?php
class Namespace_Module_Model_Layout extends Mage_Core_Model_Layout
{
    private $_configvalue;

    protected function _generateAction($node, $parent)
    {
        if (isset($node['ifconfig']) && ($path = (string) $node['ifconfig'])):
            if ($path = $this->_getCompareValue($path)):
                $value = Mage::getStoreConfig($path);
                if (!empty($value) && $value == $this->_configvalue):
                    return true;
                endif;

                return false;
            endif;
        endif;

        return parent::_generateAction($node, $parent);
    }

    private function _getCompareValue($path)
    {
        $this->_configvalue = false;
        if (strpos($path, '=') !== false):
            $array = explode('=', $path);
            $value = end($array);
            if (!empty($value)):
                $this->_configvalue = $value;

                return reset($array);
            endif;
        endif;

        return false;
    }

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.