You are here

function mailchimp_lists_subscribe_form in Mailchimp 7.4

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

Return a form element for a single newsletter.

1 string reference to 'mailchimp_lists_subscribe_form'
mailchimp_lists_forms in modules/mailchimp_lists/mailchimp_lists.module
Implements hook_forms().

File

modules/mailchimp_lists/mailchimp_lists.module, line 144

Code

function mailchimp_lists_subscribe_form($form, &$form_state, $instance, $settings, $entity, $field) {
  $use_interest_groups = $instance['settings']['show_interest_groups'] && $settings['show_interest_groups'];
  $mc_list = mailchimp_get_list($field['settings']['mc_list_id'], $use_interest_groups);
  $email = mailchimp_lists_load_email($instance, $entity);
  if (!$email) {
    return array();
  }

  // Determine if a user is subscribed to the list.
  $is_subscribed = mailchimp_is_subscribed($mc_list->id, $email);
  $wrapper_key = 'mailchimp_' . $instance['field_name'];
  $form_state['settings'] = array(
    'wrapper_key' => $wrapper_key,
    'instance' => $instance,
    'field' => $field,
    'entity' => $entity,
  );
  $form[$wrapper_key] = array(
    '#type' => 'container',
    '#tree' => TRUE,
    '#description' => $instance['description'],
    '#attributes' => array(
      'class' => array(
        'mailchimp-newsletter-wrapper',
        'mailchimp-newsletter-' . $instance['field_name'],
      ),
    ),
  );

  // Add the title and description to lists for anonymous users or if requested:
  $form[$wrapper_key]['subscribe'] = array(
    '#type' => 'checkbox',
    '#title' => t('Subscribe'),
    '#disabled' => $instance['required'],
    '#required' => $instance['required'],
    '#default_value' => $instance['required'] || $is_subscribed,
  );

  // Present interest groups:
  if ($use_interest_groups) {

    // Perform test in case error comes back from MCAPI when getting groups:
    if (is_array($mc_list->intgroups)) {
      $form[$wrapper_key]['interest_groups'] = array(
        '#type' => 'fieldset',
        '#title' => isset($settings['interest_groups_label']) ? $settings['interest_groups_label'] : t('Interest Groups'),
        '#weight' => 100,
        '#states' => array(
          'invisible' => array(
            ':input[name="' . $wrapper_key . '[subscribe]"]' => array(
              'checked' => FALSE,
            ),
          ),
        ),
      );
      $form[$wrapper_key]['interest_groups'] += mailchimp_interest_groups_form_elements($mc_list, $instance['default_value'][0]['interest_groups'], $email);
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}