Multilanguage forms missing until cache is cleared

I have exact same issue as described here 4 years ago.

It seems that after language switching, form can’t be found neither by name nor by page route :frowning: I tried:

{{ dump(forms('contact-form')) }}
{{ dump(forms({"route": "/contact/form"})) }}

Tried dumping right at the top level template, which extends base template. All I get is null. And as linked topic says - after cache is cleared, form is found in whichever language you load first, but after language switch, form is lost until you clear cache again :confused:

BTW, I have tried disabling cache both on these top level pages and on included form pages - same result. My form is described in frontmatter.yaml in the same folder as form.lt.md and form.en.md files (so that I wouldn’t have to maintain duplicate form).

I hope this has been fixed and I’m just missing something.

@Karmalakas, I’m afraid I cannot reproduce the issue…

  • Fresh install of Grav 1.7.3

  • Added the following to default ‘/config/system.yaml’

    languages:
      supported: [en,lt]
    
  • Created regular page + modular page

  • Created ‘/forms’ folder containing forms

  • Resulting directory tree:

    user/pages
    ├── 01.home
    │   └── default.md
    ├── 02.typography
    │   └── default.md
    ├── 03.page
    │   ├── default.en.md
    │   └── default.lt.md
    ├── 04.modular
    │   ├── _text
    │   │   ├── text.en.md
    │   │   └── text.lt.md
    │   ├── modular.en.md
    │   └── modular.lt.md
    └── forms
        ├── default.en.md
        └── default.lt.md
    
  • Content of regular pages ‘/03.page/default.en.md’ and ‘/03.page/default.lt.md’:

    ---
    form:
      name: in_page_form
      fields:
        name:
          label: in_page_form EN  <- or LT
          type: text
    ---
    
  • Content of modular pages ‘/04.modular/_text/text.en.md’ and ‘/04.modular/_text/text.lt.md’:

    ---
    form:
      name: in_module_form
    
      fields:
        name:
          label: in_module_form EN  <- or LT
          type: text
    ---
    
  • Contents of ‘/pages/forms/default.en.md’ and ‘/pages/forms/default.lt.md’:

    ---
    form:
      name: in_forms_dir_form
    
      fields:
        name:
          label: in_forms_dir_form EN  <- or LT
          type: text
    ---
    
  • Added the following lines to ‘/user/themes/quark/templates/default.html.twig’:

    {% include 'forms/form.html.twig' with { form: forms('in_page_form') } %}
    {% include 'forms/form.html.twig' with { form: forms('in_forms_dir_form') } %}
    

    Note: Each page will show two forms: From the page itself and from ‘/pages/forms’.

  • Add the following lines to ‘/user/themes/quark/templates/modular/text.html.twig’:

    {% include 'forms/form.html.twig' with { form: forms('in_module_form') } %}
    {% include 'forms/form.html.twig' with { form: forms('in_forms_dir_form') } %}
    

    Note: Each module will show two forms: From the module itself and from ‘/pages/forms’.

  • Browsing regular page: Switching back and forth between EN en LT:
    image

    image

  • Browsing modular page: Switching back and forth between EN en LT:
    image

    image

To me it seems all is going well…

Would you mind creating a similar step by step script to reproduce the issue?

Aha! Just managed to figure a situation, where I can’t reproduce it either, but that’s not an option, until this is fixed.

So here’s the deal.

  • The folder for contacts page is /contact-us
  • Slug for EN - contact-me
  • Slug for LT - susisiekime

The LangSwitcher links are folder based (I guess due to the issue mentioned above) and they look like:

  • EN - /en/contact-us
  • LT - /contact-us

(I think it’s just how pages.route() and pages.url() work)

No matter if I click on langSwitcher link or change manually in address bar to these links (why would it matter… :slight_smile:), form disappears. I think the redirect to correct slug URLs might mess things up. Once the redirect happens and form is missing, then only cache clear helps.

But… If I go from one language directly to appropriate link in another language:

  • EN - /en/contact-me
  • LT - /susisiekime

Then form is there like expected.

Also noticed, that when I switch language by going to correct links and in both languages forms are there, then navigating to folder based URLs or switching languages as usual work also as expected - forms do not disappear. Until next cache clear/page save.

Now I have to go to work, but I’ll try to play with URLs in the evening. Maybe I’ll figure something more. But I have very little hope - I tried already and I thought I found a solution, but it won’t work in modules.

@Karmalakas, Added LangSwitcher and added separate slugs for each language per page and module.

Still working fine.

Darn :confused: Will try in the evening to set up fresh install and see what happens :confused: Thanks for the effort

So, fresh Grav 1.7.3 install (without Admin) + Forms plugin

The main difference when forms fail to work on language switch is system.yaml setting:

pages:
  redirect_default_route: true

But here’s my full set up just in case



In Quark templates add two files:

fom.html.twig

{% if pageForm %}
    {% include "forms/form.html.twig" with {form: pageForm} %}
{% endif %}

contact-us.html.twig

{% extends 'partials/base.html.twig' %}

{% block content %}
    {% include "form.html.twig" with {pageForm: forms({"route": '/contact-us/form'})} %}
{% endblock %}

System.yaml

Later try uncommenting the redirect

languages:
  supported:
    - lt
    - en
  default_lang: lt
  include_default_lang: false
  content_fallback:
    en: en
#pages:
#  redirect_default_route: true

Folder user/pages/03.contact-us

contact-us.en.md

---
title: 'Contact me'
slug: contact-me
cache_enable: false
---

##### Text

contact-us.lt.md

---
title: Susisiekime
slug: susisiekime
cache_enable: false
---

##### Tekstas

Folder user/pages/03.contact-us/form

form.en.md

---
title: Form
visible: false
form:
    name: contact-form
    action: /contact-me/form
debugger: false
cache_enable: false
---

form.lt.md

---
title: Form
visible: false
form:
    name: contact-form
    action: /susisiekime/form
debugger: false
cache_enable: false
---

frontmatter.yaml

form:
  name: contact-form
  action: '/contact-us/form'
  client_side_validation: false
  inline_errors: true
  keep_alive: true

  fields:
    -
      id: contact-name
      name: name
      label: CONTACT_FORM.NAME
      placeholder: CONTACT_FORM.NAME_PLACEHOLDER
      autocomplete: true
      type: text
      validate:
        required: true

  buttons:
    -
      type: submit
      value: CONTACT_FORM.BUTTON.SUBMIT
      classes: "btn-primary"
    -
      type: reset
      value: CONTACT_FORM.BUTTON.RESET
      classes: "btn-secondary"

  process:
    -
      message: CONTACT_FORM.THANK_YOU
    -
      reset: true

Try this and it works. But remember that redirect option? All it does, is redirect folder based URL to a slug based. And after that redirect it fails to load the form. Already in contact-us.html.twig the forms() function can’t find the form neither by route nor by name

BTW, I noticed also if I define form action as a folder, on submit it tries to redirect, but redirected request loses all the payload and form is not submitted. Not sure if this is related, but that’s why I needed to define separate action for each language

@Karmalakas, You probably have a good reason to use it, would you mind explaining what the purpose is of the following setting:

pages:
  redirect_default_route: true

As I said, this redirects pages to correct slug based URLs instead of leaving users hanging on folder based URL. And I still couldn’t find a way to make pages.find('/folder').route work in modules - pages is null there. And the only way to get slug based URL I could find, is .route() method. Also langSwitcher generates folder based URLs, which are not good to show to user. And I guess redirects are also pretty bad for SEO. I hope you can see a loop here :slight_smile:

Need redirect, because can’t get slug URLs. Need slug URLs, because of multilanguage. Can’t make forms work in multilanguage, because of redirects.

I also hope to sort this out until I publish my personal page I’m working on and I thought it would take much longer, but Grav actually makes it so simple when you get how templates and blueprints work :slight_smile:

@Karmalakas, Ahh, I just remember a post dating back a few years: How to get the slug url using the LangSwitcher plugin? .

I responded back then because I had created my own Language-Switcher some time before to tackle the slug issue of LangSwitcher and also its generated hreflangs.

My (private) plugin does not require any redirect, but does need to fetch more pages internally to get the correct translated slug of its ancestors.

Failing Forms because of the redirect were not part of the discussion.

I guess it’s time for a proper GitHub issue :confused: This one situation revealed a whole bunch of problems under the hood with all the slug URLs, missing objects in modules and missing payloads or forms after redirects :frowning: Wonder how soon this can be fixed, because it’s the core elements of any website I think. Just getting slug URLs would open the door for workarounds at least…

Yeah… I’ll create an issue on GH later

Created an issue on GitHub. Hopefully I managed to explain it clear enough :slight_smile:

@Karmalakas, To be honest… I’m afraid I couldn’t follow the into :innocent:

  • Your demo does not match your intro. They seem different stories.
  • The intro is quite long-winded and not succinct.
  • You didn’t make clear what the issue was you were trying to show with the demo.
    • What did you expect to see?
    • What are you seeing?
  • Your demo contains needles data/info.
  • Your demo contains issues.
    • The route for the two pages are: (See their slugs)
      • en: contact-me <---- not contact-us
      • lt: susisiekime
    • In template ‘contact-us.html.twig’ you use:
      {% include "form.html.twig" with {pageForm: forms({"route": '/contact-us/form'})} %}
    • English page: The route ‘/contact-us/form’ does not exist and should be ‘/contact-me/form’ (See slug)
      Change the route and form is shown.
    • Lituanian page: Route ‘/contact-us/form’ does not exist and should be ‘/susisiekime/form’
      Change the route and form is shown.
    • Ergo, the template fails because the form is defined in to different routes.
      The design of template ‘contact-us.html.twig’ is flawed.
    • To solve this, you could:
      • include form using forms('contact-form')
      • or include form using forms({'route': page.route ~ '/form' })
      • or move folder 03.contact-us/form to ‘pages/form’, then the form will be found for both languages using route: ‘/form’
  • After fixing above issues, the demo works as expected (my expectation).

Notes:

  • Also be aware that the action routes of the form is slug dependend.
  • cache_enable: false is not needed anywhere for the forms to display.

It might seem that way, but it’s all related. I just can’t describe one issue without mentioning another, because it loses context and the solution would be almost obvious, but that solution then raises another issue. As I said - a closed circle :frowning:


I thought I said it multiple times :confused: Want to see forms working without ruining other parts of the website. Now they just don’t :confused:


Route doesn’t - folder does. Changing it doesn’t change the behavior in any way - there’s still a redirect when you try to get to that page and form is gone. As I replied on GH, Grav handles folder based URLs fine, but it also redirects. So basically it is capable of finding pages based on folder path. And just hardcoding a slug route there of one language if page is opened in another would be the same as hardcoding folder path - Grav would still find it.


It doesn’t fail if there’s no redirect. After redirect, no matter which approach you choose, even with hardcoded values, forms() is not finding any form. That’s not a flawed template.


That why I have them separate. But again, turning off redirect and leaving one action - /contact-us/form - works fine in both languages. No redirect - no trouble.


It’s a leftover when I was testing all the variations of pages. Yes, it doesn’t change anything.

@Karmalakas, Updated previous post by adding another alternative to include form:

forms({ 'route': page.route ~ '/form')

Nope… Not working either. But why would it? It’s the same as

forms({'route': '/susisiekime/form'})

No matter what you throw at forms(), it just simply won’t find any form.

But anyway, it’s not by accident I have this folder structure - I need to include this form anywhere on any page, so page.route approach wouldn’t fit anyway :frowning:

Created a draft PR, which might be a start. @anon76427325, would you mind to take a look at it and give some ideas? And BTW, are you related to Grav? I mean are you a dev there or just an enthusiast? :slight_smile:

Created another PR to Forms plugin, that partially fixes missing form issue, but it still won’t work if form is searched by name, because $this->forms is empty there. Still hope this will get approved, because it lets me find forms by page route while I’m on another page. And form is now found after redirect.