Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mine-minelli opened this issue Apr 5, 2022 · 0 comments
Open

Comments

@mine-minelli
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant