Magento2 – How to Truly Disable Modules

magento2module

I have a module that has errors when compiling.

I am disabling it via cli: magento module:disable Aheadworks_Layerednav.

Then magento module:status shows the module as disabled. Cleared cache, removed everything from var folder.

But then when I run magento setup:di:compile I get
Errors during compilation:

    Aheadworks\Layerednav\Block\Navigation
        Incorrect dependency in class Aheadworks\Layerednav\Block\Navigation in /data/web/public/app/code/Aheadworks/Layerednav/Block/Navigation.php
\Magento\Store\Model\StoreManagerInterface already exists in context object

My expectation was that disabled modules aren't compiled.

Best Answer

@claudiu Creanga it looks like if you rename or remove your registration.php that when you run the module:status command after running module:disable Your_Module, the module doesn't show up in the "List of disabled modules". Then if you run setup:di:compile you won't see any errors, if there are any, for that module.

UPDATE: I was focusing on the di compilation and not focusing on the error message you shared. Your solution is here: http://magento-quickies.alanstorm.com/post/138686669515/quick-note-on-already-exists-in-context-object . Your problem doesn't have anything to do with a module not being disabled. It's a matter of doubly injecting an interface.

  1. Looks like you'll want to go to your /data/web/public/app/code/Aheadworks/Layerednav/Block/Navigation.php class
  2. Look for the StoreManagerInterface being passed as an argument to your constructor and remove that and the comma from the line above (if there is one)
  3. Then, in the body of the constructor, there will probably be something like $this->_storeManager = $storeManager or $this->_storeManagerInterface = $storeManagerInterface (I don't have the extension, so I can't give you an exact answer; But if you'd like to post your code I can show you exactly what to change); Make sure you're assigning $this->_storeManager = $context->getStoreManager() instead of an instance of the StoreManagerInterface

If you check Magento\Framework\View\Element\Template\Context class you'll see the getStoreManager() method, which returns an instance of \Magento\Store\Model\StoreManagerInterface, therefore the error you're seeing is Magento telling you that this StoreManagerInterface is already available, so no need to inject it into the Navigation block's constructor.

One of the things that intrigued me from Alan's article, was that these errors aren't shown in Developer mode, only when we explicitly run the setup:di:compile command.

I hope this helps.