grav-plugin-form
grav-plugin-form copied to clipboard
Pass page variables to form
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}}')} %}
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.
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.
@rhukster Need the same thing {{ page.title }}
in my mail subject. How?
- 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
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.
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
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!!!
This can be closed, as the PR got merged!