You are here

function mailchimp_interest_groups_form_elements in Mailchimp 7.4

Same name and namespace in other branches
  1. 8 mailchimp.module \mailchimp_interest_groups_form_elements()
  2. 7.5 mailchimp.module \mailchimp_interest_groups_form_elements()
  3. 7.3 mailchimp.module \mailchimp_interest_groups_form_elements()
  4. 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.

File

./mailchimp.module, line 1509
Mailchimp module.

Code

function mailchimp_interest_groups_form_elements($list, $defaults = array(), $email = NULL) {
  $return = array();
  foreach ($list->intgroups as $group) {
    if ($group->type == 'hidden') {
      continue;
    }

    /* @var \Mailchimp\MailchimpLists $mc_lists */
    $mc_lists = mailchimp_get_api_object('MailchimpLists');
    $interest_data = $mc_lists
      ->getInterests($list->id, $group->id, array(
      'count' => 500,
    ));
    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 = array();
    $default_values = array();

    // Set interest options and default values.
    foreach ($interest_data->interests 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;
}