You are here

public function SubscriptionManager::getChangesList in Simplenews 8.2

Same name and namespace in other branches
  1. 8 src/Subscription/SubscriptionManager.php \Drupal\simplenews\Subscription\SubscriptionManager::getChangesList()

Converts an array of subscription changes into descriptions.

Parameters

\Drupal\simplenews\SubscriberInterface $subscriber: Simplenews subscriber object.

array $changes: (Optional) Array of changes, each is an array with the keys action and newsletter_id. Defaults to $subscriber->getChanges(), which contains the currently saved changes for the subscriber.

string $langcode: (Optional) Specify the language of the description strings, defaults to the current language.

Return value

string[] Array of description strings describing the changes.

Overrides SubscriptionManagerInterface::getChangesList

File

src/Subscription/SubscriptionManager.php, line 198

Class

SubscriptionManager
Default subscription manager.

Namespace

Drupal\simplenews\Subscription

Code

public function getChangesList(SubscriberInterface $subscriber, array $changes = NULL, $langcode = NULL) {
  if (empty($langcode)) {
    $language = $this->languageManager
      ->getCurrentLanguage();
    $langcode = $language
      ->getId();
  }
  if (empty($changes)) {
    $changes = $subscriber
      ->getChanges();
  }
  $changes_list = [];
  foreach ($changes as $newsletter_id => $action) {
    $subscribed = $subscriber
      ->isSubscribed($newsletter_id);

    // Get text for each possible combination.
    if ($action == 'subscribe' && !$subscribed) {
      $line = $this->config
        ->get('subscription.confirm_combined_line_subscribe_unsubscribed');
    }
    elseif ($action == 'subscribe' && $subscribed) {
      $line = $this->config
        ->get('subscription.confirm_combined_line_subscribe_subscribed');
    }
    elseif ($action == 'unsubscribe' && !$subscribed) {
      $line = $this->config
        ->get('subscription.confirm_combined_line_unsubscribe_unsubscribed');
    }
    elseif ($action == 'unsubscribe' && $subscribed) {
      $line = $this->config
        ->get('subscription.confirm_combined_line_unsubscribe_subscribed');
    }
    $newsletter_context = [
      'simplenews_subscriber' => $subscriber,
      'newsletter' => Newsletter::load($newsletter_id),
    ];
    $changes_list[$newsletter_id] = $this->token
      ->replace($line, $newsletter_context, [
      'sanitize' => FALSE,
    ]);
  }
  return $changes_list;
}