Jump to content

Hooking a button on module configuration page


Recommended Posts

Hi all,

Not sure if this is even possible but I would like to add a button on my modules configuration page that triggers a method to run.

The method in question belongs to the module itself.

Is this possible?

So far I have:

MyModule.module

    public function init(){
        wire('forms')->addHookAfter('InputfieldSubmit:processInput', $this, 'foo');
    }

    public function foo(){
        error_log('IT WORKS!!');
        return;
    }

MyModuleConfig.php

    public function __construct() {

        $this->add(
            [
                [
                    'name' => 'Foo Button',
                    'type' => 'InputfieldSubmit',
                    'value' => 'Fire Foo Method',
                ]
            ]
        );

    }

This renders the button but so far haven't had any luck getting the method to fire successfully.

I haven't found any examples from my investigations so apologies if this is an impossible request.

 

 

Link to comment
Share on other sites

It's not impossible, but the approaches may vary a bit depending on what your module exactly does (and is).  The most direct approach would probably be to hook before ProcessModule::executeEdit, if $this->input->post->foobutton is set then execute your method and immediately redirect to the current page, optionally showing notifications via $this->session->message(). executeEdit is the method that outputs and processes the form with the module settings. Make sure that your button's name is valid (no blanks) since this needs to be access as a property of $input->post.

Link to comment
Share on other sites

26 minutes ago, BitPoet said:

It's not impossible, but the approaches may vary a bit depending on what your module exactly does (and is).  The most direct approach would probably be to hook before ProcessModule::executeEdit, if $this->input->post->foobutton is set then execute your method and immediately redirect to the current page, optionally showing notifications via $this->session->message(). executeEdit is the method that outputs and processes the form with the module settings. Make sure that your button's name is valid (no blanks) since this needs to be access as a property of $input->post.

Changed to use ProcessModule::executeEdit, and it fires the method.

However as you would imagine it fires it on the page load when you try to view the page with the button on it which isn't ideal.

If there was a way to pass the value of the button to the hooked method I could just use a conditional but again im not sure if thats possible

Link to comment
Share on other sites

It's just a question of proper naming. See this working example:

<?php

// MyModule.module

class MyModule extends WireData implements Module, ConfigurableModule {
	
	public static function getModuleInfo() {
		return array(
			"title"				=>	"MyModule",
			"description"			=>	"Adds a button to the module config that executes the foo() method",
			"version"			=>	"0.0.1",
			"autoload"			=>	true
		);
	}
	
    public function init(){
        $this->addHookBefore("ProcessModule::executeEdit", $this, "foo");
    }

    public function foo(HookEvent $event){
    	if($this->input->post->foobutton) {
	        $this->session->message("foo() executed!");
	        $this->session->redirect($this->page->httpUrl . "edit?" . $this->input->queryString);
    	}
    }
	
}
<?php

// MyModuleConfig.php

class MyModuleConfig extends ModuleConfig {
    public function __construct() {
        $this->add(
            [
                [
                    'name' => 'foobutton',
                    'type' => 'InputfieldSubmit',
                    'value' => 'Fire Foo Method',
                ]
            ]
        );
    }	
}

 

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

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

×
×
  • Create New...