You are here

public function CampaignMonitorSubscriptionManager::subscribeSubmitHandler in Campaign Monitor 8.2

Shared handler for submitting subscriptions.

File

src/CampaignMonitorSubscriptionManager.php, line 297

Class

CampaignMonitorSubscriptionManager
Manager for Campaignmonitor subscriptions.

Namespace

Drupal\campaignmonitor

Code

public function subscribeSubmitHandler(array &$form, FormStateInterface $form_state) {
  $lists = $this->campaignMonitorManager
    ->getLists();
  $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');
  }
  $custom_fields = isset($values['CustomFields']) ? $values['CustomFields'] : NULL;
  $name = isset($values['name']) ? HTML::escape($values['name'])
    ->__toString() : NULL;

  // The key 'mail' corresponds with user registration.
  $email = HTML::escape($values['mail'])
    ->__toString();
  foreach ($selection as $list_id) {
    if ($list_id === 0) {
      continue;
    }
    if ($this
      ->userSubscribe($list_id, $email, $name, $custom_fields)) {
      if (isset($custom_fields['Interests'])) {
        foreach ($custom_fields['Interests'] as $key => $interest) {
          if ($interest === 0) {
            unset($custom_fields['Interests'][$key]);
          }
        }
      }
      $message = $this->config
        ->get('subscription_confirmation_text') ? $this->config
        ->get('subscription_confirmation_text') : 'You are subscribed to the @name list.';
      $this->messenger
        ->addStatus($this
        ->t($message, [
        '@name' => html_entity_decode($lists[$list_id]['name']),
        '@interests' => implode(', ', $custom_fields['Interests']),
      ]));
    }
    else {
      $this->messenger
        ->addStatus($this
        ->t('You were not subscribed to the list, please try again.'));
    }
  }
}