Jump to content

Add hook in a template


Manol
 Share

Recommended Posts

I´m trying the following in my template


function hookEmail($event){
     $file = $event->return;
     echo "value: ". $file->value;
}

$form = $forms->get("reconocimientos");

echo $form->render(); 

$form->get("reconocimientos")->addHookAfter("processInput", null, 'hookEmail');

but get this error

Error: Call to a member function addHookAfter() on a non-object (line ...

Link to comment
Share on other sites

I think your issue is not the hook, but rather the: $form->get("reconocimientos")

It is not an object. I am not really very familiar with hooking in templates, but I just tested :) this and it works:

<?php

$page->addHookBefore('Page::loaded', null, 'errorTest');
function errorTest(HookEvent $e) {
    error_log('testhook');
}

so I think you are on the right track, but you need to sort out your form object. Are you maybe using FormBuilder? or do you have $forms defined elsewhere in your code?

Link to comment
Share on other sites

maybe try something like this? to get the field it sometimes easiest to use a find()

or maybe simply $field = $form->get("name=reconocimientos");

function hookEmail($event){
    $file = $event->return;
    echo "value: ". $file->value;
}

$form = $forms->get("reconocimientos");
$field = $form->find("name=reconocimientos")->first();
echo "field: " . $field->name . "<br/>";

$field->addHookAfter("processInput", null, 'hookEmail');

echo $form->render();

the hook needs to be before you render the form too. but not sure what you want to achieve

  • Like 2
Link to comment
Share on other sites

NOTE: I'm really sorry but sometimes is really hard to find the right information in the forum, I try hard to found it before making people losing time, and is not a complain at all, people here is in generaly very keen on helping others as I do when I have the knowledge. Special thanks to Soma, he really has helped me a lot and still does.

Anyway, what I'm trying is to hook a formbuilder form embedded with option C, I would like to copy some files and data from the form  to an external database and I need to hook the form after submit to get the results but I'm totally unable to do that, even the above code is not working for me, I thought I had  understood how it works and even make a little resume to help others here but no way to do it after hours researching, I know I'm near the solution but no way to get it work.

Any help is welcome.

  • Like 1
Link to comment
Share on other sites

NP manol. I?m just kidding. BUt this would be more of a formbuilder support question.

FB is special in that it needs other methods:

function hookForm($event){
  $form = $event->object;
  $file = $form->get("file");
  print_r($file->attr("value"));
}

$forms->addHookAfter("InputfieldForm::processInput", null, "hookForm");

echo $forms->load('contact-form')->render();
 

But I'm not making any money out of form-builder (but Ryan) so I'm not too keen on helping out. :P I don't mean it really...

Also there's some threads about this exactly and you better search via google as mentioned....

Edit: Ok, maybe not so good searching via googel as the form builder forum is private :)

  • Like 2
Link to comment
Share on other sites

It's also to know what to search :)

I remembered this and looked at FormBuilderProcessor.module, I found this in the FB forum with "formSubmitSuccess" http://processwire.com/talk/topic/4188-managing-successerror-messages/?hl=formsubmitsuccess#entry41151

In case of FB you may want to only hook if the form is submitted successful and you could do this in yet another way (yeah it's not easy)

 
function hookForm($event){
  $form = $event->arguments('form'); // as per the argument in the hooked function could also be arguments(0)
  $field = $form->get("file"); // familiar?
  print_r($field->value); // file field
  // do stuff
}

wire()->addHookAfter("FormBuilderProcessor::formSubmitSuccess", null, "hookForm");

echo $forms->load('contact-form')->render();
 
Or FormBuilderProcessor::formSubmitError accordingly 
 
It's all a little different with formbuilder and I had to try and lookup myself. You can't hook, at least I couldn't find out how (maybe Ryan maybe knows more) into the inputfield processInput directly as with regular InputfieldForms.
  • Like 1
Link to comment
Share on other sites

NOTE: I'm really sorry but sometimes is really hard to find the right information in the forum, I try hard to found it before making people losing time, and is not a complain at all, people here is in generaly very keen on helping others as I do when I have the knowledge.

Tell me about it. The search functionality in this forum doesn't really work well. 

Link to comment
Share on other sites

Hi Manol, not sure if this would help, but last time i had to add a hook to FB, i used something like this, in the form-builder.inc file:

$forms->addHookBefore('FormBuilderProcessor::saveForm', null, 'hookCampaignMonitor');

function hookCampaignMonitor(HookEvent $event) {
	$form = $event->object->getInputfieldsForm();
	
	// make sure it's the form you want	
	if ($form->name != 'product-inquiry') return;
	
	// grab the data
	$email = $form->get('email_address')->attr('value'); 
	$first = $form->get('name_first')->attr('value'); 
	$last = $form->get('name_last')->attr('value'); 
	$subscribe = $form->get('subscribe'); 
	$name = $first . ' ' . $last;
	
	// check to see if they subscribed
	if($subscribe->attr('checked')) {
		$cmurl = 'http://www.example.com/?some-var=' . urlencode($email) . '&cm-name=' . urlencode($name);

		// post the data
		$http = new WireHttp();
		$http->post($cmurl); 
	}
			
}
  • Like 2
Link to comment
Share on other sites

$form = $forms->get("reconocimientos");
echo $form->render();
$form->get("reconocimientos")->addHookAfter("processInput", null, 'hookEmail'); 

A couple things to mention about the above code (quoted from above). You are adding a hook after the render(). You would need to add the hook before the render. That's because processInput is triggered from render(). Secondly is that you are hooking the wrong thing, as I don't think there is a 'processInput' method on $form (since it's not an InputfieldForm). What you'd want to do instead would be this (in your form-builder.inc file):

wire()->addHookAfter('FormBuilderProcessor::processInput', null, 'hookProcessInput'); 
function hookProcessInput(HookEvent $e) {
  $processor = $e->object;
  if($processor->formName != 'reconocimientos') return;
  $inputfields = $processor->getInputfieldsForm();
  // your code here. 
}
  • Like 2
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...