We have a site with a significant userbase. About a quarter of these users opt into our lists (via Content Profile settings, not via Interest Groups).

With a few small changes, I think Mailchimp module can support allowing external modules to specify (via standard Drupal hooks) which users should be subscribed to which lists. These changes should not have any impact on existing functionality.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

xurizaemon’s picture

This patch to mailchimp.module makes two changes -

1) mailchimp_user() loads the required lists against the user in hook_user(). It maintains the existing behaviour of setting lists according to the assigned roles.

function mailchimp_user($op, &$edit, &$account, $category = NULL) {
  // attach the required lists to the $account value
  if ($op == 'load') {
    if (!isset($account->mailchimp_lists)) {
      $required_lists = _mailchimp_get_required_lists();
      foreach ($required_lists as $key => $list) {
        if ($roles = array_intersect_key($account->roles, $list->roles)) {
          $account->mailchimp_lists[$key] = $list ;
        }
      }
    }
  } 
/* ... rest of mailchimp_user() here ... */ 

2) mailchimp_cron() checks $account->mailchimp_lists instead of $account->roles.

/* ... in mailchimp_cron() ... */
          foreach ((array)$lists as $key => $list) {
            if (array_intersect_key((array)$lists, $account->mailchimp_lists)) {
              $lists[$key]->batch[] = _mailchimp_load_user_list_mergevars($row->uid, $list->id, $lists[$key]->listMergeVars);
              $is_allowed = TRUE; // user is allowed in this list
            }
            else {
              $lists[$key]->unsubscribe[] = $account->mail;
            }              
          } 
/* ... in mailchimp_cron() ... */ 

(array_intersect_key() is PHP5, if mailchimp doesn't already require php5 I can provide an alternative).

By utilising the user object, we allow other modules to interact with the user's subscribed lists in hook_user(), and we can do things like this.

/**
 * Implements hook_user().
 */
function mymodule_user($op, &$edit, &$account, $category = NULL) {
  switch ( $op ) {
    case 'load' :
      if (isset($account->mailchimp_lists) && !isset($account->mailchimp_lists_done)) {
        $required = FALSE;
        if (!isset($account->profile)) {
          $account->profile = content_profile_load('profile', $account->uid, '', TRUE);
        }
        foreach (array('field_profile_a', 'field_profile_b') as $fieldname) {
          if (is_array($account->profile->$fieldname) && $account->profile->{$fieldname}[0]['value'] == 'Y') {
            $required = TRUE;
          }
        }
        if (!$required) {
          $account->mailchimp_lists = array();
        }
        $account->mailchimp_lists_done = TRUE;
      }
      break ;
  }
} 

The above code is part of an external module for a site which uses content_profile, and has fields "content_profile_a" and "content_profile_b".

Having "Y" for either of these fields means the site user should be included in required list subscriptions; otherwise they are unsubscribed.

We set those lists to "required" for 'authenticated user' role, then decide based on properties of the user (or their profile) whether the user will be subscribed or not.

nrackleff’s picture

Component: Code » General
Issue summary: View changes
Status: Active » Closed (won't fix)

“And now our watch [for support of the 6.x version of the MailChimp module] has ended…” With the end of Drupal 6 support, I’m sad to say we too must turn the page.

Fret not! The 7.x-4.x and 8.x versions come highly recommended. Both are using Mailchimp’s new API 3.0 and are being actively maintained. “What is dead may never die, but rises again, harder and stronger!”