I have a small piece of code in a template file that开发者_运维问答 I ONLY want to run if a certain module is installed. I found the below code, which you can use to find if a module is active, but I want to know if a module is installed.
$modules = Mage::getConfig()->getNode('modules')->children();
$modulesArray = (array)$modules;
if($modulesArray['Mage_Paypal']->is('active')) {
echo "Paypal module is active.";
} else {
echo "Paypal module is not active.";
}
I'm thinking I could maybe get a list of names of all the modules that are installed, and then use
if (stristr($modulelist, 'Name_Extension'))
to show my code only if the referenced extension is installed.
Anyone any ideas how to do that? Or any better solutions?
There's a core helper for that:
Mage::helper('core')->isModuleEnabled('MyCompany_MyModule');
It's in Mage_Core_Helper_Abstract
.
There's also a isModuleOutputEnabled()
method to check if output of module is disabled in System -> Configuration -> Advanced -> Disable Modules Output.
Try this:
$modules = Mage::getConfig()->getNode('modules')->children();
$modulesArray = (array)$modules;
if(isset($modulesArray['Mage_Paypal'])) {
echo "Paypal module exists.";
} else {
echo "Paypal module doesn't exist.";
}
Another way to find if a module is installed but disabled is with:
if (Mage::getStoreConfigFlag('advanced/modules_disable_output/Mage_Paypal')) {
echo "Paypal module is installed";
}
Edit
Have just realised a version of this - using the little known ifconfig
- could show a block only if another module is disabled. eg.
<layout>
<default>
<reference name="content">
<block ifconfig="advanced/modules_disable_output/Mage_Paypal" type="core/template" template="sorry/this/is/unavailable.phtml" />
</reference>
</default>
</layout>
You can check if a module exists on installation using this snippet Mage::getConfig()->getNode('modules/Mage_Paypal')
returns FALSE if does not exist
in the declaration of your module, try adding a depends
element, like on this page.
This will probably raise an exception, which you could handle with a try/catch block.
In any class, model, controller or even PHTML you can call and it will work.
Data.php
class Company_Module_Helper_Data extends Mage_Core_Helper_Abstract
{
const XML_PATH_GEN_ENABLE = 'Section/Group/Field';
public function isEnable()
{
return Mage::getStoreConfigFlag(XML_PATH_GEN_ENABLE,Mage::app()->getStore()->getId());
}
}
You can call using below code Mage::helper('module_name')->isEnable()
Your config file is like
config.xml
<global>
<helpers>
<module_name>
<class>Company_Module_Helper</class>
</module_name>
</helpers>
// Another Necessary Code for this extension
</global>
精彩评论