You are here

function mailchimp_campaign_list_templates in Mailchimp 8

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_list_templates()
  2. 7.3 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_list_templates()
  3. 7.4 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_list_templates()
  4. 2.x modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_list_templates()

Return all available user templates.

Parameters

bool $reset: True if templates should not be loaded from cache.

int $count: The number of templates to request from the API.

Return value

mixed Array listing existing Mailchimp templates by type.

3 calls to mailchimp_campaign_list_templates()
MailchimpCampaignController::overview in modules/mailchimp_campaign/src/Controller/MailchimpCampaignController.php
MailchimpCampaignForm::form in modules/mailchimp_campaign/src/Form/MailchimpCampaignForm.php
Gets the actual form array to be built.
mailchimp_campaign_get_template in modules/mailchimp_campaign/mailchimp_campaign.module
Return full details for a Mailchimp Template.

File

modules/mailchimp_campaign/mailchimp_campaign.module, line 270
Module file for mailchimp_campaign.

Code

function mailchimp_campaign_list_templates($reset = FALSE, $count = 30) {
  $cache = \Drupal::cache('mailchimp');
  $cached_templates = $cache
    ->get('templates');
  $all_templates = [];
  $template_parameters = [
    'count' => $count,
  ];

  // Return cached lists.
  if (!$reset && !empty($cached_templates)) {
    $all_templates = $cached_templates->data;
  }
  else {
    try {

      /* @var \Mailchimp\MailchimpTemplates $mc_templates */
      if ($mc_templates = mailchimp_get_api_object('MailchimpTemplates')) {
        $response = $mc_templates
          ->getTemplates($template_parameters);
        if ($response) {
          foreach ($response->templates as $template) {
            $all_templates[$template->type][$template->id] = $template;
          }
        }
      }
      $cache
        ->set('templates', $all_templates);
    } catch (Exception $e) {
      \Drupal::messenger()
        ->addError($e
        ->getMessage());
      \Drupal::logger('mailchimp_campaign')
        ->error('An error occurred while retrieving templates: {message}', [
        'message' => $e
          ->getMessage(),
      ]);
    }
  }
  return $all_templates;
}