function mailchimp_interest_groups_form_elements in Mailchimp 7.5
Same name and namespace in other branches
- 8 mailchimp.module \mailchimp_interest_groups_form_elements()
- 7.3 mailchimp.module \mailchimp_interest_groups_form_elements()
- 7.4 mailchimp.module \mailchimp_interest_groups_form_elements()
- 2.x mailchimp.module \mailchimp_interest_groups_form_elements()
Helper function to generate form elements for a list's interest groups.
Parameters
array $list: Fully loaded array with mailchimp list settings as returned by mailchimp_get_list()
array $defaults: Array of default values to use if no group subscription values already exist at MailChimp.
string $email: Optional email address to pass to the MCAPI and retrieve existing values for use as defaults.
Return value
array A collection of form elements, one per interest group.
4 calls to mailchimp_interest_groups_form_elements()
- mailchimp_lists_field_get in modules/
mailchimp_lists/ includes/ mailchimp_lists.field.inc - Entity field data callback for MailChimp subscription fields.
- mailchimp_lists_field_widget_form in modules/
mailchimp_lists/ includes/ mailchimp_lists.field.inc - Implements hook_field_widget_form().
- mailchimp_lists_subscribe_form in modules/
mailchimp_lists/ mailchimp_lists.module - Return a form element for a single newsletter.
- mailchimp_signup_subscribe_form in modules/
mailchimp_signup/ mailchimp_signup.module - Returns a subscription form for mailchimp lists/audiences.
File
- ./
mailchimp.module, line 1683 - Mailchimp module.
Code
function mailchimp_interest_groups_form_elements($list, $defaults = array(), $email = NULL) {
$return = [];
foreach ($list->intgroups as $group) {
if ($group->type == 'hidden') {
continue;
}
try {
$interest_data = mailchimp_get_interests($list->id, $group->id);
} catch (\Exception $e) {
watchdog('mailchimp', 'An error occurred requesting interest groups for audience @list. "%message"', [
'@list' => $list->id,
'%message' => $e
->getMessage(),
], WATCHDOG_ERROR);
return array();
}
if (!empty($email)) {
$memberinfo = mailchimp_get_memberinfo($list->id, $email);
}
// Set the form field type:
switch ($group->type) {
case 'radio':
$field_type = 'radios';
break;
case 'dropdown':
$field_type = 'select';
break;
default:
$field_type = $group->type;
}
// Extract the field options:
$options = [];
$default_values = [];
// Set interest options and default values.
foreach ($interest_data as $interest) {
$options[$interest->id] = $interest->name;
if (isset($memberinfo)) {
if (isset($memberinfo->interests->{$interest->id}) && $memberinfo->interests->{$interest->id} === TRUE) {
$default_values[$group->id][] = $interest->id;
}
}
elseif (!empty($defaults)) {
if (isset($defaults[$group->id][$interest->id]) && !empty($defaults[$group->id][$interest->id])) {
$default_values[$group->id][] = $interest->id;
}
}
}
$return[$group->id] = array(
'#type' => $field_type,
'#title' => $group->title,
'#options' => $options,
'#empty_option' => t('-- select --'),
'#default_value' => isset($default_values[$group->id]) ? $default_values[$group->id] : array(),
'#attributes' => array(
'class' => array(
'mailchimp-newsletter-interests-' . $list->id,
),
),
);
}
return $return;
}