You are here

function mailchimp_signup_subscribe_form in Mailchimp 7.4

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_signup/mailchimp_signup.module \mailchimp_signup_subscribe_form()
  2. 7.3 modules/mailchimp_signup/mailchimp_signup.module \mailchimp_signup_subscribe_form()

Returns a subscription form for mailchimp lists.

If there are multiple lists, this generates a single form for all of them.

1 string reference to 'mailchimp_signup_subscribe_form'
mailchimp_signup_forms in modules/mailchimp_signup/mailchimp_signup.module
Implements hook_forms().

File

modules/mailchimp_signup/mailchimp_signup.module, line 299
Mailchimp Signup module. Allows creation of signup forms integrated with Mailchimp.

Code

function mailchimp_signup_subscribe_form($form, &$form_state, $signup, $type) {
  $form['#attributes'] = array(
    'class' => array(
      'mailchimp-signup-subscribe-form',
    ),
  );
  $form['description'] = array(
    '#markup' => mailchimp_signup_tt("mailchimp_signup:mailchimp_signup:{$signup->name}:description", filter_xss($signup->settings['description'])),
    '#prefix' => '<div class="mailchimp-signup-subscribe-form-description">',
    '#suffix' => '</div>',
  );
  $form['mailchimp_lists'] = array(
    '#tree' => TRUE,
  );
  $use_interest_groups = $signup->settings['include_interest_groups'];
  $lists = mailchimp_get_lists($signup->mc_lists, $use_interest_groups);
  $lists_count = !empty($lists) ? count($lists) : 0;
  if (empty($lists)) {
    drupal_set_message('The subscription service is currently unavailable. Please try again later.', 'warning');
  }
  $list = array();
  if ($lists_count > 1) {
    foreach ($lists as $list) {

      // Wrap in a div:
      $wrapper_key = 'mailchimp_' . $list->id;
      $form['mailchimp_lists'][$wrapper_key] = array(
        '#prefix' => '<div id="mailchimp-newsletter-' . $list->id . '" class="mailchimp-newsletter-wrapper">',
        '#suffix' => '</div>',
      );
      $form['mailchimp_lists'][$wrapper_key]['subscribe'] = array(
        '#type' => 'checkbox',
        '#title' => $list->name,
        '#return_value' => $list->id,
        '#default_value' => 0,
      );
      if ($signup->settings['include_interest_groups'] && isset($list->intgroups)) {
        $form['mailchimp_lists'][$wrapper_key]['interest_groups'] = array(
          '#type' => 'fieldset',
          '#title' => t('Interest Groups for %label', array(
            '%label' => $list->name,
          )),
          '#states' => array(
            'invisible' => array(
              ':input[name="mailchimp_lists[' . $wrapper_key . '][subscribe]"]' => array(
                'checked' => FALSE,
              ),
            ),
          ),
        );
        $form['mailchimp_lists'][$wrapper_key]['interest_groups'] += mailchimp_interest_groups_form_elements($list);
        foreach ($form['mailchimp_lists'][$wrapper_key]['interest_groups'] as $group_id => $group) {
          $form['mailchimp_lists'][$wrapper_key]['interest_groups'][$group_id]['#title'] = mailchimp_signup_tt("mailchimp_signup:mailchimp_signup:{$signup->name}:intgroup_{$group_id}", $group['#title']);
          foreach ($group['#options'] as $interest_id => $interest_name) {
            $form['mailchimp_lists'][$wrapper_key]['interest_groups'][$group_id]['#options'][$interest_id] = mailchimp_signup_tt('mailchimp_signup:mailchimp_signup:' . $signup->name . ':intgroup_' . $group_id . '_interest_' . $interest_id, $interest_name);
          }
        }
      }
    }
  }
  else {
    $list = reset($lists);
    if ($use_interest_groups && isset($list->intgroups)) {
      $form['mailchimp_lists']['#weight'] = 9;
      $form['mailchimp_lists']['interest_groups'] = mailchimp_interest_groups_form_elements($list);
      foreach ($form['mailchimp_lists']['interest_groups'] as $group_id => $group) {
        $form['mailchimp_lists']['interest_groups'][$group_id]['#title'] = mailchimp_signup_tt("mailchimp_signup:mailchimp_signup:{$signup->name}:intgroup_{$group_id}", $group['#title']);
        foreach ($group['#options'] as $interest_id => $interest_name) {
          $form['mailchimp_lists']['interest_groups'][$group_id]['#options'][$interest_id] = mailchimp_signup_tt('mailchimp_signup:mailchimp_signup:' . $signup->name . ':intgroup_' . $group_id . '_interest_' . $interest_id, $interest_name);
        }
      }
    }
  }
  $form['mergevars'] = array(
    '#prefix' => '<div id="mailchimp-newsletter-' . $list->id . '-mergefields" class="mailchimp-newsletter-mergefields">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  );
  foreach ($signup->settings['mergefields'] as $tag => $mergevar) {
    if (!empty($mergevar)) {
      $mergevar->name = mailchimp_signup_tt("mailchimp_signup:mailchimp_signup:{$signup->name}:mergefield:{$tag}", $mergevar->name);
      $placeholder = isset($signup->settings['placeholder']) ? $signup->settings['placeholder'] : NULL;
      $form['mergevars'][$tag] = mailchimp_insert_drupal_form_tag($mergevar, $placeholder);
      if (empty($lists)) {
        $form['mergevars'][$tag]['#disabled'] = TRUE;
      }
    }
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#weight' => 10,
    '#value' => mailchimp_signup_tt("mailchimp_signup:mailchimp_signup:{$signup->name}:submit_button", $signup->settings['submit_button']),
    '#disabled' => empty($lists),
  );
  return $form;
}