Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

Globally configured frontend forms

form

Solved by pamtbaau View solution

Started by Charlotte 4 years ago · 10 replies · 613 views
4 years ago

Hello there!

I'm looking for a way to globally configure forms for the purpose of reusing them across many landing pages.

I'm aware I could create the form, once, inside any page, then use its name to include it in other pages. However, these landing pages will be short-lived, and making all of them dependent on one arbitrary page is not something I feel very comfortable with (especially since my client will be the one creating the landing pages, from the admin interface).

I've tried defining the form inside my site.yaml, but to no avail. I've also stumbled upon the usage of a frontmatter.yaml file, however, these also appear to be page-specific only.

Is there a way to define forms, once, in a global file accessible to all pages?

4 years ago

I'm a bit confused. Do you want to have one global form which would show on all pages, or you want to have multiple forms, from which you could assign one to a page to show?

4 years ago Solution

@smiletolerantly,

Is there a way to define forms, once, in a global file accessible to all pages?

Yes, see Displaying forms from Twig

👍 1
last edited 04/25/22 by pamtbaau
4 years ago

I have started to add a user/pages/shared folder for reusable content in my projects. It's always a bit different, I am still looking for a good standard structure. Haven't done this to pull a form out, but it should work fine :P

You might have user/pages/shared/forms/reusable-form.md, containing the frontmatter:

YAML
routable: false
form:
  # your form here

(the page is already not visible because of its folder names)

Then in your landing page template you might have:

TWIG
{% include 'forms/form.html.twig' with {form: page.find('/shared/forms').forms(0) } %}

(edited)
That last expression page.find('/shared/forms').forms(0) probably needs tweaking, I apologise for not testing it. Edit: That expression seems fine, you could also use ...forms('NameOfForm').

Grav might also complain that there is no template reusable-form.html.twig in your theme. I doubt it.

I have mostly used the shared pages folder for modular page components (modules) so far.

I'll update this if I get a chance to test that code above.

last edited 04/25/22 by Hugh Barnes
4 years ago

@Karmalakas The latter. Have some predefined forms, and be able to include them in any page/template.

@pamtbaau That's not really a solution though, since displaying a form anywhere is not the issue (either through the method you linked, or through the inclusion of a twig snipped in the markdown itself).

The issue is defining the forms somewhere. The forms are not logically tied to any one page, and there's a possibility that a form will not be displayed on any page - so, where put the definition?

A global version of frontmatter.yaml would solve the problem, and so would being able to define forms in site.yaml.

@hughbris That looks like what I'm trying to do! Ngl, it feels a little hacky, but I'll have a try at your method. Thanks!

4 years ago

I would have forms defined somewhere as @hughbris suggested and then a separate page template with extended default (or whichever template you use for landing pages) and additional field to choose a form from a dropdown. You might need a simple plugin though for the dropdown to return forms selection though

4 years ago

@smiletolerantly:
Ngl, it feels a little hacky,

It is hacky! I haven't come with anything better overall, and been thinking about this problem for a while. The core team would probably tell you to use flex objects somehow and they will be right. That's no hack. Good luck if you can follow the documentation!

4 years ago

I understood it's always the same form, so could be hardcoded into the template, but I could have interpreted it wrong.

4 years ago

@Karmalakas Good idea! The blueprint for my landing template already extends default, so it should be easy to add.

@hughbris It works splendidly though! Very nice. Do you happen to know if there's a way to hide the /shared/ folder in the Admin panel, though? ^^

4 years ago

@smiletolerantly:
Do you happen to know if there’s a way to hide the /shared/ folder in the Admin panel, though?

I don't actually and had a quick check. There should be custom frontmatter you can put on a page. That's the convention in a lot of plugins. It might exist.

You could probably use the permission system to prevent a group of users from editing the page if that suffices.

4 years ago

@pamtbaau:
Yes, see Displaying forms from Twig

Following this link, it looks like there is another expression for referencing the shared form:

TWIG
# Contact Form
{% include "forms/form.html.twig" with { form: forms( {route:'/forms/contact'} ) } %}

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1135 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 61 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 134 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 108 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 128 7 months ago