1

I have three fields in admin config section that have to be checked. If at least one of them is set to true the files should load.

layout.xml

<reference name="head">
    <action method="addItem">
        <type>skin_js</type>
        <name>path/to/file1.js</name>
    </action>
    <action method="addItem">
        <type>skin_js</type>
        <name>path/to/file2.js</name>
    </action>
    ...
</reference>

Ideas:

  1. add a helper for each file
  2. add a new (hidden?) field to config and at sections save_before set value depending of the other fields
  3. Extended ifconfig extension is already is installed, but no idea how to set up condtions

At the moment I prefer 2nd one ...

Any other ideas? Any suggestions?

1
  • Use ifconfig like this tag <action method="addItem" ifconfig="my_config_section/advanced/load_js"> <type> skin_js </type> <name>js/my_module/my_module.js</name> </action> Jun 30, 2017 at 14:29

1 Answer 1

0

Solved it this way for now:

Layout.xml

<action method="addItem" ifconfig="[section]/[group]/enabled">
    <type>skin_js</type>
    <name>path/to/file1.js</name>
</action>

config.xml

<adminhtml>
    <events>
        <admin_system_config_changed_section_[section]>
            <observers>
                <module_enabledisable>
                    <class>[module]/observer</class>
                    <method>setConfig</method>
                </module_enabledisable>
            </observers>
        </admin_system_config_changed_section_[section]>
    </events>
</adminhtml>

Observer.php

public function setModuleEnabledDisabled(Varien_Event_Observer $observer)
{
    if (!$website = $observer->getWebsite()) {
        $scope = 'default';
        $scopeId = 0;
    } elseif (!$store = $observer->getStore()) {
        $scope = 'websites';
        $scopeId = Mage::app()->getWebsite($website)->getId();
    } else {
        $scope = 'stores';
        $scopeId = Mage::app()->getStore($store)->getId();
    }

    if ($scope == 'websites') {
        $storeId = Mage::app()->getWebsite($website)->getDefaultStore()->getId();
    } else {
        $storeId = $scopeId;
    }

    $path = '[module]/[section]/';
    $value = Mage::getStoreConfigFlag($path . 'to_cart', $storeId)
          || Mage::getStoreConfigFlag($path . 'to_compare', $storeId)
          || Mage::getStoreConfigFlag($path . 'to_wishlist', $storeId);

    try {
        Mage::getConfig()->saveConfig($path . 'enabled', (int) $value, $scope, $scopeId);
    } catch (Exception $e) {
        Mage::log($e->getMessage());
    }
}

If you have any better Ideas ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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