Grav plugin form submit (Mailchimp)

I’m trying to create a plugin to add email submitted via the newsletter form to the Mailchimp list. My question is on what event should it be initialised? I know there’s this list of events available, but it’s not clear to me at what point it should be initiated?

1 Like

If you don’t want it to interact with any other actions, use onPluginsInitialized

1 Like

Thank for your advice. The plugin is starting to do what it supposed to. However, currently I have a different problem: I’m trying to tweak AJAX call, so it prevents page refresh and handle messages.
The code below was working before I’ve created calls to MailChimp API.

Currently I’m receiving this warning:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user’s experience. For more help http://xhr.spec.whatwg.org/

Any idea what’s wrong and how to fix it?

$(document).ready(function() {
const newsletterForm = $('.js-form-newsletter');
newsletterForm.submit(function(e) {
    // prevent form submission
    e.preventDefault();

    // submit the form via Ajax
    $.ajax({
        url: newsletterForm.attr('action') + '?submit_newsletter=1',
        type: newsletterForm.attr('method'),
        dataType: 'html',
        data: newsletterForm.serialize(),
        success: function(result) {
            $('#newsletter-form-result').html(result);
            if ($('#newsletter-form-result div.alert.notices').hasClass('green')) {
                $('.newsletter_form--hide-js').hide();
                $('.newsletter__info-text').hide();
            }
        }
    });
});
});

That is because you most probably want to make a cross origin ajax call (that means: url / domain of the call is a domain different from you.

Look at https://api.jquery.com/jQuery.ajax/ in the section “async”.

Would love to see this in action. I wrote a custom plugin back in laravel and it worked swimmingly but haven’t attempted in grav yet :slight_smile:

I have wrote a plugin that is functional, but not yet ready for release to the public. I still need to write the twig, css, js to accompany this.

Hoping to get it done soon! I’m writing it to support multiple services, not just mailchimp.