Skip to content

How I made GROUPINGS in Mailchimp play nicely with Drupal form input

Perhaps this is not the most elegant solution, but it works for me and was pretty great when I figured it out.

The Form
I wrote out the checkboxes how I wanted them to appear:
…form stuff…
//Create the options
  $options = array(
    'email1' => t('Email 1'),
    'email2' => t('Email 2'),
    'email3' => t('Email 3'),
    'email4' => t('Email 4')
  );
  $form['subscriptions'] = array(
    '#title' => t(''),
    '#type' => 'checkboxes',
    '#options' => $options, //pass them into the form element
    '#default_value' => array('email1', 'email1'), // #default_value references an array of keys
    '#weight' => 25
  );
…form stuff….
  return $form;

Validate the Form in Drupal
function module_signup_form_validate ($form_id, $form_values){
  $fname = trim($form_values['values']['fname']);
  $lname = trim($form_values['values']['lname']);
  $email = trim($form_values['values']['email']);
  $title = trim($form_values['values']['title']);
  $affiliation = trim($form_values['values']['affiliation']);
  $subscriptions = array_filter($form_values['values']['subscriptions']); //running array_filter creates an array of only the checked boxes. Makes the next function easier.
 // print_r($subscriptions); used to test the output
 
  if( strlen($email) == 0 || !check_email_address($email)) {
    form_set_error('email', t('Please fill in a valid Email Address to sign up for the Newsletter.'));
    return;
  }
 
  $result= module_MailChimp_subscribe($fname, $lname, $email, $subscriptions, $title, $affiliation);
}

Pass it to the Mailchimp Function
Note, there's a Mailchimp module, but I'm not sure about robust groupings support. Read more about Mailchimp's listSubscribe( )

I used MailChimp's convenient PHP kit (http://www.mailchimp.com/api/downloads/) modifying for my needs.

Note: most of this is straight-up from Mailchimp, so I'm going to cut some out for brevity's sake. Put my comments in italics

function module_MailChimp_subscribe($firstName, $lastName, $email, $subscriptions, $title = null, $affiliation = null){

require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey

$api = new MCAPI($apikey);

// for the array $groups, add each item if it is checked. Since we used array_filter, we can just check if a key exists, and if it does, add it to the array.
if ($subscriptions['email1'])
    $groups .= 'Email 1,'; //Note the comma at the end of the item … this is how groupings are passed ('Item1,Item2')
if  ($subscriptions['email2'])
    $groups .= 'Email 2,';
if  ($subscriptions['email3'])
    $groups .= 'Email 3,';
if  ($subscriptions['email4'])
    $groups .= 'Email 4';

//To understand below, see listSubscribe( ) in Mailchimp.
$groupings = array(
        'Subscriptions' => array(
            'name' => 'Subscriptions',
            'groups' => $groups,
        )
    );

//Form the $merge_vars array

$merge_vars = array('FNAME'=>$firstName, 'LNAME'=>$lastName, 'GROUPINGS' => $groupings, 'MMERGE4' => $title, 'MMERGE5' => $affiliation);

$retval = $api->listSubscribe($listId, $email, $merge_vars);

if ($api->errorCode){
    echo "Unable to load listSubscribe()!n";
    echo "tCode=".$api->errorCode."n";
    echo "tMsg=".$api->errorMessage."n";
} else {
  //  echo "Returned: ".$retval."n"; Hide the returned value for production code, otherwise your user will see "Returned 1"
}
}

Closing
So, yeah, lots here, but maybe this will help someone else when they're googling "Drupal Mailchimp Groups Groupings OMG." Also, the Mailchimp API people are great and always willing to answer questions.

2 Replies to “How I made GROUPINGS in Mailchimp play nicely with Drupal form input”

  1. Hi there, this is how I got Groupings to work;

    given an array of $groups = array(“General”, “Politics”, “Sport”);

    foreach($groups as $group)
    $groupings[] = array(“id”=>1234, “groups”=>$group);

    $vars[“GROUPINGS”] = $groupings;

    Then it doesn’t have to match on a “name” and therefore won’t break if the name changes.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.