getgrav / grav-plugin-form

Grav Form Plugin
http://getgrav.org
MIT License
53 stars 79 forks source link

Form generated in Twig no longer works after update to version 6.0.0 #563

Open mine-minelli opened 2 years ago

mine-minelli commented 2 years ago

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!