You are here

public function CampaignMonitorSubscriptionManager::userSubscribe in Campaign Monitor 8.2

Subscribe user to CM list in real time or by adding to the queue.

1 call to CampaignMonitorSubscriptionManager::userSubscribe()
CampaignMonitorSubscriptionManager::subscribeSubmitHandler in src/CampaignMonitorSubscriptionManager.php
Shared handler for submitting subscriptions.

File

src/CampaignMonitorSubscriptionManager.php, line 417

Class

CampaignMonitorSubscriptionManager
Manager for Campaignmonitor subscriptions.

Namespace

Drupal\campaignmonitor

Code

public function userSubscribe($list_id, $email, $submitted_name, $merge_vars = [], $interests = [], $double_optin = FALSE, $format = 'html') {
  if (empty($submitted_name)) {

    // Load the user object.
    $account = $this->entityTypeManager
      ->getStorage('user')
      ->load($this->account
      ->id());
    $settings = $this->campaignMonitorManager
      ->getListSettings($list_id);
    $fname = $settings['display']['fname_field'];
    $lname = $settings['display']['lname_field'];
    $fields = [
      $fname,
      $lname,
    ];
    $name = [];
    foreach ($fields as $field) {
      if (isset($field) && is_string($field) && $field != '') {
        if ($account
          ->hasField($field)) {
          $value = $account
            ->get($field)
            ->getValue();
          if (isset($value[0]['value']) && $value[0]['value'] != '') {
            $name[] = $account
              ->get($field)
              ->getValue()[0]['value'];
          }
        }
        else {

          // Likely a custom field.
          foreach ($merge_vars as $key => $var) {
            if ($field == $key) {
              $name[] = $var;
            }
          }
        }
      }
    }
    $name = implode(' ', $name);
  }
  else {
    $name = $submitted_name;
  }
  if ($this->config
    ->get('cron')) {
    $args = [
      'list_id' => $list_id,
      'email' => $email,
      'merge_vars' => $merge_vars,
      'interests' => $interests,
      'double_optin' => $double_optin,
      'format' => $format,
    ];
    return $this
      ->addtoQueue('campaignmonitor_subscribe_process', $args);
  }
  return $this
    ->subscribeProcess($list_id, $email, $name, $merge_vars, $interests, $double_optin, $format);
}