You are here

function campaignmonitor_subscribe_process in Campaign Monitor 8

1 call to campaignmonitor_subscribe_process()
campaignmonitor_subscribe in ./campaignmonitor.module
Subscribe a user to a CampaignMonitor list in real time or by adding to the queue.
1 string reference to 'campaignmonitor_subscribe_process'
campaignmonitor_subscribe in ./campaignmonitor.module
Subscribe a user to a CampaignMonitor list in real time or by adding to the queue.

File

./campaignmonitor.module, line 360
Module that plugs in Campaign Monitor functionality to your Drupal web site. For Campaign Monitor information see: http://www.campaignmonitor.com/.

Code

function campaignmonitor_subscribe_process($list_id, $email, $name, $CustomFields = NULL, $interests = [], $double_optin = FALSE, $format = 'html', $sanitize_custom = TRUE) {
  $config = \Drupal::config('campaignmonitor.settings');
  $result = FALSE;
  try {
    $cm = CampaignMonitor::getConnector();
    $custom_fields = [];
    if (isset($CustomFields)) {
      foreach ($CustomFields as $key => $field) {
        if (is_array($field)) {

          // Filter out non-selected values.
          $field = array_filter($field);

          // Transform two level array into one level.
          foreach ($field as $value) {
            $custom_fields[] = [
              'Key' => $sanitize_custom ? SafeMarkup::checkPlain($key) : $key,
              'Value' => $sanitize_custom ? SafeMarkup::checkPlain($value) : $value,
            ];
          }
        }
        else {

          // Add non-array custom fields.
          $custom_fields[] = [
            'Key' => $sanitize_custom ? SafeMarkup::checkPlain($key) : $key,
            'Value' => $sanitize_custom ? SafeMarkup::checkPlain($field) : $field,
          ];
        }
      }
    }

    // Update subscriber information or add new subscriber to the list.
    if ($cm
      ->subscribe($list_id, $email, $name, $custom_fields)) {
      campaignmonitor_cache_clear_subscriber($list_id, $email);
      Drupal::moduleHandler()
        ->invokeAll('campaignmonitor_subscribe', [
        $list_id,
        $email,
      ]);
      return TRUE;
    }
    else {
      return FALSE;
    }

    // // Check if the user should be sent to a subscribe page.
    //    $lists = $cm->getLists();
    //    if (isset($lists[$list_id]['details']['ConfirmationSuccessPage']) && !empty($lists[$list_id]['details']['ConfirmationSuccessPage'])) {
    //      drupal_goto($lists[$list_id]['details']['ConfirmationSuccessPage']);
    //    }
    //    else {
    //      drupal_set_message(t('You are now subscribed to the "@list" list.', array('@list' => $lists[$list_id]['name'])), 'status');
    //    }.
  } catch (Exception $e) {
    \Drupal::logger('campaignmonitor')
      ->error('An error occurred subscribing {email} to list {list}. "{message}"', [
      'email' => $email,
      'list' => $list_id,
      'message' => $e
        ->getMessage(),
    ]);
  }
  return $result;
}