1

I'm looking for code examples for creating custom mailchimp signup forms that use the mailchimp api integration provided by the mailchimp module.

Context: I have a D7 site using a standard embedded sign-up form for mailchimp. I just recently switched the site to https, which unfortunately causes some browsers (e.g. firefox) to throw a scary message when a user clicks the submit button on the sign-up form, because standard mailchimp embedded forms can only use http.

The mailchimp module uses the mailchimp API, which allows secure connections, so that's the obvious solution. And the mailchimp module automatically provides a block for each list. But the module makes assumptions that I don't want. So, instead of the automatic block provided by the mailchimp module, I'd like to create my own block(s) with a sign-up form that uses the api, so that I have more control over the look and feel of the form.

Also, I know the webform mailchimp module provides a mailchimp component for webform, (there's a good write up here) but I'd like to explore creating my own forms as a first step.

  • 1
    Just curious about the down vote. The help text for down voting says "does not show any research effort; it is unclear or not useful". For a question like this, am I expected to first try to code a solution myself, then post the code I'm trying to use (that presumably doesn't work)? Or is it OK to ask first if anyone has done it already? – Dave Bruns Dec 4 '13 at 17:03
7

In the end, this is what I did:

  1. Installed the mailchimp module and connected drupal to a list in mailchimp using normal instructions and an API key that mailchimp provides.

  2. Created a new custom module that creates and displays blocks using standard hook_block_info and hook_block_view code.

  3. Created a very simple form with hook_form, with a companion submit function, hook_form_submit, and add this form to the block(s) created above.

  4. In hook_form_submit, I open a connection to the mailchimp api with:

    $q = mailchimp_get_api_object();

Then subscribe the user with:

$success = $q->listSubscribe($list_id, $email, $merge_vars, 'html', TRUE, TRUE);

The above functions are documented in the mailchimp api.

So, in the end, I'm not really using the Drupal mailchimp module for anything other than providing access to mailchimp api over a secure connection (the drupal site is running https). The main advantage of this approach is complete control over the look and feel of the forms that appear in the blocks, and the actions taken when a user submits.

Another advantage: Because I'm using hook_form_submit to control processing once the form is submitted, I can add the form location (using request_path()) to the merge variables that are passed back to mailchimp. That way, the form is the same on every page that it appears, but the location is passed back to mailchimp as part of the sign-up process.

0

I did something similar to @Dave Bruns in my hook_form_submit but added in the mailchimp_addto_queue function so this gets triggered when cron runs.

// Connect to MailChimp API.
$q = mailchimp_get_api_object();

// Send data to our Mailchimp list for this site.
$args = array(
  'list_id' => '91919191',
  'email' => $email,
  'merge_vars' => array('fname' => 'Lee', 'lname' => 'Woods'),
  'format' => 'html',
  'double_optin' => FALSE,
  'update_existing' => TRUE,
  'replace_interests' => TRUE,
  'confirm' => TRUE,
);
mailchimp_addto_queue('mailchimp_subscribe_process', $args);

mailchimp_subscribe_process($list_id, $email, $merge_vars, $double_optin, $format, $update_existing, $replace_interests, $confirm);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.