When it comes to building survey style forms in Drupal, you really can’t beat Webform. The module allows editors to create great looking forms directly within Drupal and the results can be exported out.
So for example, if you create a competition form using Webform, you could add a checkbox which allows users to subscribe to an email list on MailChimp directly from the form.
In this tutorial, I’ll show you how to add a “Subscribe to newsletter” checkbox on any form created using Webform.
Now before we continue, it’s important to note that Webform Mailchimp only works with MailChimp. You can’t use it for any other newsletter service. Also, I’m not aware of a similar module that can be used with Entityforms. If you know of a module, leave a comment below.
I’ll assume you’ve already setup Webform and you’re using the 3.x branch (7.x-3.19). We won’t look at how to setup Webform in this tutorial.
Now go download and install the following modules:
Because we’re using Webform 3.x, we’ll need to use the 7.x-1.x branch of Webform Mailchimp.
If you use Drush, run the following command:
drush dl mailchimp webform_mailchimp-7.x-1.0-rc2 libraries
Setup MailChimp Module
Webform Mailchimp uses the MailChimp module to integrate Drupal with the newsletter service. So before we can configure the form we need to setup MailChimp and make sure it’s working.
If you’ve already setup the MailChimp module, then you can skip this section. If not, keep reading.
Download MCAPI library
First, download the MCAPI library and place it in the libraries directory.
Once the library has been extracted, the path to the main php file should be:
Create MailChimp API key
Log in to your MailChimp account and go to the “Account Settings” page. Under Extras, click on API keys.
Click on the “Create A Key” button and grab the generated API key.
Setup MailChimp in Drupal
In your Drupal site, go to Configuration, MailChimp and paste the API key into the “Mailchimp API Key” text field.
At this point, you should have a basic setup of the MailChimp module. Now let’s look at how to integrate Webform and MailChimp.
How to Use Webform Mailchimp
The form I created using Webform is fairly simple, it only has three fields: Name, Email and Suggestions.
Now let’s add a checkbox to the form which allows users to subscribe to an email list.
1. Click on the Webform tab on an existing form and create a Mailchimp component (Webform field).
2. Scroll down to the “Choose list” drop-down list and select one.
Selecting a list is a requirement, if you can’t see any, then make sure you create one on the MailChimp website.
3. From the “Select an existing e-mail field, or create one” drop-down list, you can choose how you want to capture an email address.
Addresses can be captured in two ways: using its own text field or existing E-mail component. If you’re already capturing an email address on the form and want to display a checkbox, then select an existing email component from the drop-down list.
You can also change the checkbox label and the default state of the checkbox: checked or unchecked.
Now head over to the form and you should see a checkbox called “Subscribe to newsletter”
As of this writing, double opt-in is not supported by Webform Mailchimp out of the box. You’ll have to patch the module manually, click here for the issue.
Once you’ve applied the patch, you’ll get a “Double Opt In” checkbox when you edit the Mailchimp component.
The Webform Mailchimp module makes it extremely easy to capture email addresses directly from forms. If your website uses Webform a lot then this may end up being a great way to increase email subscribers. I’m sure the marketing department will appreciate it.
Here’s a list of similar modules for other newsletter services:
If you know of any other modules, leave a comment and I’ll add it to this list.