You are here

function mailchimp_events_list_member_events in Mailchimp 2.x

Get a list of all events for a given email in a given list.

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.

int $count: The number of records to return up to 1000 from newest to oldest.

int $offset: Used for pagination, the number of records from a collection to skip.

array $fields: An array of fields to return, like 'events.name', or 'events.value'.

array $exclude_fields: An array of fields to exclude, like 'events.name', or 'events.value'.

Return value

false|object The body of the response from Mailchimp as an object.

1 call to mailchimp_events_list_member_events()
ListMailchimpEventsForMember::submitForm in modules/mailchimp_events/modules/mailchimp_events_example/src/Form/ListMailchimpEventsForMember.php
Form submission handler.

File

modules/mailchimp_events/mailchimp_events.module, line 58
Contains mailchimp_events.module.

Code

function mailchimp_events_list_member_events($list_id, $email, $count = 10, $offset = 0, array $fields = [], array $exclude_fields = []) {
  if (empty($email)) {
    \Drupal::logger('mailchimp_events')
      ->error('Attempted to find events on an empty email.');
    return FALSE;
  }
  if (empty($list_id)) {
    \Drupal::logger('mailchimp_events')
      ->error('Attempted to find events on an email without an audience.');
    return FALSE;
  }
  $cache = \Drupal::cache('mailchimp');
  $cached_data = FALSE;

  // Only cache the results if this is getting all results.
  $can_cache = FALSE;
  if (empty($offset) && empty($fields) && empty($excluded_fields)) {
    $can_cache = TRUE;
    $cached_data = $cache
      ->get($list_id . '-' . $email . '-events');
  }
  if ($cached_data) {
    $results = $cached_data->data;
  }
  else {
    try {

      /** @var \Mailchimp\MailchimpLists $mcapi */
      $mcapi = mailchimp_get_api_object('MailchimpLists');
      $options = [];
      if ($count != 10) {
        $options['count'] = $count;
      }
      if ($offset) {
        $options['offset'] = $offset;
      }
      if (!empty($fields)) {
        $options['fields'] = implode(',', $fields);
      }
      if (!empty($exclude_fields)) {
        $options['exclude_fields'] = implode(',', $exclude_fields);
      }
      $results = $mcapi
        ->getMemberEvents($list_id, $email, $options);
      if ($can_cache) {
        $cache
          ->set($list_id . '-' . $email . '-events', $results);
      }
    } catch (\Exception $e) {
      \Drupal::logger('mailchimp')
        ->error('An error occurred pulling member event info for {person}. "{message}"', [
        'person' => $email,
        'message' => $e
          ->getMessage(),
      ]);
      return FALSE;
    }
  }
  return $results;
}