You are here

public function CampaignMonitorSubscribeForm::submitForm in Campaign Monitor 8

Same name and namespace in other branches
  1. 8.2 src/Form/CampaignMonitorSubscribeForm.php \Drupal\campaignmonitor\Form\CampaignMonitorSubscribeForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/CampaignMonitorSubscribeForm.php, line 305

Class

CampaignMonitorSubscribeForm
Subscribe to a campaignmonitor list.

Namespace

Drupal\campaignmonitor\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $lists = campaignmonitor_get_lists();
  $values = $form_state
    ->getValues();
  $config = $form_state
    ->getValue('config');
  $config = unserialize($config);
  switch ($config['list']) {
    case 'single':
      $selection = [
        $form_state
          ->getValue('list_id'),
      ];
      break;
    default:
      $selection = $form_state
        ->getValue('selection');
  }

  // Sanitize user-supplied text.
  $name = isset($values['name']) ? SafeMarkup::checkPlain($values['name'])
    ->__toString() : NULL;
  $email = SafeMarkup::checkPlain($values['email'])
    ->__toString();
  $custom_fields = NULL;
  if (isset($values['CustomFields'])) {
    $custom_fields = $values['CustomFields'];
    foreach ($custom_fields as $key => $value) {
      if ($form['CustomFields'][$key]['#type'] == 'textfield') {
        $custom_fields[$key] = SafeMarkup::checkPlain($value)
          ->__toString();
      }
    }
  }
  foreach ($selection as $list_id) {
    if ($list_id === 0) {
      continue;
    }
    if (campaignmonitor_subscribe($list_id, $email, $name, $custom_fields, [], FALSE, 'html', FALSE)) {
      $settings = $this
        ->config('campaignmonitor.settings');
      $message = $settings
        ->get('subscription_confirmation_text') ? $settings
        ->get('subscription_confirmation_text') : 'You are subscribed to the @name list.';
      drupal_set_message(t($message, [
        '@name' => html_entity_decode($lists[$list_id]['name']),
      ]));
    }
    else {
      drupal_set_message(t('You were not subscribed to the list, please try again.'));
    }
  }
}