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.

Plugins

Issue with forms and caching

form

Solved by Dabu Ka View solution

Started by Dabu Ka 3 years ago · 4 replies · 389 views
3 years ago

Hi,

I have a site with three languages and a products listing where each product page has a contact form. To get the same form everywhere I created a page where the form is defined in all languages and included in the product's detail view like this:

TWIG
      {% set contact = page.find('/product-contact') %}
      <h2>{{ contact.title }}</h2>
      <p>{{ contact.content }}</p>
      <div class="contact-area">
           // also tried just form: forms('form-name') here as well, same behaviour
          {% include 'forms/form.html.twig' with { form: forms({'route' : contact.route}) } %}
      </div>

The /product-contact frontmatter has cache_enable: false in its frontmatter, but when I have Grav caching enabled there's a very strange behaviour where it'll show up only on 2 of the 3 languages. After clearing the cache whichever language page I load first renders the form fine, then usually switching to the second one is fine as well, but on the third it returns null.

When disabling Grav caching everything works as expected. Any idea? And thanks in advance!

Form: v7.1.2 and Grav v1.7.39.4

3 years ago Solution

So after messing around a little bit more looks like getting the form directly from the page object and passing that on seems to do the trick and now it's working as expected.

TWIG
{% set contact = page.find('/product-contact') %}
{% include 'forms/form.html.twig' with { form: contact.getForms()['product-form'] } %}
3 years ago

@dabuuker, Would this more simpler snippet also work for you:

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

This sample is copied from the docs Displaying Forms from Twig saying:

Using this method, you can choose a specific name of a form to display. You can even provide the name of a form defined in other pages. As long as all your form names are unique throughout your site, Grav will find and render the correct form!

last edited 03/15/23 by pamtbaau
3 years ago

That's the first method I tried and ran into the same issues in the original post. My use case is a bit different than the examples there as well.

3 years ago

@dabuuker, Thanks for testing.

I wouldn't expect the observed behaviour and haven't found a similar know issue.

Would you mind sharing your observations to the dev team by showing the caching results of all three snippets?

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 75 1 week ago
Plugins · by Xavier, 4 weeks ago
2 81 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1208 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 74 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 99 2 months ago