You are here

function mailchimp_get_lists in Mailchimp 8

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

Returns all Mailchimp lists for a given key. Lists are stored in the cache.

Parameters

array $list_ids: An array of list IDs to filter the results by.

bool $reset: Force a cache reset.

Return value

array An array of list data arrays.

10 calls to mailchimp_get_lists()
MailchimpCampaignForm::form in modules/mailchimp_campaign/src/Form/MailchimpCampaignForm.php
Gets the actual form array to be built.
MailchimpListsClearCacheForm::submitForm in modules/mailchimp_lists/src/Form/MailchimpListsClearCacheForm.php
Form submission handler.
MailchimpListsController::overview in modules/mailchimp_lists/src/Controller/MailchimpListsController.php
MailchimpListsSubscription::storageSettingsForm in modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php
Returns a form for the storage-level settings.
MailchimpListsTest::testMultiListRetrieval in modules/mailchimp_lists/src/Tests/MailchimpListsTest.php
Tests retrieval of a specific set of lists.

... See full list

File

./mailchimp.module, line 141
Mailchimp module.

Code

function mailchimp_get_lists(array $list_ids = [], $reset = FALSE) {
  $lists = [];
  $cache = \Drupal::cache('mailchimp');
  $cached_data = $reset ? NULL : $cache
    ->get('lists');

  // Return cached lists.
  if ($cached_data) {
    $lists = $cached_data->data;
  }
  else {
    try {

      /* @var \Mailchimp\MailchimpLists $mcapi */
      $mcapi = mailchimp_get_api_object('MailchimpLists');
      if ($mcapi != NULL) {
        $result = $mcapi
          ->getLists([
          'count' => 500,
        ]);
        if ($result->total_items > 0) {
          foreach ($result->lists as $list) {
            $int_category_data = $mcapi
              ->getInterestCategories($list->id, [
              'count' => 500,
            ]);
            if ($int_category_data->total_items > 0) {
              $list->intgroups = [];
              foreach ($int_category_data->categories as $interest_category) {
                $interest_data = $mcapi
                  ->getInterests($list->id, $interest_category->id, [
                  'count' => 500,
                ]);
                if ($interest_data->total_items > 0) {
                  $interest_category->interests = $interest_data->interests;
                }
                $list->intgroups[] = $interest_category;
              }
            }
            $lists[$list->id] = $list;

            // Append mergefields:
            $mergefields = $mcapi
              ->getMergeFields($list->id, [
              'count' => 500,
            ]);
            if ($mergefields->total_items > 0) {
              $lists[$list->id]->mergevars = $mergefields->merge_fields;
            }
          }
        }
        uasort($lists, '_mailchimp_list_cmp');
        $cache
          ->set('lists', $lists);
      }
    } catch (\Exception $e) {
      \Drupal::logger('mailchimp')
        ->error('An error occurred requesting list information from Mailchimp. "{message}"', [
        'message' => $e
          ->getMessage(),
      ]);
    }
  }

  // Filter by given IDs.
  if (!empty($list_ids)) {
    $filtered_lists = [];
    foreach ($list_ids as $id) {
      if (array_key_exists($id, $lists)) {
        $filtered_lists[$id] = $lists[$id];
      }
    }
    return $filtered_lists;
  }
  else {
    return $lists;
  }
}