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>
Related
I am trying to create my first Magento module which would allow an attribute (yes/no) on a product page to alter the styling of the grouped products options on a product view page.
I'm probably starting a little too deep here but this is my understanding of how to do this:
Create an attribute and assign to default attribute set (Alternative Group View alt_group_view).
Create an alternative grouped.phtml file in /template/catalog/product/view/type - groupedAlt.phtml
Create a basic module structure that initialises my module. I should really have the module create the attribute, but I havent a clue about that yet!!!
Then this is where I get stuck. Essentially I need to right the logic within the module file that looks for the attribute on the product - if its set I then need to code that tells the layout block presumably something that extends this:
<PRODUCT_TYPE_grouped translate="label" module="catalog">
<reference name="product.info">
<block type="catalog/product_view_type_grouped" name="product.info.grouped" as="product_type_data" template="catalog/product/view/type/grouped.phtml">
</reference>
</PRODUCT_TYPE_grouped translate="label" module="catalog">
Hopefully that makes sense to someone?
I'm not sure that I can realisticly acheive this as Im just starting with module development although I have quite a lot of knowledge with the frontend and admin areas of Magento.
Any advice would be much appreciated.
Regards,
Steve
You should handle this through an observer. The event to observe is :
controller_action_layout_generate_blocks_after
1°) In your module, in etc/config.xml, add your event handler :
<frontend>
<events>
<controller_action_layout_generate_blocks_after>
<observers>
<yourmodule_generate_blocks_after>
<type>singleton</type>
<class>mymodule/observer</class>
<method>generateBlocksAfter</method>
</yourmodule_generate_blocks_after>
</observers>
</controller_action_layout_generate_blocks_after>
</events>
</frontend>
Then create a Model named Observer.php in your Model directory (MyCompany/MyModule/Model/Observer.php)
In this model, add your generateBlocksAfter() method like this :
public function generateBlocksAfter($event)
{
$controller = $event->getAction();
//limit to the product view page
if($controller->getFullActionName() != 'catalog_product_view')
{
return;
}
$layout = $controller->getLayout();
$myblock = $layout->getBlock('product.info.grouped');
$_product = Mage::registry('current_product');
if ($_product->getAltGroupView()) {
$myblock->setTemplate('catalog/product/view/type/groupedalt.phtml');
}
}
And here you are.
I would like to rename the URL path of the shopping cart from checkout/cart to checkout/adverts.
I have tried the following two methods:
Catalog > URL Rewrite Management: Custom
I have also tried doing it using a custom module, with the following in my config.xml:
<global>
<rewrite>
<mynamespace_mymodule_checkout_cart>
<from><![CDATA[#^/checkout/cart#]]></from>
<to>/checkout/adverts</to>
</mynamespace_mymodule_checkout_cart>
</rewrite>
</global>
Both methods have gone to the correct URL path, but shown a 404 Error page - is there something else I need to do? Magento ver. 1.5.0.1
What happen if you add a empty controller to your custom module
<?php
require_once('Mage/Checkout/controllers/CartController.php');
class Mynamespace_Mymodule_CartController extends Mage_Checkout_CartController
{
}
As R.S said you have to extend CartController.php in your module
<?php
require_once('Mage/Checkout/controllers/CartController.php');
class Mynamespace_Mymodule_CartController extends Mage_Checkout_CartController
{
}
?>
in it copy the indexAction() of Mage/Checkout/controllers/CartController.php
Also in your local.xml write
<mymodule_cart_adverts>
// copy the xml code inside <checkout_cart_index> ... </checkout_cart_index> here
</mymodule_cart_adverts>
I havent tried it but it should work
struggling getting access to an admin block ive created.
Ive created a module...it has many elements, all working. Ive got header includes added to certain admin pages no problem, using my adminhtml layout update xml file.
The issue seems to be it cant access/see my block...so muct be referencing wrong, even though ive been following the 'module creator' extension files.
Another silly issue i think, been at this too long! :)
First the code:
Mworkz/MyModuleName/Block/Adminhtml/MyBlock.php
class Mworkz_MyModuleName_Block_Adminhtml_MyModuleName extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
var_dump('WE ARE IN THE ADMIN BLOCK!');exit;
$this->_controller = 'adminhtml_mymodulename';
$this->_blockGroup = 'mymodulename';
$this->_headerText = Mage::helper('mymodulename')->__('Item Manager');
$this->_addButtonLabel = Mage::helper('mymodulename')->__('Add Item');
parent::__construct();
}
}
My layout xml (this file works, and is referenced right, as my admin header includes work)
Should point out i have a custom tab and controller...all working.
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodulename_adminhtml_mymodulename_index>
<reference name="head">
<action method="addJs"><script>Mworkz/MyModuleName.js</script></action>
</reference>
<reference name="content">
<block type="mymodulename/adminhtml_mymodulename" name="mymodulename" ></block>
</reference>
</mymodulename_adminhtml_mymodulename_index>
</layout>
I expect to see the var_dump stmt ive inserted....but it doesnt display.
Thanks in advance...
file naming! Simple caps issue...
My block file was called '...Adminhtml/MyModuleName.php',
My block identifier inside the file was '...Adminhtml_Mymodulename {'
Another set of working code snippets for adminhtml block users i suppose!
Thanks
I am creating a module that will have its own page at mysite.com/memymodule/index/index
I want to add functionality from a template file from at templates/me/template.phtml
I have an index controller like this:
<?php
class me_mymodule_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$this->loadLayout();
$this->renderLayout();
}
}
?>
I have a layout update mymodule.xml that looks like this:
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_index_index>
<reference name="content">
<block type="core/template" name="me.mymodule" template="me/template.phtml" />
</reference>
</mymodule_index_index>
</layout>
The frontName of the module is mymodule
When the page renders the content block is completely empty and the content of template.phtml is completely ignored.
Help much appreciated :-)
Try this Code within your Controller's Action -
var_dump(Mage::getSingleton('core/layout')->getUpdate()->getHandles());
exit("Your Layout Path is ".__LINE__." in ".__FILE__);
This code tells you about the Tag which you need to create within Layout.xml.
Also check Config.xml that the Layout Update Section is correctly defined or not.
Hope it'll be beneficial for you.
THANKS
I am building a custom magento module and i try to add a custom css file to my block. I wrote :
<?php
class Wise_InteractiveSlider_Block_Slider extends Mage_Core_Block_Template
{
protected function _prepareLayout()
{
$this->getLayout()->getBlock('head')->addCss('css/mycompany/mymodule/stylesheet.css');
return parent::_prepareLayout();
}
}
but it doesn't work, my css file is not loaded, any idea?
Thank you.
My alternative solution was to add this in my xml layout :
<default>
<reference name="head">
<action method="addCss"><stylesheet>css/interactiveslider.css</stylesheet></action>
</reference>
</default>
Thank you for your help
All the CSS & Images are normally available in the "skin" folder. It should be:-
"skin" folder
-> Package Name (like "base" or "default")
-> Theme Name (like "modern" or "mycompany")
-> "css" folder
-> "mymodule" folder
-> "stylesheet.css" file
So I suppose that you have been following this above-mentioned basic structure, which is considered as one of the best practices.
Coming back to your question, I suppose that you have mentioned the correct block class in your module's layout file "layout.xml". So the above code should be, according to the above folder structure:-
<?php
class Wise_InteractiveSlider_Block_Slider extends Mage_Core_Block_Template
{
protected function _prepareLayout()
{
$this->getLayout()->getBlock('head')->addCss('css/mymodule/stylesheet.css');
return parent::_prepareLayout();
}
}
Lastly, please make sure that you have uploaded your CSS file "stylesheet.css" in the correct folder.
Hope it helps.
You can only use the _prepareLayout() method if the block is defined in the layout XML. If you 'inline' the block inside a CMS page via the {{block type... method, the layout is already prepared by the time the block is rendered