4

I have 2 stores views and a custom module, lat say module X with an observerv.

public function execute(\Magento\Framework\Event\Observer $observer)
    {   

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->get('Magento\Customer\Model\Session');

         $urlInterface = $objectManager->get('Magento\Framework\UrlInterface');

        //check if the crrent url not belong to the clubshop
        if($urlInterface->getCurrentUrl() =='https://www.storeview1.com/'){
            return true;
        }
        if(!strpos($urlInterface->getCurrentUrl(), "club_login" )){

            if($customerSession->getMyValue()==1){
                return true;

            }else{

            //redirect if sotreview 2
             $url = $urlInterface->getUrl('club_login');
                 $observer->getControllerAction()
                            ->getResponse()
                            ->setRedirect($url);
                            return false;
            }

        }

    }

For storeview no 1. this module in disabled(Storev-Config-Developer-Advanced) and for storeview no 2 this module is enabled.

Even the module is disabled on sotreview no 1, the observer still work on both stores and it redirect me.

What i tried to do, is to check if the module x is or no enabled on the storeview no 1, to avoid the extecution of this function.

Does anyone know how to do that?

  • how can one module be disabled on one store view and enabled for another? you mean the output is disabled from system->configuration->advanced? – Marius Sep 6 '17 at 7:51
  • yes. I updated my question. – Alin Lupoiu Sep 6 '17 at 7:53
7

From Storev-Config-Developer-Advanced you only disable the output of a module and not the module itself. So the plugins the preferences and the event observers are still taken into consideration.
If you want to detect if a module output is disabled you can do this:

change your class constructor to provide an instance of Magento\Framework\Module\Manager as a dependency:

protected $moduleManager;
public function __construct(
    ...
    \Magento\Framework\Module\Manager $moduleManager,
    ....
) {
    ...
    $this->moduleManager = $moduleManager;
    ...
}

then you can call in your execute method this:

if ($this->moduleManager->isOutputEnabled('Vendor_Module')) {
    //the module output is enabled
} else {
    //the module output is disabled
}

you might not need the else part

  • great. thank you, but how this will know when is enabled and when is disabled? For example if a go on storeview 1 url how this code will know if on this store the output is disabled and for the other one is disabled? – Alin Lupoiu Sep 6 '17 at 8:15
  • 1
    it will check the configuration setting. But I just found something. Starting version 2.2.0 the output disabling is not possible anymore. So maybe you should rethink your architecture to take into consideration a custom yes/no setting instead of the module output. github.com/magento/magento2/blob/2.2.0/lib/internal/Magento/… – Marius Sep 6 '17 at 8:17
  • Ok. thank you very much for your help. I will take this in consideration – Alin Lupoiu Sep 6 '17 at 8:18
9

You can check module status by below command:

php bin/magento module:status

You can see the list of enables modules.

  • thank you for your answer, but i want to make this check inside of observer. – Alin Lupoiu Sep 6 '17 at 8:34
  • 1
    you can check it by app/etc/config.php – shivashankar m Mar 21 '18 at 12:03
1

You can use the method isEnabled to check if your module is enabled, if your code is enabled when you use this command below, so you can use this method to do the same check:

php bin/magento module:status

First thing, add the class into your module.

protected $moduleManager;
public function __construct(
    ...
    \Magento\Framework\Module\Manager $moduleManager,
    ....
) {
    ...
    $this->moduleManager = $moduleManager;
    ...
}

Then implement this check:

if ($this->moduleManager->isEnabled('Vendor_Module')) {
    //the module is enabled
} else {
    //the module is disabled
}

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.