Global form

There are websites that I always have a contact form at the bottom of each pages. Sometimes I also need a newsletter form which is a popup and shows automatically when visitors scroll to the bottom of the page to ask them to subscribe to our newsletters.

For these cases, in each markdown file I define a form. It works as always but if I want to change something, I need to make the change in all of these pages, this is just a search-and-replace with code editor, it is easy for me, but not really user friendly with the clients who use Admin plugin.

I wonder if there is a way to define 1 single form for Form plugin and use it in the whole site. This is similar to Joomla! or WordPress, you create a form in WordPress plugin or create a Joomla form module and you show them any where you want, if you want to change something you just need to change it the module/plugin’s settings.

Figured it out. I store the form in an unpublished page and get the form like this:

{% set form = grav.page.find('/myform').header.form %}
---

Genius! I never even thought of that approach :slight_smile:

The form is displayed but its data is not saved because the form object doesn’t exist in the current page. So the form plugin doesn’t process, as in its code it only process if the header has a form object:

        $header = $page->header();
        if (isset($header->form) && is_array($header->form)) {
        // Save the submission.
        }

To fix this I need to manually add the form object to the current page’s header by using a plugin:

    public function onPageInitialized()
    {
        $currentPage = $this->grav['page'];
        $formPage = $this->grav['pages']->find('/myform');

        if ($formPage && $formPage->header()->form)
        {
            $currentPage->modifyHeader('form', $formPage->header()->form);
        }
    }

This ways the form is available in the headers of all the pages.

Oops. the code above should be

if ($formPage && isset($formPage->header()->form))
---

I have some notes to improve forms plugin to allow for multiple forms per page and also to be able to include forms from other pages in current page. Needs some fundamental changes in how it works in order for this to happen in a flexible and reliable way though.

Il was able to do something equivalent by adding an iframe in the template of the pages that need to include the form. The form template is stripped from the header and footer, and contains stricltly the form html. To know from which page the form was sent from, I added a hidden field in the form that is automatically filled by Javascript depending on a parameter sent to the form page. The pages including the form have this parameter sent in the iframe url. The advantage of it is not to have to writte a plugin. It has one problem though : the iframe height. The easy solution : a fixed iframe height has to be set for all the page resolutions in the css file accordingly. The tad more compicated one : as the iframe domain will be the same then the page calling it, we can set a javascript function that will send the form page height in parameter to a function in the parent page that will resize the iframe accordingly.

BTW Forms 2.0 has been released with globally-accessible as well as multiple-forms-per-page.

It works beautifully! Grav doesn’t cease to amaze me. Thank you so much for all the effort put into the best CMS I ever worked with!

@rhukster Do you have any examples how to conclude form from .md file. And how to conclude it by twig.Could you share these. I did not found good solutions.

Should be everything you need in the docs