Juergen Posted June 16, 2021 Share Posted June 16, 2021 Hello @all I am struggeling to find a way to set the checkbox status of the uninstall checkbox at the bottom of the module configuration page to disabled. I have created a module and I want that the checkbox should be disabled under certain conditions. I have tried to use this hook in my module file init method: $this->addHookBefore('ProcessModule::executeEdit', $this, 'showHideUninstallCheckbox'); and the method itself: protected function showHideUninstallCheckbox(HookEvent $event) { if(condition) .... run code to disable the checkbox } Maybe this hook is not suitable for this purpose. Can someone help me out to show me a way to achive this. Thanks in advance!! Link to comment Share on other sites More sharing options...
horst Posted June 16, 2021 Share Posted June 16, 2021 You can set the module as permanent, then it cannot be uninstalled. public static function getModuleInfo() { return array( 'title' => '', 'version' => 100, 'summary' => '', 'href' => '', 'author' => '', 'singular' => true, 'autoload' => true, 'permanent' => true // <-- PERMANENT ); } Or, if you need it conditionally set, you can check your condition and set the permanent state in the modules config function.(?) public static function getModuleInfo() { $conditionMatch = (bool) calculateSomeConditionHere(); return array( 'title' => '', 'version' => 100, 'summary' => '', 'href' => '', 'author' => '', 'singular' => true, 'autoload' => true, 'permanent' => $conditionMatch // <-- PERMANENT ); } 2 1 Link to comment Share on other sites More sharing options...
Juergen Posted June 16, 2021 Author Share Posted June 16, 2021 Thanks @horst Interesting thought, but the condition depends on a setting in a configuration field of the module, so I guess this will not work, but I will try it! 1 Link to comment Share on other sites More sharing options...
Juergen Posted June 16, 2021 Author Share Posted June 16, 2021 Ok, doesn't work! I used this code inside my checking method to set and save config data: $data = wire('modules')->getConfig('MyModuleName'); if(Condition){ $data['permanent'] = true; } else { $data['permanent'] = false; } wire('modules')->saveConfig('MyModuleName', $data); The permanent value will be changed and saved inside the data table, but it seems that there is a module cache problem which prevents the uninstall checkbox from beeing updated (disabled or not). Link to comment Share on other sites More sharing options...
netcarver Posted June 16, 2021 Share Posted June 16, 2021 I might be misunderstanding this, but I think you could be setting it in the wrong place. Could you change the array returned from getModuleInfo() based on the result of the condition? Like this; public static function getModuleInfo() { $module_info = [ 'title' => '', 'version' => 100, ... ... the other usual fields here ... 'permanent' => false, ]; $data = wire('modules')->getConfig('MyModuleName'); if (your_condition_based_on_settings($data)) { $module_info['permanent'] = true; } return $module_info; } Hope that helps. 3 Link to comment Share on other sites More sharing options...
Juergen Posted June 16, 2021 Author Share Posted June 16, 2021 Thanks @netcarver This works, but only if the module cache will be deleted. So I have to include a cache delete and save new method too. I will try to figure out how this could be realized. Link to comment Share on other sites More sharing options...
Robin S Posted June 16, 2021 Share Posted June 16, 2021 (edited) Here's a hook that might help: $wire->addHookBefore('ProcessModule::executeEdit', function(HookEvent $event) { if($event->wire()->input->get('name') !== 'YourModuleName') return; $event->wire()->addHookBefore('InputfieldForm::render', function(HookEvent $event) { /** @var InputfieldForm $form */ $form = $event->object; if($form->id !== 'ModuleEditForm') return; $your_module = $event->wire()->modules->get('YourModuleName'); if($your_module->foo === 'bar') { $uninstall = $form->getChildByName('uninstall'); $uninstall->description('You may not uninstall this module because foo equals bar.'); $uninstall->attr('disabled', 'disabled'); } }); }); Update: an example in the context of your module... public function ready() { $this->addHookBefore('ProcessModule::executeEdit', $this, 'disableUninstall'); } protected function disableUninstall(HookEvent $event) { if($this->wire()->input->get('name') !== $this->className) return; $this->wire()->addHookBefore('InputfieldForm::render', function(HookEvent $event) { /** @var InputfieldForm $form */ $form = $event->object; if($form->id !== 'ModuleEditForm') return; if($this->foo === 'bar') { $uninstall = $form->getChildByName('uninstall'); $uninstall->description('You may not uninstall this module because foo equals bar.'); $uninstall->attr('disabled', 'disabled'); } }); } Edited June 16, 2021 by Robin S Add example in module context 3 Link to comment Share on other sites More sharing options...
Juergen Posted June 17, 2021 Author Share Posted June 17, 2021 Yes, that works!! Hook inside hook -> nice combination. That was exactly what I was looking for!! Problem solved!! ? Thanks!!!!!! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now