2

In magento 1 disable module and it's output programmatically

protected function _disableModule($moduleName) {
    // Disable the module itself
    $nodePath = "modules/$moduleName/active";
    if (Mage::helper('core/data')->isModuleEnabled($moduleName)) {
        Mage::getConfig()->setNode($nodePath, 'false', true);
    }

    // Disable its output as well (which was already loaded)
    $outputPath = "advanced/modules_disable_output/$moduleName";
    if (!Mage::getStoreConfig($outputPath)) {
        Mage::app()->getStore()->setConfig($outputPath, true);
    }
}

How can I do same in magento 2?

7

To disable a module itself, you need to use Magento\Framework\Module\Status class:

$status = $this->objectManager->create('Magento\Framework\Module\Status');
$status->setIsEnabled(false,[$moduleName]);

To disable the module output, you need to use Magento\Config\Model\ResourceModel\Config via the factory Magento\Config\Model\ConfigFactory:

$outputPath = "advanced/modules_disable_output/$moduleName";
$config = $this->objectManager->create('Magento\Config\Model\ResourceModel\Config');
$config->saveConfig($outputPath,true,'default',0);

In the line, default corresponds to the scope and 0 to the scope id. Those values will have to be changed depending on your needs.

N.B. : this code is provided as a quick example, try to avoid using the object manager directly and use dependency injection to use those classes

  • Thanks for your help. It's not working saveConfig have 4 argument 'saveConfig($path, $value, $scope, $scopeId)' you have mentioned only 2. It gives error argument missing. – Prashant Valanda May 3 '16 at 13:45
  • @PrashantValanda see my updated code. – Raphael at Digital Pianism May 3 '16 at 13:49
  • $this->objectManager->get should be $this->objectManager->create because it is working for me with $this->objectManager->create – Prashant Valanda May 3 '16 at 14:02
  • @PrashantValanda I've updated the code ;) – Raphael at Digital Pianism May 3 '16 at 14:08
1

You can using block method,

i have given demo for wishlist, you can change any module using replace Magento_Wishlist to your required module in setConfigvalue parameter 1.

class Custom{
    protected $resourceConfig;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Config\Model\ResourceModel\Config $resourceConfig
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->resourceConfig = $resourceConfig;
    }


    protected function setConfigValue($pathId, $value, $scope = 'default', $scopeId = 0)
    {
        $this->resourceConfig->saveConfig($pathId, $value, $scope, $scopeId);
    }

    /**
     * Disable module's functionality for case when new relic extension is not available
     *
     * @return void
     */
    public function disableModule()
    {
        $this->setConfigValue('advanced/modules_disable_output/Magento_Wishlist', 1);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.