Jump to content

Formbuilder - one form, different recipients


brandy
 Share

Recommended Posts

Hi!

I am using Formbuilder on a website.
The form is for contacting, if you are interested in a real estate.

With the following code I read out, on which site you are sending the contact form:

<?php $form = $forms->render('kontakt', [ 'form_page' => $page->parent->title . " " . $page->title ]); ?>

So you receive a mail with the parent-page and page-title as subject, which works perfectly.

Is it now possible to extend this and send the contact-form to different mailadresses, depending on the page-parent?
I do have a field in the parent, which is linked to the right contact-information.

Thanks a lot!

Link to comment
Share on other sites

You know what, I misread your issue.

What could probable work is placing a hook in emailFormReady

https://processwire.com/store/form-builder/hooks/#where-to-place-hooks

wire()->addHookBefore("FormBuilderProcessor::emailFormReady",function($e){
    /** @var InputfieldForm  **/
	$form = $e->arguments(0);
    /** @var FormBuilderEmail **/
    $email = $e->arguments(1);
    if($form->name !== "the_form_in_question") return;
	$page_id = $form->getChildByName('form_page_id');
	if($page_id){
	   $contact_email = wire('pages')->get($page_id)->contact_email;
       $email->to = $contact_email;
    }
})

form_page_id would be a hidden field in the form where you save the page_id, not sure if there is a nicer way to do this! That is, without this hidden field to pass on the id info.

Link to comment
Share on other sites

Thanks a lot!

I do have a construction, like this, for the emailSubject:

				<?php 
					$processor = $event->object;
					$processor->emailSubject = "Page title is: $page->title";			
					echo $form;
				?>

Is it possible to make it very simple like

$processor->emailReceipient =

Only a quick thought!

  • Like 1
Link to comment
Share on other sites

Yeah, i forgot it too.

I added following:

$processor->emailTo = "{$page->parent->projekt_bautraeger->bautraeger_mail}";	

As the e-mail is a pageReference-field in the parent, I have to build it like that, but it doesn´t work.

The following shows me the emailadress:

echo "{$page->parent->projekt_bautraeger->bautraeger_mail}";

Do you think there is a problem, as the form has recipients in the backend?

All in all emailTo seems good to me:
https://processwire.com/api/ref/form-builder-processor/

 

Link to comment
Share on other sites

In the head there is this:

<?php $form = $forms->render('kontakt', [ 'form_page' => $page->parent->title . " " . $page->title ]); ?>

And in the template there is this, which shows the form:
 

<?php 
	$processor = $event->object;
	$processor->emailSubject = "Page title is: $page->title";
	$processor->emailTo = "{$page->parent->projekt_bautraeger->bautraeger_mail}";			
	echo $form;
?>

Thanks a lot!

Link to comment
Share on other sites

Ok so the second part of the code, in the template needs a hook to actually work!

If you debug $processor variable, it is most likely null! 

So, in the head.php set this up:

<?php 

wire()->addHookBefore("FormBuilderProcessor::emailFormReady",function($e){
    /** @var InputfieldForm  **/
	$form = $e->arguments(0);
    /** @var FormBuilderEmail **/
    $email = $e->arguments(1);
  
    // is this the form where we want to change the behaviour?
    if($form->name !== "the_form_in_question") return;
 
	$page_id = $form->getChildByName('page_with_contact_email');
	if($page_id){
       $page_with_contact_email = wire('pages')->get($page_id);
       if($page_with_contact_email->id){
	   		$email->to = $page_with_contact_email->contact_email;
       }
    }
    // Not really sure if this is necessary so try putting it on and off
    $event->arguments(1, $email);
})

$form = $forms->render('kontakt', [ 'form_page' => $page->parent->title . " " . $page->title , 
                                    'page_with_contact_email' => $page->parent->id
                                  ]);

The hook code is a bit different from the previous one I posted since I realised that the contact_

Then on your template:

echo $form;

Let me know how it goes!

Link to comment
Share on other sites

Thanks a lot!

But I have a question - page_with_contact_email differs, depending from the site, where its send from.

So I mean, I have a parent page with subpages with the different contact-email-adresses.
Is this already recognized in this example?

Thanks a lot!

 

Link to comment
Share on other sites

Maybe this part should be:

$form = $forms->render('kontakt', [ 'form_page' => $page->parent->title . " " . $page->title , 
                                    'page_with_contact_email' => $page->parent("contact_email!=");
                                  ]);

So this part:

$page->parent("contact_email!=");

Should traverse upwards from the page where the form is rendering until it finds a page with the contact_email fileld.

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...