comment.tokens.inc 8.87 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Builds placeholder replacement tokens for comment-related data.
 */

8
use Drupal\Component\Utility\UrlHelper;
9 10
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Render\BubbleableMetadata;
11

12
/**
13
 * Implements hook_token_info().
14 15 16 17 18 19 20 21
 */
function comment_token_info() {
  $type = array(
    'name' => t('Comments'),
    'description' => t('Tokens for comments posted on the site.'),
    'needs-data' => 'comment',
  );

22
  // @todo Make this work per field. See https://www.drupal.org/node/2031903.
23
  $entity['comment-count'] = array(
24
    'name' => t("Comment count"),
25
    'description' => t("The number of comments posted on an entity."),
26
  );
27
  $entity['comment-count-new'] = array(
28
    'name' => t("New comment count"),
29
    'description' => t("The number of comments posted on an entity since the reader last viewed it."),
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  );

  // Core comment tokens
  $comment['cid'] = array(
    'name' => t("Comment ID"),
    'description' => t("The unique ID of the comment."),
  );
  $comment['hostname'] = array(
    'name' => t("IP Address"),
    'description' => t("The IP address of the computer the comment was posted from."),
  );
  $comment['mail'] = array(
    'name' => t("Email address"),
    'description' => t("The email address left by the comment author."),
  );
  $comment['homepage'] = array(
    'name' => t("Home page"),
    'description' => t("The home page URL left by the comment author."),
  );
  $comment['title'] = array(
    'name' => t("Title"),
    'description' => t("The title of the comment."),
  );
  $comment['body'] = array(
    'name' => t("Content"),
    'description' => t("The formatted content of the comment itself."),
  );
57 58 59 60
  $comment['langcode'] = array(
    'name' => t('Language code'),
    'description' => t('The language code of the language the comment is written in.'),
  );
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  $comment['url'] = array(
    'name' => t("URL"),
    'description' => t("The URL of the comment."),
  );
  $comment['edit-url'] = array(
    'name' => t("Edit URL"),
    'description' => t("The URL of the comment's edit page."),
  );

  // Chained tokens for comments
  $comment['created'] = array(
    'name' => t("Date created"),
    'description' => t("The date the comment was posted."),
    'type' => 'date',
  );
76 77 78 79 80
  $comment['changed'] = array(
    'name' => t("Date changed"),
    'description' => t("The date the comment was most recently updated."),
    'type' => 'date',
  );
81 82 83 84 85
  $comment['parent'] = array(
    'name' => t("Parent"),
    'description' => t("The comment's parent, if comment threading is active."),
    'type' => 'comment',
  );
86 87 88 89 90
  $comment['entity'] = array(
    'name' => t("Entity"),
    'description' => t("The entity the comment was posted to."),
    'type' => 'entity',
  );
91 92
  $comment['author'] = array(
    'name' => t("Author"),
93
    'description' => t("The author name of the comment."),
94 95 96 97 98 99
    'type' => 'user',
  );

  return array(
    'types' => array('comment' => $type),
    'tokens' => array(
100
      'entity' => $entity,
101 102 103 104 105 106
      'comment' => $comment,
    ),
  );
}

/**
107
 * Implements hook_tokens().
108
 */
109
function comment_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
110
  $token_service = \Drupal::token();
111

112
  $url_options = array('absolute' => TRUE);
113
  if (isset($options['langcode'])) {
114
    $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
115
    $langcode = $options['langcode'];
116 117
  }
  else {
118
    $langcode = NULL;
119 120 121 122
  }
  $replacements = array();

  if ($type == 'comment' && !empty($data['comment'])) {
123
    /** @var \Drupal\comment\CommentInterface $comment */
124 125 126 127 128 129
    $comment = $data['comment'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        // Simple key values on the comment.
        case 'cid':
130
          $replacements[$original] = $comment->id();
131 132
          break;

133
        // Poster identity information for comments.
134
        case 'hostname':
135
          $replacements[$original] = $comment->getHostname();
136 137 138
          break;

        case 'mail':
139
          $mail = $comment->getAuthorEmail();
140 141 142 143 144
          // Add the user cacheability metadata in case the author of the comment
          // is not the anonymous user.
          if ($comment->getOwnerId()) {
            $bubbleable_metadata->addCacheableDependency($comment->getOwner());
          }
145
          $replacements[$original] = $mail;
146 147 148
          break;

        case 'homepage':
149
          $replacements[$original] = UrlHelper::stripDangerousProtocols($comment->getHomepage());
150 151 152
          break;

        case 'title':
153
          $replacements[$original] = $comment->getSubject();
154 155 156
          break;

        case 'body':
157 158
          // "processed" returns a \Drupal\Component\Render\MarkupInterface via
          // check_markup().
159
          $replacements[$original] = $comment->comment_body->processed;
160 161
          break;

162
        case 'langcode':
163
          $replacements[$original] = $comment->language()->getId();
164 165
          break;

166 167
        // Comment related URLs.
        case 'url':
168
          $url_options['fragment']  = 'comment-' . $comment->id();
169
          $replacements[$original] = $comment->url('canonical', $url_options);
170 171 172
          break;

        case 'edit-url':
173
          $url_options['fragment'] = NULL;
174
          $replacements[$original] = $comment->url('edit-form', $url_options);
175 176 177
          break;

        case 'author':
178
          $name = $comment->getAuthorName();
179 180 181 182 183
          // Add the user cacheability metadata in case the author of the comment
          // is not the anonymous user.
          if ($comment->getOwnerId()) {
            $bubbleable_metadata->addCacheableDependency($comment->getOwner());
          }
184
          $replacements[$original] = $name;
185 186 187
          break;

        case 'parent':
188 189
          if ($comment->hasParentComment()) {
            $parent = $comment->getParentComment();
190
            $bubbleable_metadata->addCacheableDependency($parent);
191
            $replacements[$original] = $parent->getSubject();
192 193 194 195
          }
          break;

        case 'created':
196 197
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata->addCacheableDependency($date_format);
198
          $replacements[$original] = format_date($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
199 200 201
          break;

        case 'changed':
202 203
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata->addCacheableDependency($date_format);
204
          $replacements[$original] = format_date($comment->getChangedTime(), 'medium', '', NULL, $langcode);
205 206
          break;

207
        case 'entity':
208
          $entity = $comment->getCommentedEntity();
209
          $bubbleable_metadata->addCacheableDependency($entity);
210
          $title = $entity->label();
211
          $replacements[$original] = $title;
212 213 214 215 216
          break;
      }
    }

    // Chained token relationships.
217
    if ($entity_tokens = $token_service->findwithPrefix($tokens, 'entity')) {
218
      $entity = $comment->getCommentedEntity();
219
      $replacements += $token_service->generate($comment->getCommentedEntityTypeId(), $entity_tokens, array($comment->getCommentedEntityTypeId() => $entity), $options, $bubbleable_metadata);
220 221
    }

222
    if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) {
223
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getCreatedTime()), $options, $bubbleable_metadata);
224 225
    }

226
    if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
227
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getChangedTime()), $options, $bubbleable_metadata);
228 229
    }

230
    if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->getParentComment()) {
231
      $replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options, $bubbleable_metadata);
232 233
    }

234
    if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->getOwner()) {
235
      $replacements += $token_service->generate('user', $author_tokens, array('user' => $account), $options, $bubbleable_metadata);
236 237
    }
  }
238
  elseif ($type == 'entity' & !empty($data['entity'])) {
239
    /** @var $entity \Drupal\Core\Entity\FieldableEntityInterface */
240
    $entity = $data['entity'];
241 242

    foreach ($tokens as $name => $original) {
243
      switch ($name) {
244
        case 'comment-count':
245
          $count = 0;
246
          $fields = array_keys(\Drupal::service('comment.manager')->getFields($entity->getEntityTypeId()));
247
          $definitions = array_keys($entity->getFieldDefinitions());
248 249 250 251 252
          $valid_fields = array_intersect($fields, $definitions);
          foreach ($valid_fields as $field_name) {
            $count += $entity->get($field_name)->comment_count;
          }
          $replacements[$original] = $count;
253 254 255
          break;

        case 'comment-count-new':
256
          $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity);
257 258 259 260 261 262 263
          break;
      }
    }
  }

  return $replacements;
}