function mailchimp_get_mergevars in Mailchimp 8
Same name and namespace in other branches
- 7.5 mailchimp.module \mailchimp_get_mergevars()
- 7.3 mailchimp.module \mailchimp_get_mergevars()
- 7.4 mailchimp.module \mailchimp_get_mergevars()
- 2.x mailchimp.module \mailchimp_get_mergevars()
Wrapper around MailchimpLists->getMergeFields().
Parameters
array $list_ids: Array of Mailchimp list IDs.
bool $reset: Set to TRUE if mergevars should not be loaded from cache.
Return value
array Struct describing mergevars for the specified lists.
4 calls to mailchimp_get_mergevars()
- MailchimpCampaignForm::form in modules/
mailchimp_campaign/ src/ Form/ MailchimpCampaignForm.php - Gets the actual form array to be built.
- MailchimpListsSubscription::fieldSettingsForm in modules/
mailchimp_lists/ src/ Plugin/ Field/ FieldType/ MailchimpListsSubscription.php - Returns a form for the field-level settings.
- MailchimpListsTest::testGetMergevars in modules/
mailchimp_lists/ src/ Tests/ MailchimpListsTest.php - Tests retrieval of mergevars for a set of lists.
- MailchimpSignupForm::getMergevarOptions in modules/
mailchimp_signup/ src/ Form/ MailchimpSignupForm.php - Gets the mergevar options for the given lists.
File
- ./
mailchimp.module, line 243 - Mailchimp module.
Code
function mailchimp_get_mergevars(array $list_ids, $reset = FALSE) {
$mergevars = [];
$cache = \Drupal::cache('mailchimp');
if (!$reset) {
foreach ($list_ids as $key => $list_id) {
$cached_data = $cache
->get($list_id . '-mergevars');
// Get cached data and unset from our remaining lists to query.
if ($cached_data) {
$mergevars[$list_id] = $cached_data->data;
unset($list_ids[$key]);
}
}
}
// Get the uncached merge vars from Mailchimp.
if (count($list_ids)) {
/* @var \Mailchimp\MailchimpLists $mc_lists */
$mc_lists = mailchimp_get_api_object('MailchimpLists');
$list_id = NULL;
try {
if (!$mc_lists) {
throw new Exception('Cannot get merge vars without Mailchimp API. Check API key has been entered.');
}
foreach ($list_ids as $list_id) {
// Add default EMAIL merge var for all lists.
$mergevars[$list_id] = [
(object) [
'tag' => 'EMAIL',
'name' => t('Email Address'),
'type' => 'email',
'required' => TRUE,
'default_value' => '',
'public' => TRUE,
'display_order' => 1,
'options' => (object) [
'size' => 25,
],
],
];
$result = $mc_lists
->getMergeFields($list_id, [
'count' => 500,
]);
if ($result->total_items > 0) {
$mergevars[$list_id] = array_merge($mergevars[$list_id], $result->merge_fields);
}
$cache
->set($list_id . '-mergevars', $mergevars[$list_id]);
}
} catch (\Exception $e) {
\Drupal::logger('mailchimp')
->error('An error occurred requesting mergevars for list {list}. "{message}"', [
'list' => $list_id,
'message' => $e
->getMessage(),
]);
}
}
return $mergevars;
}