You are here

function mailchimp_get_campaign_data in Mailchimp 8

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

Wrapper function to return data for a given campaign.

Data is stored in the Mailchimp cache.

Parameters

string $campaign_id: The ID of the campaign to get data for.

bool $reset: Set to TRUE if campaign data should not be loaded from cache.

Return value

mixed Array of campaign data or FALSE if not found.

1 call to mailchimp_get_campaign_data()
MailchimpCampaignTest::testGetCampaign in modules/mailchimp_campaign/src/Tests/MailchimpCampaignTest.php
Tests retrieval of a specific campaign.

File

./mailchimp.module, line 883
Mailchimp module.

Code

function mailchimp_get_campaign_data($campaign_id, $reset = FALSE) {
  $cache = \Drupal::cache('mailchimp');
  $campaign_data = FALSE;
  if (!$reset) {
    $cached_data = $cache
      ->get('campaign_' . $campaign_id);
    if ($cached_data) {
      return $cached_data->data;
    }
  }
  try {

    /* @var \Mailchimp\MailchimpCampaigns $mcapi */
    $mcapi = mailchimp_get_api_object('MailchimpCampaigns');
    $response = $mcapi
      ->getCampaign($campaign_id);
    if (!empty($response->id)) {
      $campaign_data = $response;
      $cache
        ->set('campaign_' . $campaign_id, $campaign_data);
    }
  } catch (\Exception $e) {
    \Drupal::logger('mailchimp')
      ->error('An error occurred retrieving campaign data for {campaign}. "{message}"', [
      'campaign' => $campaign_id,
      'message' => $e
        ->getMessage(),
    ]);
  }
  return $campaign_data;
}