You are here

function mailchimp_get_memberinfo in Mailchimp 8

Same name and namespace in other branches
  1. 7.5 mailchimp.module \mailchimp_get_memberinfo()
  2. 7.2 mailchimp.module \mailchimp_get_memberinfo()
  3. 7.3 mailchimp.module \mailchimp_get_memberinfo()
  4. 7.4 mailchimp.module \mailchimp_get_memberinfo()
  5. 2.x mailchimp.module \mailchimp_get_memberinfo()

Get the Mailchimp member info for a given email address and list.

Results are cached in the cache_mailchimp bin which is cleared by the Mailchimp web hooks system when needed.

Parameters

string $list_id: The Mailchimp list ID to get member info for.

string $email: The Mailchimp user email address to load member info for.

bool $reset: Set to TRUE if member info should not be loaded from cache.

Return value

object Member info object, empty if there is no valid info.

9 calls to mailchimp_get_memberinfo()
MailchimpListsSubscribeDefaultFormatter::viewElements in modules/mailchimp_lists/src/Plugin/Field/FieldFormatter/MailchimpListsSubscribeDefaultFormatter.php
Builds a renderable array for a field value.
MailchimpListsSubscriptionTest::testGetMemberInfo in modules/mailchimp_lists/src/Tests/MailchimpListsSubscriptionTest.php
Tests retrieval of member info for a list and email address.
MailchimpSignupPageForm::submitForm in modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php
Form submission handler.
MailchimpWebhookController::endpoint in src/Controller/MailchimpWebhookController.php
mailchimp_get_marketing_permissions in ./mailchimp.module
Get the marketing permissions for a subscribed member.

... See full list

File

./mailchimp.module, line 323
Mailchimp module.

Code

function mailchimp_get_memberinfo($list_id, $email, $reset = FALSE) {
  $cache = \Drupal::cache('mailchimp');
  if (!$reset) {
    $cached_data = $cache
      ->get($list_id . '-' . $email);
    if ($cached_data) {
      return $cached_data->data;
    }
  }

  // Query lists from the MCAPI and store in cache:
  $memberinfo = new stdClass();

  /* @var \Mailchimp\MailchimpLists $mc_lists */
  $mc_lists = mailchimp_get_api_object('MailchimpLists');
  try {
    if (!$mc_lists) {
      throw new Exception('Cannot get member info without Mailchimp API. Check API key has been entered.');
    }
    $result = $mc_lists
      ->getMemberInfo($list_id, $email);
    if (!empty($result->id)) {
      $memberinfo = $result;
      $cache
        ->set($list_id . '-' . $email, $memberinfo);
    }
  } catch (\Exception $e) {

    // A 404 exception code means mailchimp does not have subscription
    // information for given email address. This is not an error and we can
    // cache this information.
    if ($e
      ->getCode() == 404) {
      $cache
        ->set($list_id . '-' . $email, $memberinfo);
    }
    else {
      \Drupal::logger('mailchimp')
        ->error('An error occurred requesting memberinfo for {email} in list {list}. "{message}"', [
        'email' => $email,
        'list' => $list_id,
        'message' => $e
          ->getMessage(),
      ]);
    }
  }
  return $memberinfo;
}