You are here

function mailchimp_insert_drupal_form_tag in Mailchimp 7.4

Same name and namespace in other branches
  1. 8 mailchimp.module \mailchimp_insert_drupal_form_tag()
  2. 7.5 mailchimp.module \mailchimp_insert_drupal_form_tag()
  3. 7.3 mailchimp.module \mailchimp_insert_drupal_form_tag()
  4. 2.x mailchimp.module \mailchimp_insert_drupal_form_tag()

Convert mailchimp form elements to Drupal Form API.

Parameters

array $mergevar: The mailchimp-formatted form element to convert.

Return value

array A properly formatted drupal form element.

1 call to mailchimp_insert_drupal_form_tag()
mailchimp_signup_subscribe_form in modules/mailchimp_signup/mailchimp_signup.module
Returns a subscription form for mailchimp lists.

File

./mailchimp.module, line 1580
Mailchimp module.

Code

function mailchimp_insert_drupal_form_tag($mergevar, $placeholder) {

  // Insert common FormAPI properties:
  $input = array(
    '#weight' => $mergevar->display_order,
    '#required' => $mergevar->required,
    '#default_value' => $mergevar->default_value,
  );
  $placeholder_req = $mergevar->required ? ' *' : '';
  $title = t('@mergevar', array(
    '@mergevar' => $mergevar->name,
  ));

  // Check to see if we should set placeholder or #title attribute
  if ($placeholder) {
    $input['#attributes']['placeholder'] = $title . $placeholder_req;
  }
  else {
    $input['#title'] = $title;
  }
  switch ($mergevar->type) {
    case 'address':

      // Sub-array of address elements according to Mailchimp specs.
      // https://apidocs.mailchimp.com/api/2.0/lists/subscribe.php
      $input['#type'] = 'container';
      $input['#tree'] = TRUE;
      $input['addr1'] = array(
        '#type' => 'textfield',
      );
      $input['addr2'] = array(
        '#type' => 'textfield',
      );
      $input['city'] = array(
        '#type' => 'textfield',
      );
      $input['state'] = array(
        '#type' => 'textfield',
        '#size' => 2,
        '#maxlength' => 2,
      );
      $input['zip'] = array(
        '#type' => 'textfield',
        '#size' => 6,
        '#maxlength' => 6,
      );
      $input['country'] = array(
        '#type' => 'textfield',
        '#size' => 2,
        '#maxlength' => 2,
      );
      if (!$placeholder) {
        $input['addr1']['#title'] = t('Address 1');
        $input['addr2']['#title'] = t('Address 2');
        $input['city']['#title'] = t('City');
        $input['state']['#title'] = t('State');
        $input['zip']['#title'] = t('Zip');
        $input['country']['#title'] = t('Country');
      }
      else {
        $input['addr1']['#attributes']['placeholder'] = t('Address 1') . $placeholder_req;
        $input['addr2']['#attributes']['placeholder'] = t('Address 2') . $placeholder_req;
        $input['city']['#attributes']['placeholder'] = t('City') . $placeholder_req;
        $input['state']['#attributes']['placeholder'] = t('State') . $placeholder_req;
        $input['zip']['#attributes']['placeholder'] = t('Zip') . $placeholder_req;
        $input['country']['#attributes']['placeholder'] = t('Country') . $placeholder_req;
      }
      break;
    case 'dropdown':

      // Dropdown is mapped to <select> element in Drupal Form API.
      $input['#type'] = 'select';

      // Creates options, we must delete array keys to have relevant information
      // on Mailchimp.
      $choices = array();
      foreach ($mergevar->options->choices as $choice) {
        $choices[$choice] = $choice;
      }
      $input['#options'] = $choices;
      break;
    case 'radio':

      // Radio is mapped to <input type='radio' /> i.e. 'radios' element in
      // Drupal Form API.
      $input['#type'] = 'radios';

      // Creates options, we must delete array keys to have relevant information
      // on Mailchimp.
      $choices = array();
      foreach ($mergevar->options->choices as $choice) {
        $choices[$choice] = $choice;
      }
      $input['#options'] = $choices;
      break;
    case 'email':
      if (element_info_property('emailfield', '#type')) {

        // Set to an HTML5 email type if 'emailfield' is supported:
        $input['#type'] = 'emailfield';
      }
      else {

        // Set to standard text type if 'emailfield' isn't defined:
        $input['#type'] = 'textfield';

        // Add validation - not necessary for emailfield type input because
        // the "elements" module provides it for elements of type emailfield.
        $input['#element_validate'] = array(
          'mailchimp_validate_email',
        );
      }
      $input['#size'] = isset($mergevar->options->size) ? $mergevar->options->size : 25;
      break;
    case 'phone':
      $input['#type'] = 'textfield';
      $input['#size'] = isset($mergevar->options->size) ? $mergevar->options->size : 25;
      $input['#attributes'] = array(
        'type' => 'tel',
      );
      break;
    default:

      // This is a standard input[type=text] or something we can't handle with
      // Drupal FormAPI.
      $input['#type'] = 'textfield';
      $input['#size'] = isset($mergevar->options->size) ? $mergevar->options->size : 25;
      break;
  }

  // Special cases for Mailchimp hidden defined fields:
  if ($mergevar->public === FALSE) {
    $input['#access'] = FALSE;
  }
  return $input;
}