Hi Community,

After testing the Drupal Mailchimp Signup Plugin (everything works :-) ) I thought it would be a much nicer User Experience if the Form would be send via Ajax and maybe a div with the response text appears (slides down/fade in/etc.)

Maybe a default ajax version which gracefully fall back to normal Request.

CommentFileSizeAuthor
#23 interdiff-2721249-21-23.txt509 bytessamuel.mortenson
#23 2721249-23.patch5.29 KBsamuel.mortenson
#21 2721249-21.patch5.29 KBsamuel.mortenson
#21 interdiff-2721249-19-21.txt2.55 KBsamuel.mortenson
#19 mailchimp-ajax_submit-2721249-19.patch4.67 KBtostinni
#16 mailchimp-ajax_submit-2721249-16.patch4.61 KBbasvredeling
#15 interdiff.txt2.07 KBbasvredeling
#15 mailchimp-ajax_submit-2721249-15.patch4.64 KBbasvredeling
#14 mailchimp-2721249-14-ajax-submit.patch4.54 KBnicola85
#13 mailchimp-2721249-13-ajax-submit.patch4.46 KBtim-diels
#12 mailchimp-2721249-12-ajax-submit.patch4.46 KBtim-diels
#11 mailchimp-2721249-11-ajax-submit.patch4.39 KBartamiel
#10 mailchimp-2721249-10-ajax-submit.patch4.42 KBtim-diels
#8 mailchimp-2721249-8-ajax-submit.patch4.39 KBartamiel
#7 mailchimp-2721249-7-ajax-submit.patch4.58 KBartamiel
#5 mailchimp-2721249-5-ajax-submit.patch3.94 KBakoe
#4 mailchimp-2721249-4-ajax-submit.patch3.27 KBakoe
#3 mailchimp-2721249-3-7.x-4.x-ajax-mailchim-sign-up-block.patch3.28 KBAlan D.
#3 mailchimp-ajax-with-error-handlng.png22.73 KBAlan D.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bambamboole created an issue. See original summary.

asrob’s picture

Alan D.’s picture

Totally worth it if you find the time.

I hope that drupal_set_message() is gone in D8 when the list is unavailable, and error response support has been added.

i.e. This always causes issues on live sites.

  if (empty($lists)) {
    drupal_set_message('The subscription service is currently unavailable. Please try again later.', 'warning');
  }

and 'There was a problem with your newsletter signup to %list.' is meaningless by default.

The ltrim($result->message, '0123456789: ') trims off the "400: " prefix off the error message, and I am guessing there are other response codes sent back.

      if (!empty($result->message)) {
        drupal_set_message(t('There was a problem with your newsletter signup to %list. The error returned was: %error', array(
          '%list' => $list_details[$list_id]->name,
          '%error' => ltrim($result->message, '0123456789: '),
        )), 'warning');
      }
      else {
        drupal_set_message(t('There was a problem with your newsletter signup to %list.', array(
          '%list' => $list_details[$list_id]->name,
        )), 'warning');
      }

I have attached a 7.x-4.x version I quickly hacked up for a client. It just returns the message, but you could optionally replace the entire form rather than just adding the message. Success shows the std confirmation message.

akoe’s picture

I took a new attempt to ajaxify Mailchimp form submit with this draft patch. A config variables is added to the signup form settings and used during form build.
The ajax submit handler introduced is still very raw and more result/feedback in the form would help to give more feedback in the ajax submit handler.

akoe’s picture

Reworked the ajax response a little to use core's ajax commands and response with error display.

tim-diels’s picture

Status: Active » Reviewed & tested by the community

Tested latest patch and works very good! Would love to see this committed.

artamiel’s picture

The latest patch works really good for me as well, but I was facing a slightly different problem regarding the returned response.
In this case whether the form was submitted correctly or some validation error was returned, the response would simply replace the form entirely, thus the user needs to refresh the page once again.

So I simply tweaked the patch above by adding an additional empty element/container between the description field and the form itself and used it instead as a target element, so that the response gets printed in there and my form would still be intact.

artamiel’s picture

Well, in our case we needed the status messages visible all the time, without having to replace the form. So, I ended up removing the functionality regarding replacing the form with status messages and simply use the newly added response container as a replacement for any status messages returned.
Now, we are able to freely modify in our custom template, the display order of the description, response and form.

Yasser Samman’s picture

The patch #8 is working great for me.

tim-diels’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
4.42 KB

After testing the latest patch, I had a problem that the page jumps to another form because of the autorefocus in ajax.
I provide a new patch with adding disable-autorefocus to TRUE to stop this behavior.

artamiel’s picture

I am re-uploading a tiny fix for the latest patch I've added (#8).
There was a wrong render key, that was left out in the original patch (#5) that I was basing my work on.
Instead of #postfix, it should've been #suffix and this was causing a break in the markup.

tim-diels’s picture

After using the AJAX submit a longer time, i faced an issue with the submit handler. When other forms are having the same ID on the submit button, the AJAX submit of Mailchimp is causing the other form to also use AJAX when they are not configured to act likt that.
This can be fixed by having the Mailchimp submit button to have it's own ID. Patch provided and started from #11 as this also fixes the issue of the auto-refocus.

tim-diels’s picture

nicola85’s picture

basvredeling’s picture

The patch from #14 works mostly as expected. However, I changed some comments and added a class to the form to signify a posted status. This allows you to visually hide the form once it has been submitted.

basvredeling’s picture

The last submitted patch, 15: mailchimp-ajax_submit-2721249-15.patch, failed testing. View results

tim-diels’s picture

Latest patch is working on the 1.8 branch without any issues, so would love to see this in the module.

tostinni’s picture

The patch works fine with 1.10, however there is a notice when an old signup form has been created and not updated :
Notice: Undefined index: ajax_submit in Drupal\mailchimp_signup\Form\MailchimpSignupPageForm->buildForm() (line 90 of modules/contrib/mailchimp/modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php).

The updated patch solve this and fix the offset of the previous patch.

samuel.mortenson’s picture

Assigned: Unassigned » samuel.mortenson

Going to be testing and hopefully committing this soon.

samuel.mortenson’s picture

Here's a small tweak to clear form values if submission was successful, which mimics the behavior of the current form and should prevent double-submit.

Status: Needs review » Needs work

The last submitted patch, 21: 2721249-21.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

samuel.mortenson’s picture

Status: Needs work » Needs review
FileSize
5.29 KB
509 bytes

Fixed tests.

  • samuel.mortenson authored 5b4868a on 8.x-1.x
    Issue #2721249 by tim-diels, stanislav.nikolov, samuel.mortenson,...
samuel.mortenson’s picture

Assigned: samuel.mortenson » Unassigned
Status: Needs review » Fixed

Committed - thanks all!

I'm marking this as fixed since the patch was for D8, but if anyone has time to work on a 7.x-5.x backport please change the version of the issue to 7.x-5.x, upload your patch, and set the issue to "Needs review".

basvredeling’s picture

@samuel.morentson thanks for picking up older issues such as this!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.