0

I want to give ability to admin to active or deactive a module for each store.

at previous magento version this option was exist under store -> configuration -> advanced -> advanced menue and we could enable or disable a module per store , but from magento 2.2 this option has been removed and no longer avalable.

according this link for this purpose we have to do some setting manually in our module .

I want to make setting in my module that admin can see the list of stores and by selecting them to enable or disable a module . for example admin wants to enable module be active for store A and B , and deactive for store C .

the list of stores should be dynamic and reads from database and shouldn't be hard code.

I don't have any idea how can I do it .Someone can help me figure out how I can do it ? If you could please point this out, I would be really grateful .

1

You can create admin filed using system.xml, then use that field to whatever the location..

For example..

By using etc/system.xml

 <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>General Configuration</label>
      <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Module Enable</label>
          <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
      </field>
 </group>

Then get this value in helper class

/*
 * @return bool
 */
public function isEnabled($scope = ScopeInterface::SCOPE_STORE)
{
    return (bool) $this->scopeConfig->isSetFlag(
        'additional_registration_fields/general/enable',
        $scope
    );
}

then in block you can use as

public function isEnabled()
{
    return $this->helperData->isEnabled();
}

& final check on view files of frontend:

<?php if ($block->isEnabled()) : ?>
    // code here if module is enabled for current storeview
<?php endif; ?>
  • thank you for reply , that's good idea , but I want enable or disable a module for both frontend and adminhtml for specefic stores, and I don't have access to change core magento codes , I want set configuration in my module and these changes apply throughout the store . is there any global variable to hold state of modules in magento that magento checks them for loading modules ? – Behzad kahvand Apr 25 at 14:39
  • Yes, You can enable/disable whole module by using this method too.. – Arslan Tabassum Apr 25 at 17:01
  • Just add if condition in every controller's execute method like this ... if ($this->helperData->isEnabled() == 'yes' ) { // your whole code here } – Arslan Tabassum Apr 25 at 17:34
  • your were right , just one question , how can I use this if condition for prevent load js files ? . I have requirejs-config file that I load some js files instead of core magento js files , how to load these files conditionally ?. – Behzad kahvand Apr 26 at 19:03
  • 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.