You are here

function simplenews_tokens in Simplenews 8.2

Same name and namespace in other branches
  1. 8 simplenews.tokens.inc \simplenews_tokens()
  2. 7.2 simplenews.tokens.inc \simplenews_tokens()
  3. 7 simplenews.tokens.inc \simplenews_tokens()
  4. 3.x simplenews.tokens.inc \simplenews_tokens()

Implements hook_tokens().

File

./simplenews.tokens.inc, line 78
Token related hook implementations.

Code

function simplenews_tokens($type, $tokens, $data, $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  $sanitize = !empty($options['sanitize']);
  switch ($type) {
    case 'simplenews-subscriber':
      if (!isset($data['simplenews_subscriber'])) {
        return;
      }

      /** @var \Drupal\simplenews\Entity\Subscriber $subscriber */
      $subscriber = $data['simplenews_subscriber'];
      $newsletter = isset($data['newsletter']) ? $data['newsletter'] : NULL;
      $language = $subscriber
        ->getLangcode();
      $url_arguments = [
        'absolute' => TRUE,
        'langcode' => $language,
      ];
      foreach ($tokens as $name => $original) {
        $route_parameters = [
          'snid' => $subscriber
            ->id(),
          'timestamp' => REQUEST_TIME,
        ];
        switch ($name) {
          case 'subscribe-url':
            $hash = simplenews_generate_hash($subscriber
              ->getMail(), 'add');
            $bubbleable_metadata
              ->setCacheMaxAge(0);
            $route_parameters += [
              'action' => 'add',
              'newsletter_id' => $newsletter
                ->id(),
              'hash' => $hash,
            ];
            $replacements[$original] = Url::fromRoute('simplenews.newsletter_confirm_subscription', $route_parameters, $url_arguments)
              ->toString();
            break;
          case 'unsubscribe-url':
            if (!$subscriber
              ->id()) {

              // Temporary test subscriber.
              $replacements[$original] = t('No unsubscribe link for test subscribers.');
              break;
            }
            $hash = simplenews_generate_hash($subscriber
              ->getMail(), 'remove');
            $bubbleable_metadata
              ->setCacheMaxAge(0);
            $route_parameters += [
              'action' => 'remove',
              'newsletter_id' => $newsletter
                ->id(),
              'hash' => $hash,
            ];
            $replacements[$original] = Url::fromRoute('simplenews.newsletter_confirm_subscription', $route_parameters, $url_arguments)
              ->toString();
            break;
          case 'combined-url':
            $hash = simplenews_generate_hash($subscriber
              ->getMail(), 'combined' . serialize($subscriber
              ->getChanges()));
            $bubbleable_metadata
              ->setCacheMaxAge(0);
            $route_parameters += [
              'hash' => $hash,
            ];
            $replacements[$original] = Url::fromRoute('simplenews.newsletter_confirm_combined', $route_parameters, $url_arguments)
              ->toString();
            break;
          case 'manage-url':
            $hash = simplenews_generate_hash($subscriber
              ->getMail(), 'manage');
            $bubbleable_metadata
              ->setCacheMaxAge(0);
            $route_parameters += [
              'hash' => $hash,
            ];
            $replacements[$original] = Url::fromRoute('simplenews.newsletter_subscriptions_arguments', $route_parameters, $url_arguments)
              ->toString();
            break;
          case 'mail':
            $replacements[$original] = Html::escape($subscriber
              ->getMail());
            break;
        }
      }
      if (($user_tokens = \Drupal::token()
        ->findWithPrefix($tokens, 'user')) && !empty($subscriber->uid)) {
        $replacements += \Drupal::token()
          ->generate('user', $user_tokens, [
          'user' => User::load($subscriber->uid),
        ], $options, $bubbleable_metadata);
      }
      break;
    case 'simplenews-newsletter':
      if (!isset($data['newsletter'])) {
        return;
      }
      $newsletter = $data['newsletter'];
      foreach ($tokens as $name => $original) {
        switch ($name) {
          case 'name':
            if (isset($newsletter->name)) {
              $replacements[$original] = $sanitize ? Html::escape($newsletter->name) : $newsletter->name;
            }
            else {
              $replacements[$original] = t('Unassigned newsletter');
            }
            break;
          case 'url':
            $replacements[$original] = $newsletter
              ->url();
            break;
        }
      }
      break;
  }
  return $replacements;
}