How to add JS/CSS from layout.xml only if multible conditions are met?
-
10-10-2020 - |
Question
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:
- add a helper for each file
- add a new (hidden?) field to config and at sections
save_before
set value depending of the other fields - 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?
Solution
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 ...
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange