A form on every page using a template

Hello,

I have a series of pages which all end with the same form. I created the form in a separate page, and it works alone. Now I want to insert the form at the end of every page using that certain template.

For now, I added at the bottom of the template:

{% for form in page.collection() %}
    {{ form.content }}
    {% include "forms/form.html.twig" %}
{% endfor %}

And this to my pages:

content:
    items:
        '@page.self': /path/to/form

It’s probably not the cleanest way to do it because there’s only one form to import and not a collection, and ideally I would not need to add something to the front-matter of each page.

Anyway it doesn’t work. Only the content of the form page appears, and not the form itself.

How can I manage to do this?

Thanks :slight_smile:

The problem is your “form” is not being defined on each page. The form definition in the page frontmatter is what builds the form itself. One solution is to use modular pages and have the form defined there

Thanks for your answer. It’s actually the first thing I tried, but I couldn’t find a way to make it work on several pages.

I would like to define the form just once, and display it in several pages.

There is currently no way to do it with the Form plugin.
I would like to add a shortcode-based form you can embed in pages, but currently it’s not there yet.

You can use a js-based form from an external service (e.g. Wufoo or others) in the meantime.

Thanks, okay.

Another solution for me would be to have one form only, but with a select element to choose ‘what page your message is about’. In which way can I list, say, all the subpages of one page as the options of a select element in a form?

Hi again,
Wouldn’t this be possible to use import@ for this? By putting the whole form into an external yaml and calling it from each frontmatter?

First import@ only works when defining a form. It is not actually for importing yaml data from one place to another.

Overall this problem is not that trivial to solve. I was looking at this and it’s quite complicated. Currently the form data is assigned to the Twig object when viewing the current page. That’s why you currently need to add the form definition to the frontmatter of the current page.

For this to work, the form data will need to be parsed and associated with the page it was defined on. After the form is rendered, it still will have to process via a dedicated page because the current page wouldn’t know about the form coming from some other ‘included’ page.

Anyway, needless to say it’s a bit complicated. It’s something we want to add, but it might require a bit of a rewrite in order to support this properly.

Just browsing and haven’t tested, but what about something like this:

Since you only have one form but want it on several pages…again, just a thought, not sure if it will work.

No it won’t work because the process of parsing that form data is handled onPageInitialized, and that is only run for the overall page, it does not get kicked off when you simply include a a twig template of another page with a form.

This is why we need to process the form and assign the form properties to the page itself. Then it could be available whenever you call the page’s content()method.

This is the heart of the problem, it can be solved, but it’s quite a major rewrite that I hesitate to undertake while we’re trying to finish up Grav/Admin 1.1