Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Global form

Started by Muut Archive 10 years ago · 10 replies · 606 views
10 years ago

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.

10 years ago

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

TWIG

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

Genius! I never even thought of that approach :)

10 years ago

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:

PHP
        $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:

PHP
    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.

10 years ago

Oops. the code above should be

PHP

if ($formPage && isset($formPage->header()->form))
---
10 years ago

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.

10 years ago

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.

10 years ago

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

10 years ago

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!

10 years ago

@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.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1360 9 years ago
Archive · by Muut Archive, 9 years ago
2 938 9 years ago
Archive · by Muut Archive, 9 years ago
2 4068 9 years ago
Archive · by Muut Archive, 9 years ago
1 2958 9 years ago
Archive · by Muut Archive, 9 years ago
3 1122 9 years ago