getgrav / getgrav/grav-plugin-form
Form generated in Twig no longer works after update to version 6.0.0
- Dominant language
- PHP
- Stars
- 64
- Forks
- 80
- Avg merge
- 10h 13m
- Merged PRs (30d)
- 9
Description
Hi all of you,
I'm working on a project where some form page gets generated in a template with Twig by load other to pages with forms and merge them in one.
Here the piece of code that doesn't work any more:
```
{# Form Types #}
{% set formtype = page.find( '/forms-type' ) %}
{% set forms_forms = forms.header.forms %}
{% set form_btm_name = DRAFT ? 'draft-edit-bottom' : 'edit-bottom' %}
{% set form_btm = forms( form_btm_name ) %}
{# Original Form and Merge #}
{% set form = forms( original_form_name ) %}
{% set submitted_data = form.data.toArray %}
{% set merged_data = page.header.crud_fields|merge(submitted_data) %}
{% do form.set('action', page.route) %}
{% do form.set('buttons', form_btm.buttons) %}
{% do form.set('process', form_btm.process) %}
{% do form.setAllData(merged_data) %}
```
So I tried to load and merge the two forms via plugin.
Because I already have a custom Plugin I made for loading the correct template for the page, I added some code in `onPageInitialized` function:
```
public function onPageInitialized(Event $event): void
{
…
$form = new Form($page, $name, $form_original[$name]);
// Dunno if this needed
$page->addForms( array( $form->name() => $form->doSerialize()) );
// This I copy from Login Plugin
$twig = $this->grav['twig'];
$twig->twig_vars['form'] = $form;
…
}
```
By doing this the Form is correctly display on the page, but the `process` form is ignored:
```
…
process:
- save:
filename: feedback.txt
body: "{% include 'forms/data.txt.twig' %}"
operation: add
- mine_crud: true
```
and Function `onFormProcessed` does not get called any more.
It is my first time to attempt some custom Plugin in Grav, so probabily I do not understand something in the last update CHANGELOG:
```
* Optimized form caching by not initializing the forms in `onPageProcessed` event anymore
* **BACKWARD COMPATIBILITY**: As form initialization has been delayed, logic relaying on `onPageProcessed` with forms may not work anymore
Fixed `FormPlugin::getForm()` to properly search the current page first
* Fixed `FormPlugin::getForm()` to ignore fallback if the page was given as parameter
```
Thank you for the help!
Contributor guide
No contributing guide indexed for this repository
Research direction
Start at the custom plugin's onPageInitialized function, the Form construction, and the assignments to page forms and twig_vars shown in the report. Compare that lifecycle with the FormPlugin changes described in the 6.0.0 changelog, then verify that the declared process runs and onFormProcessed is called when the merged form is submitted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100