You are here

function mailchimp_insert_drupal_form_tag in Mailchimp 2.x

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. 7.4 mailchimp.module \mailchimp_insert_drupal_form_tag()

Convert Mailchimp form elements to Drupal Form API.

Parameters

object $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()
MailchimpSignupPageForm::buildForm in modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php
Form constructor.

File

./mailchimp.module, line 1252
Mailchimp module.

Code

function mailchimp_insert_drupal_form_tag($mergevar) {

  // Insert common FormAPI properties:
  $input = [
    '#weight' => $mergevar->display_order,
    '#required' => $mergevar->required,
    '#default_value' => $mergevar->default_value,
  ];

  // phpcs:disable
  $input['#title'] = t((string) $mergevar->name);

  // phpcs:enable
  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'] = [
        '#title' => t('Address 1'),
        '#type' => 'textfield',
      ];
      $input['addr2'] = [
        '#title' => t('Address 2'),
        '#type' => 'textfield',
      ];
      $input['city'] = [
        '#title' => t('City'),
        '#type' => 'textfield',
      ];
      $input['state'] = [
        '#title' => t('State'),
        '#type' => 'textfield',
        '#size' => 2,
        '#maxlength' => 2,
      ];
      $input['zip'] = [
        '#title' => t('Zip'),
        '#type' => 'textfield',
        '#size' => 6,
        '#maxlength' => 6,
      ];
      $input['country'] = [
        '#title' => t('Country'),
        '#type' => 'textfield',
        '#size' => 2,
        '#maxlength' => 2,
      ];
      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 = [];
      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 = [];
      foreach ($mergevar->options->choices as $choice) {
        $choices[$choice] = $choice;
      }
      $input['#options'] = $choices;
      break;
    case 'email':
      if (\Drupal::service('element_info')
        ->getInfo('emailfield', '#type')) {

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

        // Set to standard text type if 'emailfield' isn't defined:
        $input['#type'] = 'textfield';
      }
      $input['#size'] = $mergevar->options->size;
      $input['#element_validate'] = [
        'mailchimp_validate_email',
      ];
      break;
    case 'date':
      $input['#type'] = 'date';
      break;
    default:

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

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