Hi,

I'm building a dashboard for logged in users. One of the things I'd like to show there is their subscription status for a specific list. Is this possible with Views? I can't find an option within the 'Mailchimp Lists' type view?

Thanks a lot.
Remko

Comments

jsibley’s picture

Issue summary: View changes

This dates from November 2012 and has had no response at all.

It would be helpful to make available to views a way to show whether a user is subscribed to a list.

As of now, I don't see anything from mailchimp to create a relationship from a user view.

Is this information stored within Drupal at all or is it only available through API calls?

mstrelan’s picture

As far as I can tell this is because the subscription data is not actually stored in the field data table, but stored in the cache_mailchimp table. The relevant cid would be [listid]-[user:mail], but you can't really query the cached data, only the presence of the cache entry. An alternative would be to use a computed field or manually sync to a hidden field in a custom module and filter on that.

michaellenahan’s picture

Following up from @mstrelan's comment, here's what I did to solve this in my case.

In my case, I have two fields on the user profile: a mailchimp subscription field (field_profile_mailchimp) and an ordinary Drupal drop-down yes/no field (field_profile_newsletter).

I use hook_mailchimp_process_webhook() to update this field whenever the user unsubscribes using the mailchimp unsubscribe link.

/**
 * Implements hook_mailchimp_process_webhook().
 */
function MODULENAME_mailchimp_process_webhook($type, $data) {
  if ($type == 'unsubscribe' && $data['action'] == 'unsub') {
    // A user has unsubscribed from the mailing list.
    // Set field_profile_newsletter value to 'no'.
    $mail = $data['email'];
    $account = user_load_by_mail($mail);
    $profile = profile2_load_by_user($account);
    $profile->field_profile_newsletter[LANGUAGE_UNDEFINED][0]['value'] = 0;
    profile2_save($profile);
  }
}

In the profile itself, we:
- Detect if the mailchimp field has changed, and update the drupal field accordingly
- Hide the actual mailchimp subscription field

/**
 * Implements hook_form_profile2_form_alter().
 */
function f_user_account_form_profile2_form_alter(&$form, &$form_state, $form_id) {
  // The mailchimp subscription field tracks the subscription status of the mailchimp list.
  // We synchronize this with field_profile_newsletter.
  // Why do this? Because mailchimp subscription form fields are not available
  // to views. @see: https://www.drupal.org/node/1830630
  if ($form['profile_main']['field_profile_mailchimp'][LANGUAGE_NONE][0]['subscribe']['#default_value']) {
    $form['profile_main']['field_profile_newsletter'][LANGUAGE_NONE]['#default_value'][0] = 1;
  }
  else {
    $form['profile_main']['field_profile_newsletter'][LANGUAGE_NONE]['#default_value'][0] = 0;
  }
  // Hide the actual mailchimp field.
  $form['profile_main']['field_profile_mailchimp']['#access'] = FALSE;
jlarrubia’s picture

I had the same problem. Solution in #3 is good but not consider every case. People can be suscribed/unsuscribed using different ways, not only your Drupal website or the unsuscribe link.

Following up #3, I solved in this way:

1. Configuring my webhook (admin Mailchimp) to send updates when a change is made:
- by a subscriber
- by an account admin
- via the API

So, I can be sure any change in my list will be notified.

2. Modifying the mailchimp_process_webhook:

/**
 * Implements hook_mailchimp_process_webhook().
 */
function MODULENAME_mailchimp_process_webhook($type, $data) {
  
  if (isset($data['email'])) {
    $account = user_load_by_mail($data['email']);
     // Check if the suscriber is an actual user
     if ($account->uid) {
       // A user has subscribed from a mailing list.
       if ($type == 'subscribe') {
        $account->field_profile_newsletter['und'][0]['value'] = 1;
        user_save($account);
        watchdog('Mailchimp', $account->mail . ' has been suscribed in the list ' . $data['list_id']);
      }
      // A user has unsubscribed or has been cleaned from a mailing list
      elseif ($type == 'unsubscribe' || $type == 'cleaned') {
        $account->field_profile_newsletter['und'][0]['value'] = 0;
        user_save($account);
        watchdog('Mailchimp', $account->mail . ' has been suscribed in the list ' . $data['list_id']);
      }
    }
  }
  // A suscriber has changed his email
  elseif ($type == 'upemail') {
    $account_old = user_load_by_mail($data['old_email']);
    // Check if the old suscriber is an actual user
    if ($account_old->uid) {
      $account_old->field_profile_newsletter['und'][0]['value'] = 0;
      user_save($account_old);
      watchdog('Mailchimp', $account_old->mail . ' has been suscribed in the list ' . $data['list_id']);
    }
    $account_new = user_load_by_mail($data['new_email']);
    // Check if the new suscriber is an actual user
    if ($account_new->uid) {
      $account_new->field_profile_newsletter['und'][0]['value'] = 1;
      user_save($account_new);
      watchdog('Mailchimp', $account_new->mail . ' has been suscribed in the list ' . $data['list_id']);
    }
  }
}

3. Hiding the ordinary Drupal drop-down yes/no field ('field_profile_newsletter'). If a user changes his suscription by the form of the mailchimp field ('field_profile_mailchimp'), the field 'field_profile_newsletter' will be updated by the previous hook (step 2).

4. hook_form_profile2_form_alter is not necessary

If you have several lists, you should check the list which fired the webhook and change the corresponding field.

amytswan’s picture

Status: Active » Closed (won't fix)

“And now our watch [for support of the 7.x-2.x version of the MailChimp module] has ended…” With the approaching deprecation of MailChimp’s API version 2.0, I’m sad to say we too must turn the page. This branch will become unsupported in early October and officially deprecated by the end of this year (2016).

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. If you find this issue still exists on either the 7.x-4.x or 8.x branches, let us know by opening a new ticket. “What is dead may never die, but rises again, harder and stronger!”

Marja Kuijsten’s picture

Looking for this to work with 7.x-4.x!