Magento 2: How to check if module is installed or enabled?
We had a requirement to check if one of our modules is enabled or not as two of our modules were sharing the same attribute code. We wanted to make sure that we don’t try to create an attribute again if the other module already installed or enabled.
We have done this many times in Magento 1 by using the following code snippet -:
Mage::helper('core')->isModuleEnabled('vendor_modulename');
So we were wondering there must be a simple way of doing this in Magento 2 as well and yes we were not wrong. It is not a single line of code as we had in Magento 1 but there is nothing in Magento 2 which can be implemented in Magneto 2 ???? You have to add dependency to get access to the functions.
Anyways let’s crack on with the code to find out if the module exists or enabled in Magento 2
<?php
class Custom
{
/**
* @var \Magento\Framework\Module\Manager
*/
protected $_moduleManager;
/**
* @param \Magento\Framework\Module\Manager $moduleManager
*/
public function __construct(
\Magento\Framework\Module\Manager $moduleManager
) {
$this->_moduleManager = $moduleManager;
}
/**
* Returns if module exists or not
*
* @return bool
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function isModuleEnabled()
{
return $this->_moduleManager->isEnabled('Scommerce_CatalogUrl');
}
}
That’s it, Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.