You are here

function mailing_list_tokens in Mailing List 8

Implements hook_tokens().

File

./mailing_list.tokens.inc, line 121
Token related hook implementations.

Code

function mailing_list_tokens($type, $tokens, $data, $options, BubbleableMetadata $bubbleable_metadata) {
  if ($type != 'mailing-list-subscription' || !isset($data['mailing_list_subscription'])) {
    return [];
  }
  $url_options = [
    'absolute' => TRUE,
  ];
  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()
      ->getLanguage($options['langcode']);
    $langcode = $options['langcode'];
  }
  else {
    $langcode = LanguageInterface::LANGCODE_DEFAULT;
  }
  $replacements = [];

  /** @var \Drupal\mailing_list\SubscriptionInterface $subscription */
  $subscription = $data['mailing_list_subscription'];
  foreach ($tokens as $name => $original) {
    switch ($name) {
      case 'sid':
        $replacements[$original] = $subscription
          ->id();
        break;
      case 'list':
        $replacements[$original] = $subscription
          ->getListId();
        break;
      case 'list-name':
        $replacements[$original] = $subscription
          ->getList()
          ->label();
        break;
      case 'title':
        $replacements[$original] = $subscription
          ->getTitle();
        break;
      case 'email':
        $replacements[$original] = $subscription
          ->getEmail();
        break;
      case 'obfuscated-email':
        $replacements[$original] = $subscription
          ->getEmail(TRUE);
        break;
      case 'langcode':
        $replacements[$original] = $subscription
          ->language()
          ->getId();
        break;
      case 'url':
        $replacements[$original] = $subscription
          ->url('canonical', $url_options);
        break;
      case 'edit-url':
        $replacements[$original] = $subscription
          ->url('edit-form', $url_options);
        break;
      case 'cancel-url':
        $replacements[$original] = $subscription
          ->url('delete-form', $url_options);
        break;
      case 'unrestricted-view-url':
        $replacements[$original] = Url::fromRoute('mailing_list.access_subscription', [
          'sid' => $subscription
            ->id(),
          'hash' => $subscription
            ->getAccessHash(),
          'rel' => 'view',
        ], $url_options)
          ->toString();
        break;
      case 'unrestricted-edit-url':
        $replacements[$original] = Url::fromRoute('mailing_list.access_subscription', [
          'sid' => $subscription
            ->id(),
          'hash' => $subscription
            ->getAccessHash(),
          'rel' => 'edit-form',
        ], $url_options)
          ->toString();
        break;
      case 'unrestricted-cancel-url':
        $replacements[$original] = Url::fromRoute('mailing_list.access_subscription', [
          'sid' => $subscription
            ->id(),
          'hash' => $subscription
            ->getAccessHash(),
          'rel' => 'delete-form',
        ], $url_options)
          ->toString();
        break;
      case 'unrestricted-manage-url':
        $replacements[$original] = Url::fromRoute('mailing_list.access_subscription', [
          'sid' => $subscription
            ->id(),
          'hash' => $subscription
            ->getAccessHash(),
          'rel' => 'manage',
        ], $url_options)
          ->toString();
        break;

      // Default values for the chained tokens handled below.
      case 'owner':
        $account = $subscription
          ->getOwner() ? $subscription
          ->getOwner() : User::getAnonymousUser();
        $bubbleable_metadata
          ->addCacheableDependency($account);
        $replacements[$original] = $account
          ->label();
        break;
      case 'created':
        $date_format = DateFormat::load('medium');
        $bubbleable_metadata
          ->addCacheableDependency($date_format);
        $replacements[$original] = format_date($subscription
          ->getCreatedTime(), 'medium', '', NULL, $langcode);
        break;
      case 'changed':
        $date_format = DateFormat::load('medium');
        $bubbleable_metadata
          ->addCacheableDependency($date_format);
        $replacements[$original] = format_date($subscription
          ->getChangedTime(), 'medium', '', NULL, $langcode);
        break;
    }
  }
  $token_service = \Drupal::token();
  if ($owner_tokens = $token_service
    ->findWithPrefix($tokens, 'owner')) {
    $replacements += $token_service
      ->generate('user', $owner_tokens, [
      'user' => $subscription
        ->getOwner(),
    ], $options, $bubbleable_metadata);
  }
  if ($created_tokens = $token_service
    ->findWithPrefix($tokens, 'created')) {
    $replacements += $token_service
      ->generate('date', $created_tokens, [
      'date' => $subscription
        ->getCreatedTime(),
    ], $options, $bubbleable_metadata);
  }
  if ($changed_tokens = $token_service
    ->findWithPrefix($tokens, 'changed')) {
    $replacements += $token_service
      ->generate('date', $changed_tokens, [
      'date' => $subscription
        ->getChangedTime(),
    ], $options, $bubbleable_metadata);
  }
  return $replacements;
}