Jump to content

How to hook into uninstall checkbox on module configuration page [SOLVED]


Juergen
 Share

Recommended Posts

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

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
		);
	}

 

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

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

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.

  • Like 3
Link to comment
Share on other sites

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 by Robin S
Add example in module context
  • Like 3
Link to comment
Share on other sites

  • Juergen changed the title to How to hook into uninstall checkbox on module configuration page [SOLVED]

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...