You are here

function mailchimp_signup_subscribe_form in Mailchimp 7.3

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_signup/mailchimp_signup.module \mailchimp_signup_subscribe_form()
  2. 7.4 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 261
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("field:mailchimp_signup:form:{$signup->mcs_id}:description", filter_xss($signup->settings['description'])),
    '#prefix' => '<div class="mailchimp-signup-subscribe-form-description">',
    '#suffix' => '</div>',
  );
  $form['mailchimp_lists'] = array(
    '#tree' => TRUE,
  );
  $lists = mailchimp_get_lists($signup->mc_lists);
  $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['web_id'];
      $form['mailchimp_lists'][$wrapper_key] = array(
        '#prefix' => '<div id="mailchimp-newsletter-' . $list['web_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);
      }
    }
  }
  else {
    $list = reset($lists);
    if ($signup->settings['include_interest_groups'] && isset($list['intgroups'])) {
      $form['mailchimp_lists']['#weight'] = 9;
      $form['mailchimp_lists']['interest_groups'] = mailchimp_interest_groups_form_elements($list);
    }
  }
  $form['mergevars'] = array(
    '#prefix' => '<div id="mailchimp-newsletter-' . $list['web_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("field:mailchimp_signup:form:{$signup->mcs_id}:mergefield:{$tag}", $mergevar['name']);
      $form['mergevars'][$tag] = mailchimp_insert_drupal_form_tag($mergevar);
      if (empty($lists)) {
        $form['mergevars'][$tag]['#disabled'] = TRUE;
      }
    }
  }
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#weight' => 10,
    '#value' => mailchimp_signup_tt("field:mailchimp_signup:form:{$signup->mcs_id}:submit_button", $signup->settings['submit_button']),
    '#disabled' => empty($lists),
  );
  return $form;
}