You are here

public function CampaignMonitorSubscriptionManager::singleSubscribeForm in Campaign Monitor 8.2

Obtain the form elements required for subscription.

This resides here so that it can be used by both: The subscription form and registration form.

File

src/CampaignMonitorSubscriptionManager.php, line 128

Class

CampaignMonitorSubscriptionManager
Manager for Campaignmonitor subscriptions.

Namespace

Drupal\campaignmonitor

Code

public function singleSubscribeForm($config, $email = '') {
  $list_id = $config['list_id'];
  $list = $this->campaignMonitorManager
    ->getExtendedList($list_id);
  $list_options = $this->campaignMonitorManager
    ->getListSettings($list_id);

  // Set options for the form.
  $form = [
    '#tree' => TRUE,
    '#attributes' => [
      'class' => [
        'campaignmonitor-subscribe-form',
        'campaignmonitor-subscribe-form-' . str_replace(' ', '-', strtolower($list['name'])),
      ],
    ],
  ];
  if ($config['list_id_text'] != '') {
    $text = str_replace('@name', $list['name'], $config['list_id_text']);
    $form['explanation'] = [
      '#type' => 'container',
      '#markup' => $text,
    ];
  }

  // Token replace if the token module is present.
  if (isset($list_options['tokens']['name']) && $this->moduleHandler
    ->moduleExists('token') && $this->account
    ->id() > 0) {
    $name = $this->token
      ->replace($list_options['tokens']['name'], [
      'user' => $this->entityTypeManager
        ->getStorage('user')
        ->load($this->account
        ->id()),
    ], [
      'clear' => TRUE,
    ]);
  }

  // Check if the user is subscribed and get name from Campaign Monitor.
  if (!empty($email) && $this->campaignMonitorManager
    ->isSubscribed($list_id, $email)) {

    // If subscribed, get user name from Campaign Monitor.
    $subscriber = $this->campaignMonitorManager
      ->getSubscriber($list_id, $email);
    $name = isset($subscriber['Name']) ? $subscriber['Name'] : $name;
  }

  // Should the name field be displayed for this user.
  if (isset($list_options['display']['name']) && $list_options['display']['name']) {
    $form['name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Name'),
      '#required' => TRUE,
      '#maxlength' => 200,
      '#default_value' => isset($name) ? $name : '',
    ];
  }

  // Get the order correct.
  if (isset($list_options['CustomFields'])) {
    $new_list = [];
    foreach ($list_options['CustomFields'] as $key => $values) {
      $new_list['CustomFields'][$key] = $list['CustomFields']['[' . $key . ']'];
    }
    $list = $new_list;
  }
  if (isset($list['CustomFields'])) {
    foreach ($list['CustomFields'] as $key => $field) {

      // Form API can't handle keys with [] in all cases.
      $form_key = str_replace([
        '[',
        ']',
      ], '', $key);

      // Check if field should be displayed.
      if (isset($list_options['CustomFields']) && !$list_options['CustomFields'][$form_key]['selected']) {

        // Field is not selected, so continue.
        continue;
      }

      // Token replace default value, if the token module is present.
      $token = '';
      if ($this->moduleHandler
        ->moduleExists('token') && isset($list_options['tokens'][$form_key])) {
        $token_user = $this->account
          ->id() > 0 ? $this->entityTypeManager
          ->getStorage('user')
          ->load($this->account
          ->id()) : NULL;
        $token = $this->token
          ->replace($list_options['tokens'][$form_key], [
          'user' => $token_user,
        ], [
          'clear' => TRUE,
        ]);
      }
      switch ($field['DataType']) {
        case 'Text':
          $form['CustomFields'][$form_key] = [
            '#type' => 'textfield',
            '#title' => HTML::escape($field['FieldName']),
            '#maxlength' => 200,
            '#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
          ];
          break;
        case 'MultiSelectOne':
          $options = [];
          foreach ($field['FieldOptions'] as $option) {
            $options[$option] = $option;
          }
          $form['CustomFields'][$form_key] = [
            '#type' => 'select',
            '#title' => HTML::escape($field['FieldName']),
            '#options' => $options,
            '#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
          ];
          break;
        case 'MultiSelectMany':
          $options = [];
          foreach ($field['FieldOptions'] as $option) {
            $options[$option] = $option;
          }

          // If one value was selected, default is a string else an array.
          $cm_default = isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : [];

          // Expensive.
          $is_array = is_array($cm_default);
          $default = [];
          foreach ($options as $value) {
            if ($is_array) {
              if (in_array($value, $cm_default)) {
                $default[$value] = $value;
              }
            }
            elseif ($cm_default == $value) {
              $default[$cm_default] = $cm_default;
            }
            else {
              $default[$value] = 0;
            }
          }
          $form['CustomFields'][$form_key] = [
            '#type' => 'checkboxes',
            '#title' => HTML::escape($field['FieldName']),
            '#options' => $options,
            '#default_value' => $default,
          ];
          break;
        case 'Number':
          $form['CustomFields'][$form_key] = [
            '#type' => 'textfield',
            '#title' => HTML::escape($field['FieldName']),
            '#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
          ];
          break;
        case 'Date':
          $form['CustomFields'][$form_key] = [
            '#type' => 'date',
            '#title' => HTML::escape($field['FieldName']),
            '#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
            '#attributes' => [
              'class' => [
                'campaignmonitor-date',
              ],
              'type' => 'date',
            ],
          ];
          break;
      }
      if ($list_options['CustomFields'][$form_key]['required']) {
        $form['CustomFields'][$form_key]['#required'] = TRUE;
      }
    }
  }
  $form['list_id'] = [
    '#type' => 'hidden',
    '#default_value' => $list_id,
  ];
  return $form;
}