You are here

function _mailchimp_interest_groups_element in Mailchimp 6.2

Helper that returns interest groups form element

Parameters

array $groups - list of interest groups:

array $groupings - list of groups a user is in:

2 calls to _mailchimp_interest_groups_element()
mailchimp_auth_newsletter_form in ./mailchimp.module
Called from mailchimp_subscribe_auth_form() and embeds the form elements for a single newseltter.
_mailchimp_subscribe_anon_form in ./mailchimp.module
Helper function to return form elements for a single anon newsletter

File

./mailchimp.module, line 665
Mailchimp module.

Code

function _mailchimp_interest_groups_element($q, $list_id, $groupings = array(), $is_subscribed = FALSE) {
  $element = array();
  if ($groups = $q
    ->listInterestGroupings($list_id)) {
    $element['interest_groups_' . $list_id] = array(
      '#type' => 'fieldset',
      '#title' => t('Interest Groups'),
      '#description' => t(''),
      '#collapsible' => TRUE,
      '#collapsed' => !$is_subscribed,
      '#tree' => TRUE,
      '#attributes' => array(
        'class' => 'mailchimp-newsletter-interests-' . $list_id,
      ),
    );
    foreach ($groups as $group) {

      // ignore hidden groups
      if ($group['form_field'] != 'hidden') {
        $default = array();
        if (!empty($groupings)) {
          foreach ($groupings as $grouping) {
            if ($grouping['id'] == $group['id']) {
              $default = _mailchimp_explode_interest_groups($grouping['groups']);
            }
          }
        }
        $field_type = '';
        switch ($group['form_field']) {
          case 'radio':
            $field_type = 'radios';
            break;
          case 'dropdown':
            $field_type = 'select';
            break;
          default:
            $field_type = $group['form_field'];
        }
        $options = array();
        foreach ((array) $group['groups'] as $item) {
          $options[$item['name']] = $item['name'];
        }
        $element['interest_groups_' . $list_id][$group['id']] = array(
          '#type' => $field_type,
          '#title' => t('@name', array(
            '@name' => $group['name'],
          )),
          '#multiple' => FALSE,
          '#options' => $options,
          '#default_value' => $default,
        );
      }
    }
  }
  return $element;
}