Jump to content

How to hook


Federico
 Share

Recommended Posts

Hi,

here is my attempt in short. I want to hook the submitted form in custom admin module and fire a function if conditional applies.

Problem: I cannot find a working hook to check if a form has been submitted. I would like to fire the addButtonMethod function if the form "form_application" is submitted by the "hook_accept_application" button.

I know it sounds basic, but I have tried all of the following without success. Thanks a lot for the help

public function init() {
  parent::init();
  $this->addHookBefore('InputfieldForm::processInput', $this, 'addButtonsMethods');
  $this->addHookBefore('InputfieldWrapper::processInput', $this, 'addButtonsMethods');
  $this->addHookBefore('InputfieldSubmit::processInput', $this, 'addButtonsMethods');
}

public function ___execute() {
  $modules = $this->wire('modules');
  $form = $this->modules->get("InputfieldForm");
  $form->action = "./";
  $form->method = "post";
  $form->attr("id+name", "form_application");
  $btn_application_create = $this->modules->get("InputfieldSubmit");
  $btn_application_create->attr('id+name', 'hook_accept_application');
  $btn_application_create->value = "Start Student";
  $btn_application_create->addClass("ui-priority-primary button_submit");
  $form->append($btn_application_create);

  return $form->render();
}

public function addButtonsMethods($event) {
  if($this->input->post->hook_accept_application){
  	wire('session')->redirect(wire('pages')->get(123)->editUrl;
  }
}

 

 
Link to comment
Share on other sites

Hi @Federico

Why just not check for input post like 

$modules = $this->wire('modules');
  $form = $this->modules->get("InputfieldForm");
  $form->action = "./";
  $form->method = "post";
  $form->attr("id+name", "form_application");
if($this->wire('input')->post->your_submit_button) {
  $btn_application_create = $this->modules->get("InputfieldSubmit");
  $btn_application_create->attr('id+name', 'hook_accept_application');
  $btn_application_create->value = "Start Student";
  $btn_application_create->addClass("ui-priority-primary button_submit");
  $form->append($btn_application_create);
}

 

Link to comment
Share on other sites

Hi @Zeka

In the code above I have the POST input check, as follow:

if($this->input->post->hook_accept_application){
	wire('session')->redirect(wire('pages')->get(123)->editUrl;
}

which works. However, for a more articulated module development I am looking for a hook method to fire a function when the form named "form-application" is submitted. In other words, I am looking to wrap it into a function like the following one. Any idea?

public function addButtonsMethods($event) {
  if($this->input->post->hook_accept_application){
  	wire('session')->redirect(wire('pages')->get(123)->editUrl;
  }
}

 

Link to comment
Share on other sites

Ok, then

public function ___execute()
{
	$out = '';
	$input = $this->wire('input');
	$modules = $this->wire('modules');
	$form = $this->modules->get("InputfieldForm");
	$form->action = "./";
	$form->method = "post";
	$form->attr("id+name", "form_application");
	$btn_application_create = $this->modules->get("InputfieldSubmit");
	$btn_application_create->attr('id+name', 'hook_accept_application');
	$btn_application_create->value = "Start Student";
	$btn_application_create->addClass("ui-priority-primary button_submit");
	$form->append($btn_application_create);

	if ($input->post->hook_accept_application) {
		$form->processInput($input->post);

		if ($form->getErrors()) {
			$out .= $form->render();
		} else {
			$this->addButtonsMethods();
		}
	} else {
		$out = $form->render();
	}

	return $out;
}

public function addButtonsMethods($event)
{
	wire('session')->redirect(wire('pages')->get(123)->editUrl;
}

 

  • Like 1
Link to comment
Share on other sites

$wire->addHookAfter("InputfieldForm(name=form-application)::processInput", function($event) {
    $form = $event->object; /** @var InputfieldForm $form */
    if(count($form->getErrors())) return;

    // do your stuff
    $input = $event->arguments(0);
});

Something like this?

See https://processwire.com/api/ref/inputfield-form/process-input/

  • Like 2
Link to comment
Share on other sites

Thanks @Zeka and @bernhard

for some reasons the code posted by Bernard gives an error, even if starts with 

$this->wire->addHookAfter

instead of just 

$wire->addHookAfter

 maybe because the form name declaration in this method (I did not know, can you actually declare the form name in that?)

InputfieldForm(name=form-application)::processInput

 Anyway, I have solved this minor issue with the approach as per the Zeka suggestion. Thank you both

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

  • Recently Browsing   0 members

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