Processing page variables in a form

Hello everybody,

I have just set up my first proper form, a simple contact form, to send mails via SMTP, which works nicely. Now I would like to do something a bit more advanced:

I have several similar pages, each featuring a Thing. All of these pages shall have the same form included at the bottom. When somebody fills it out and submits it, they shall be sent an email with a PDF attached about the Thing they were interested in (so that’s a page-specific parameter). Another email shall be sent to the site owner to let them know the details of who was interested in which Thing (also page-specific).

I defined this form and include it in the template for these Thing pages like so:

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

The problem now seems to be processing each page’s details. For example putting the page title in the subject line:

process:
    -
        email:
            subject: "Somebody wants to know more about {{ page.title }}"
            body: "{% include 'forms/data.html.twig' %}"

That outputs nothing after “about”. Why? The include for body: works just fine. I feel like I might need to put twig: true somewhere, but where?

Thank you for your time and thoughts!

I recently created a thing that just might help you with this. It’s called the Form Prefiller Plugin.

It will help in using for instance page specific data from your page frontmatter in your form. You can use it to prefill a normal form field or a hidden field.

The demo page in the demo folder in the repository contains a number of examples. As GitHub tries to display that markdown file and fails you are better off selecting the raw presentation.

Although the page does have a title it’s set in the page frontmatter. For mysterious reasons the frontmatter is more often than not called “page header” in Grav. So the reason that {{ page.title }} outputs nothing is probably due to that there is no such frontmatter variable. Instead use {{ page.header.title }}.
Rectification: Both {{ page.title }} and `{{ page.header.title }}’ are valid in both markdown and Twig.

Thank you so much for your reply! I’m still stumped – I’ll check out your plugin next, maybe it’s just what I need.

I tried to follow the modular page with form example in the docs, since it’s quite similar. It says there:

In the form header, make sure you add the action parameter, with the modular page route.

Now I am having this weird issue: if I put

action: {{ page.url }}

in the form.md header, it nicely outputs the correct path of the Thing page where the form is. BUT the rest of the form is broken and there’s no output of it at all. It also tells me

Failed to read /path/to/form.de.md: Unexpected characters near “}” at line 5 (near “action: {{ page.url }}”).

If, however, I put it in quotes:

action: '{{ page.url }}'

it gets output in the html as action="/{{ page.url }}", which obviously doesn’t work either. But then the rest of the form is alright! So what’s up here? Should I wrap it in something? I’ve tried turning on twig evaluation, doesn’t change the issue though.

Any help is much appreciated!

After some time investigating this and trying similar things as you did I think the action parameter can’t be set dynamically. Your best option is to raise an issue in the Form Plugin repository.

An update: I have changed things around a bit and got a few things to work now.

  1. I have put the form details directly in the page’s header, with the intention to prefill this via a small plugin whenever a new page with that template is created. This solves the problem with the action.

  2. I have added a hidden field with the page title, like this:

     -
         name: thing
         label: Thing
         type: hidden
         evaluate: true
         default: 'page.title'
    

(Got that from here, works perfectly.)

I also added a yaml blueprint for this page template to my theme, with a simple text field and a file field that saves to a folder in user/data (this should not be reachable via url, right?). The only thing I’m still struggling with is getting this file attached – the email plugin seems to expect an array of details for an attachment, and I don’t know how to provide that. Maybe my best course of action would be to define a custom process that wrangles the attachment – I’ll look into that next.