You are here

function constant_contact_form_user_register_alter in Constant Contact 6.2

Same name and namespace in other branches
  1. 6.3 constant_contact.module \constant_contact_form_user_register_alter()

Alter the user registration form

File

./constant_contact.module, line 148

Code

function constant_contact_form_user_register_alter(&$form, &$form_state) {
  $show_on_register = variable_get('constant_contact_show_on_register', CONSTANT_CONTACT_SHOW_ON_REGISTER);
  $show_lists_selection = variable_get('constant_contact_show_list_selection', CONSTANT_CONTACT_SHOW_LIST_SELECTION);
  if (!$show_on_register) {
    return;
  }
  $cc = constant_contact_create_object();
  if (!is_object($cc)) {
    drupal_set_message(t("Failed to create constant contact object, please check your account details are correctly entered on the settings page"), 'error');
    return;
  }

  // select all lists by default
  $lists = $cc
    ->get_lists();
  if ($lists) {
    foreach ($lists as $k => $v) {
      $selected_lists[$k] = $v['id'];
    }
  }
  $form['account']['cc_newsletter'] = array(
    '#type' => 'checkbox',
    '#title' => variable_get('constant_contact_signup_title', CONSTANT_CONTACT_SIGNUP_TITLE),
    '#description' => variable_get('constant_contact_signup_description', CONSTANT_CONTACT_SIGNUP_DESCRIPTION),
    '#weight' => 10,
  );
  $form['account']['cc_newsletter']['#default_value'] = 1;
  if ($show_lists_selection) {
    $exclude_lists = variable_get('constant_contact_lists', '');
    $lists = $cc
      ->get_lists();
    if ($lists) {
      foreach ($lists as $k => $v) {
        $lists[$k] = $v['id'];
      }
    }
    if (!is_array($exclude_lists)) {
      $exclude_lists = array();
    }
    $options = array();
    foreach ($lists as $list_id) {
      if (!in_array($list_id, $exclude_lists)) {
        $list = $cc
          ->get_list($list_id);
        $options[$list['id']] = $list['Name'];
      }
    }
    if (count($options) > 0) {
      $form['account']['cc_newsletter_lists'] = array(
        '#type' => 'select',
        '#description' => variable_get('constant_contact_signup_lists_description', CONSTANT_CONTACT_SIGNUP_LISTS_DESCRIPTION),
        '#options' => $options,
        '#multiple' => true,
        '#size' => count($options),
        '#default_value' => $selected_lists,
        '#weight' => 11,
      );
    }
  }
  return $form;
}