getgrav / getgrav/grav-plugin-form
Slug based URLs, forms and multi-language
- Dominant language
- PHP
- Stars
- 64
- Forks
- 80
- Avg merge
- 10h 13m
- Merged PRs (30d)
- 9
Description
So I'm actually not sure how to explain the issue very clearly, but I'll try
Grav 1.7.3 + Form 4.3.0 plugin. All freshly installed
Basically, trying to implement one feature on website (building personal page from scratch) encountered an issue, which uncovered a bunch of problems with Gravs core and how in handles URLs, routes, redirects and pages.
First: the only way to get slug based URL, is `page.route()` (at least that's what I figured), but that's not always an option, because sometimes you want to add a link just from some saved [pages field](https://learn.getgrav.org/17/forms/blueprints/fields-available#pages-field), which saves slug value [without language code](https://github.com/getgrav/grav-plugin-admin/issues/2021). That wouldn't be a problem in a normal page, because we can just `pages.find([SLUG]).route` or `pages.find([FOLDER]).route`, but there's a problem - `pages === null` in modules of modular pages.
OK, we can't get to `page.route`, then lets try redirects. Yes, even though they are one of the worst enemies of SEO, but still - we don't have much of a choice. When in _system.yaml_ you have `pages.redirect_default_route: true`, when you get to folder based URL, this setting enabled, redirects to correct slug based URL. OK it seems we have a really dirty workaround, BUT...
After redirect, `forms()` will return `null` no matter how you try to get the form - by name or by route. And only cache clearing helps. But it helps only if you have a single language page and won't navigate to folder based URLs (so that redirect wouldn't happen). If you have at least two languages and want to switch it on page with a form, again there's no easy way to get slug URL for same page in different language and you have to use whatever URL you can get (most likely folder based URL). After clicking on new language redirect happens and form is gone.
Same applies if you want to implement AJAX form and have different slugs than folder name. You can't use common _frontmatter.yaml_ for all languages, because you need to have separate actions for form in each language depending on slug, because if you use folder based action URL, after submit it redirects to correct slug based URL and whole payload is gone - form never gets submitted.
It's kind of a closed loop
You need to have correct (slug based) URLs to be somewhat good to SEO. There's no way to get slug URLs, because you can't access `page.route` in some situations (especially for different language). So you just use folder based URLs with redirects, but you can't use them either not only because of SEO, but because of redirects after which you lose form payload or lose forms all together.
I was already liking Grav and even considering a donation, but no no matter how easy it is to extend, I'm starting to find it pretty much unusable if you want to have beautiful multilingual SEO friendly slugs and at least a contact form.
BTW, missing forms issue dates [4 years back](https://discourse.getgrav.org/t/form-with-multilanguage/2833)
---
Here's my setup to reproduce the issue (copy from [discourse](https://discourse.getgrav.org/t/multilanguage-forms-missing-until-cache-is-cleared/14977), where I initially raised a question)
### In Quark templates add two files:
_fom.html.twig_
```twig
{% if pageForm %}
{% include "forms/form.html.twig" with {form: pageForm} %}
{% endif %}
```
_contact-us.html.twig_
```twig
{% extends 'partials/base.html.twig' %}
{% block content %}
{% include "form.html.twig" with {pageForm: forms({"route": '/contact-us/form'})} %}
{% endblock %}
```
### _System.yaml_
Uncomment redirect and navigate to folder based URL to reproduce the missing form issue
```yaml
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_
```md
---
title: 'Contact me'
slug: contact-me
cache_enable: false
---
##### Text
```
_contact-us.lt.md_
```md
---
title: Susisiekime
slug: susisiekime
cache_enable: false
---
##### Tekstas
```
### Folder _user/pages/03.contact-us/form_
_form.en.md_
```md
---
title: Form
visible: false
form:
name: contact-form
action: /contact-me/form
debugger: false
cache_enable: false
---
```
_form.lt.md_
```md
---
title: Form
visible: false
form:
name: contact-form
action: /susisiekime/form
debugger: false
cache_enable: false
---
```
_frontmatter.yaml_
```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
```
---
Hope this can be fixed before I need to publish my page :(
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.