grav-plugin-form icon indicating copy to clipboard operation
grav-plugin-form copied to clipboard

Pass page variables to form

Open butchewing opened this issue 8 years ago • 8 comments

I'm using {% include "forms/form.html.twig" with {form: forms('contact-form')} %}. I have a forms folder with a default.md containing multiple forms. I do this so that I can include any one of these forms on multiple pages.

I would like to send the {{ page.header.title }} to the form. I tried, adding it as a hidden field. I also tried adding more to the form array like {% include "forms/form.html.twig" with {form: forms('contact-form', data: '{{page.header.title}}')} %}

butchewing avatar Mar 01 '17 18:03 butchewing

Unfortunately it's not that simple. Forms are processed and stored during onPageProcessed and are not really modifyable beyond that point. This will need some consideration.

rhukster avatar Mar 09 '17 00:03 rhukster

To add my vote. It would be really useful to allow people to go back to a page later by including a reference in the email form such as {{page.header.title}} or {{page.url}}. Applies to forms both embedded in the page and and called from the page.

withindale avatar May 07 '17 12:05 withindale

@rhukster Need the same thing {{ page.title }} in my mail subject. How?

Sogl avatar Nov 26 '17 01:11 Sogl

  • Add a text input to your form
  • give it a class to hide it with css
  • give it an id to reference it with JS
  • Then write some JS set the value of this input with whatever you want.
  • If the visitor fills in the form, the email wil contain your data

KennethJoris avatar Feb 14 '19 14:02 KennethJoris

How about adding forms to the pages like this (to the header):

forms: 
  'new-form-id': '/path/to/another/form:old-form-id'

It doesn't work yet, but it shouldn't be too hard to add support for it for Form 3.0.

PS. you can already add data to the forms by using page header.

EDIT: OK, this may not work in your case as you likely do not want to duplicate title or set the form in the header.

mahagr avatar Feb 15 '19 21:02 mahagr

I had the same issue, I wanted to read the email from the page frontmatter:

email:
  subject: "[Contact Form] {{ page.title }}"
  body: "{% include 'forms/data.html.twig' %}"
  to: "{{ page.header.contact_email }}"
  to_name: "{{ page.title }}"

This is useful, as I am reusing the form on multiple pages:

{% include "forms/form.html.twig" with { form: forms('restaurant_contact') } %}

I've added a patch here: https://github.com/getgrav/grav-plugin-email/pull/141

Also this was requested in the forum: https://discourse.getgrav.org/t/processing-page-variables-in-a-form/9964/6

NicoHood avatar Dec 09 '20 15:12 NicoHood

A workaround would be:

        page_title:
          type: hidden
          evaluate: true
          default: 'page.title|e'

And in the email processing:

to_name: "{{ form.value.page_title|e }}"

Note: This is not safe, as the user can change the fields in the POST data!!!

NicoHood avatar Dec 09 '20 18:12 NicoHood

This can be closed, as the PR got merged!

NicoHood avatar Apr 19 '21 12:04 NicoHood