You are here

public function CampaignMonitorUserSubscriptionForm::submitForm in Campaign Monitor 8

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

modules/campaignmonitor_user/src/Form/CampaignMonitorUserSubscriptionForm.php, line 158

Class

CampaignMonitorUserSubscriptionForm
Subscribe to a campaignmonitor list.

Namespace

Drupal\campaignmonitor_user\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $lists = campaignmonitor_get_lists();
  $values = $form_state
    ->getValues();
  $subscribed = FALSE;
  $config = $form_state
    ->getValue('config');
  $config = unserialize($config);

  // Loop through the lists.
  foreach ($values['lists'] as $list_id => $selected) {
    if ($selected !== 0) {

      // Maybe this is an unsubscribe.
      if (campaignmonitor_subscribe($list_id, $values['email'], $values['name'])) {
        drupal_set_message(t('You are now subscribed to the "@list" list.', [
          '@list' => html_entity_decode($lists[$list_id]['name']),
        ]), 'status');
        $subscribed = TRUE;
      }
      else {
        drupal_set_message(t('You were not subscribed to the list. Please try again later.'));
      }

      // Check if the user should be sent to an unsubscribe page.
      //          if (isset($lists_info[$list_id]['details']['UnsubscribePage']) && !empty($lists_info[$list_id]['details']['UnsubscribePage'])) {
      //            drupal_goto($lists_info[$list_id]['details']['UnsubscribePage']);
      //          }
      //          else {
      //            drupal_set_message(t('You are now removed from the "@list" list.', array('@list' => $lists_info[$list_id]['name'])), 'status');
      //          }.
    }
    else {
      campaignmonitor_unsubscribe($list_id, $values['email']);
    }
  }
  if ($subscribed) {
    drupal_set_message(t('Changes to your preferences will take a minute or two to show on the site.'));
  }

  // Return to user page
  // If we stay on the form it will display the old information.
  $url = Url::fromRoute('user.page');
  $form_state
    ->setRedirectUrl($url);
}