You are here

function _mailchimp_lists_field_instance_settings_form_process in Mailchimp 7.4

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_lists/includes/mailchimp_lists.field.inc \_mailchimp_lists_field_instance_settings_form_process()
  2. 7.3 modules/mailchimp_lists/includes/mailchimp_lists.field.inc \_mailchimp_lists_field_instance_settings_form_process()

Element processor. Expand the mergefields mapping form.

Doing it this way instead of adding all the mapping fields inside of the hook_field_instance_settings_form() implementation allows us to use AJAX in the settings form to toggle between the basic and advanced mapping UI.

1 string reference to '_mailchimp_lists_field_instance_settings_form_process'
mailchimp_lists_field_instance_settings_form in modules/mailchimp_lists/includes/mailchimp_lists.field.inc
Implements hook_field_instance_settings_form().

File

modules/mailchimp_lists/includes/mailchimp_lists.field.inc, line 180
Field hooks.

Code

function _mailchimp_lists_field_instance_settings_form_process($form, &$form_state) {
  $field = $form['#field'];
  $instance = $instance = isset($form_state['mailchimp']['instance']) ? $form_state['mailchimp']['instance'] : $form['#instance'];
  $mc_list_id = $form['#mc_list_id'];
  $mv_defaults = $instance['settings']['mergefields'];
  $use_advanced_mode = isset($mv_defaults['advanced']) ? (bool) $mv_defaults['advanced'] : FALSE;

  // If the AJAX checkbox to toggle between advanced and basic input mode has
  // been clicked we can use that info here to override what is stored in the
  // database.
  // TODO: figure out why 'mergefields' is in 'input' but not 'values'
  if (isset($form_state['input']) && isset($form_state['input']['instance']['settings']['mergefields']['advanced'])) {
    $use_advanced_mode = (bool) $form_state['input']['instance']['settings']['mergefields']['advanced'];
  }
  $form['#description'] = $use_advanced_mode ? t('Use tokens like [user:mail] which will be replaced with the appropriate value before sending them to Mailchimp.') : t('Multi-value fields will only sync their first value to Mailchimp.');
  $mergevars = mailchimp_get_mergevars(array(
    $mc_list_id,
  ));
  $fields = mailchimp_lists_fieldmap_options($instance['entity_type'], $instance['bundle']);
  $required_fields = mailchimp_lists_fieldmap_options($instance['entity_type'], $instance['bundle'], TRUE);
  unset($fields[$field['field_name']]);
  foreach ($mergevars[$mc_list_id] as $mergevar) {
    $default_value = isset($mv_defaults[$mergevar->tag]) ? $mv_defaults[$mergevar->tag] : -1;

    // If the advanced UI is enabled we use a textfield here, otherwise use a
    // select field.
    $form[$mergevar->tag] = array(
      '#type' => $use_advanced_mode ? 'textfield' : 'select',
      '#title' => check_plain($mergevar->name),
      '#default_value' => $default_value ? $default_value : '',
      '#required' => $mergevar->required,
    );
    if (!$mergevar->required || $mergevar->tag === 'EMAIL') {

      // Add select field options when using basic UI.
      if (!$use_advanced_mode) {
        $form[$mergevar->tag]['#options'] = $fields;
      }
      if ($mergevar->tag === 'EMAIL') {
        $form[$mergevar->tag]['#description'] = t('Any entity with an empty or invalid email address field value will simply be ignored by the Mailchimp subscription system. <em>This is why the Email field is the only required merge field which can sync to non-required fields.</em>');
      }
    }
    else {

      // Add options for select fields when using basic UI.
      if (!$use_advanced_mode) {
        $form[$mergevar->tag]['#options'] = $required_fields;
      }
      $form[$mergevar->tag]['#description'] = t("Only 'required' and 'calculated' fields are allowed to be synced with Mailchimp 'required' merge fields.");
    }
  }

  // If we're displaying the advanced mode UI, and the token module from contrib
  // is enabled we can use its ability to add some additional features like a
  // better token browser, and better validation.
  if ($use_advanced_mode && module_exists('token')) {

    // Add element validator for each text field. This allows the token module
    // to provide feedback on token syntax.
    foreach ($mergevars[$mc_list_id] as $mergevar) {
      $form[$mergevar->tag] += array(
        '#element_validate' => array(
          'token_element_validate',
        ),
        '#token_types' => array(
          $instance['entity_type'],
        ),
      );
    }

    // Add the UI for browsing tokens.
    $form['_tokens'] = array(
      '#theme' => 'token_tree_link',
      '#token_types' => array(
        $instance['entity_type'],
      ),
      '#global_types' => TRUE,
      '#click_insert' => TRUE,
    );
  }

  // Toggle advanced mode, allows for directly entering tokens into a textfield
  // rather than using the <select> based UI.
  $form['advanced'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable advanced mapping'),
    '#description' => t('Enable token based input to deal with multi-value fields and gain more control over formatting.'),
    '#default_value' => $use_advanced_mode,
    '#ajax' => array(
      'callback' => 'mailchimp_lists_field_instance_settings_form_ajax_callback',
      'wrapper' => 'mergefield-wrapper',
    ),
  );
  return $form;
}