Magento 2: Check if a module is installed, enabled or active

This article shows how we can check if a module is installed / enabled or active in Magento 2.

Both Dependency Injection and Object Manager ways are shown below.

Using Dependency Injection (DI)

Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Framework\Module\Manager class in the constructor of my module’s block class.

app/code/Chapagain/HelloWorld/Block/HelloWorld.php


<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{	
	protected $_moduleManager;
		
	public function __construct(
		\Magento\Backend\Block\Template\Context $context,		
		\Magento\Framework\Module\Manager $moduleManager,
		array $data = []
	)
	{	
		$this->_moduleManager = $moduleManager;
		parent::__construct($context, $data);
	}
	
	/**
     * Whether a module is enabled in the configuration or not
     *
     * @param string $moduleName Fully-qualified module name
     * @return boolean
     */
	public function isModuleEnabled($moduleName)
	{
		return $this->_moduleManager->isEnabled($moduleName);
	}
	
	/**
     * Whether a module output is permitted by the configuration or not
     *
     * @param string $moduleName Fully-qualified module name
     * @return boolean
     */
    public function isOutputEnabled($moduleName)
    {
		return $this->_moduleManager->isOutputEnabled($moduleName);
	}
}
?>

Now, we use these functions in our template (.phtml) file.


var_dump($block->isModuleEnabled('YourNamespace_YourModule'));
var_dump($block->isOutputEnabled('YourNamespace_YourModule'));

var_dump($block->isModuleEnabled('Magento_Catalog'));
var_dump($block->isOutputEnabled('Magento_Catalog'));

Using Object Manager


$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();		

$moduleManager = $objectManager->get('\Magento\Framework\Module\Manager');

var_dump($moduleManager->isEnabled('YourNamespace_YourModule'));
var_dump($moduleManager->isOutputEnabled('YourNamespace_YourModule'));

var_dump($moduleManager->isEnabled('Magento_Catalog'));
var_dump($moduleManager->isOutputEnabled('Magento_Catalog'));

Hope this helps. Thanks.