comment.tokens.inc 9.03 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\String;
9 10
use Drupal\Component\Utility\Xss;

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

21 22
  // @todo Make this work per field. See http://drupal.org/node/2031903
  $entity['comment-count'] = array(
23
    'name' => t("Comment count"),
24
    'description' => t("The number of comments posted on an entity."),
25
  );
26
  $entity['comment-count-new'] = array(
27
    'name' => t("New comment count"),
28
    'description' => t("The number of comments posted on an entity since the reader last viewed it."),
29 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  );

  // 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."),
  );
  $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',
  );
71 72 73 74 75
  $comment['changed'] = array(
    'name' => t("Date changed"),
    'description' => t("The date the comment was most recently updated."),
    'type' => 'date',
  );
76 77 78 79 80
  $comment['parent'] = array(
    'name' => t("Parent"),
    'description' => t("The comment's parent, if comment threading is active."),
    'type' => 'comment',
  );
81 82 83 84 85
  $comment['entity'] = array(
    'name' => t("Entity"),
    'description' => t("The entity the comment was posted to."),
    'type' => 'entity',
  );
86
  // Support legacy comment node tokens, since tokens are embedded in user data
87
  // and can't be upgraded directly.
88 89
  $comment['node'] = array(
    'name' => t("Node"),
90
    'description' => t("DEPRECATED: The node the comment was posted to."),
91 92 93 94
    'type' => 'node',
  );
  $comment['author'] = array(
    'name' => t("Author"),
95
    'description' => t("The author name of the comment."),
96 97 98 99 100 101
    'type' => 'user',
  );

  return array(
    'types' => array('comment' => $type),
    'tokens' => array(
102
      'entity' => $entity,
103
      'comment' => $comment,
104 105
      // Support deprecated node tokens.
      'node' => $entity,
106 107 108 109 110
    ),
  );
}

/**
111
 * Implements hook_tokens().
112 113
 */
function comment_tokens($type, $tokens, array $data = array(), array $options = array()) {
114
  $token_service = \Drupal::token();
115

116
  $url_options = array('absolute' => TRUE);
117 118 119
  if (isset($options['langcode'])) {
    $url_options['language'] = language_load($options['langcode']);
    $langcode = $options['langcode'];
120 121
  }
  else {
122
    $langcode = NULL;
123 124 125 126 127 128
  }
  $sanitize = !empty($options['sanitize']);

  $replacements = array();

  if ($type == 'comment' && !empty($data['comment'])) {
129
    /** @var \Drupal\comment\CommentInterface $comment */
130 131 132 133 134 135
    $comment = $data['comment'];

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

139
        // Poster identity information for comments.
140
        case 'hostname':
141
          $replacements[$original] = $sanitize ? String::checkPlain($comment->getHostname()) : $comment->getHostname();
142 143 144
          break;

        case 'mail':
145
          $mail = $comment->getAuthorEmail();
146
          $replacements[$original] = $sanitize ? String::checkPlain($mail) : $mail;
147 148 149
          break;

        case 'homepage':
150
          $replacements[$original] = $sanitize ? check_url($comment->getHomepage()) : $comment->getHomepage();
151 152 153
          break;

        case 'title':
154
          $replacements[$original] = $sanitize ? Xss::filter($comment->getSubject()) : $comment->getSubject();
155 156 157
          break;

        case 'body':
158
          $replacements[$original] = $sanitize ? $comment->comment_body->processed : $comment->comment_body->value;
159 160 161 162
          break;

        // Comment related URLs.
        case 'url':
163
          $url_options['fragment']  = 'comment-' . $comment->id();
164
          $replacements[$original] = $comment->url('canonical', $url_options);
165 166 167
          break;

        case 'edit-url':
168
          $url_options['fragment'] = NULL;
169
          $replacements[$original] = $comment->url('edit-form', $url_options);
170 171 172
          break;

        case 'author':
173
          $name = $comment->getAuthorName();
174
          $replacements[$original] = $sanitize ? Xss::filter($name) : $name;
175 176 177
          break;

        case 'parent':
178 179
          if ($comment->hasParentComment()) {
            $parent = $comment->getParentComment();
180
            $replacements[$original] = $sanitize ? Xss::filter($parent->getSubject()) : $parent->getSubject();
181 182 183 184
          }
          break;

        case 'created':
185
          $replacements[$original] = format_date($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
186 187 188
          break;

        case 'changed':
189
          $replacements[$original] = format_date($comment->getChangedTime(), 'medium', '', NULL, $langcode);
190 191
          break;

192
        case 'entity':
193
          $entity = $comment->getCommentedEntity();
194
          $title = $entity->label();
195
          $replacements[$original] = $sanitize ? Xss::filter($title) : $title;
196
          break;
197 198

        case 'node':
199
          // Support legacy comment node tokens, since tokens are embedded in
200 201
          // user data and can't be upgraded directly.
          // @todo Remove in Drupal 9, see https://drupal.org/node/2031901.
202 203
          if ($comment->getCommentedEntityTypeId() == 'node') {
            $entity = $comment->getCommentedEntity();
204
            $title = $entity->label();
205
            $replacements[$original] = $sanitize ? Xss::filter($title) : $title;
206 207 208 209 210
          }
          else {
            $replacements[$original] = NULL;
          }
          break;
211 212 213 214
      }
    }

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

220 221
    if (($node_tokens = $token_service->findwithPrefix($tokens, 'node')) && $comment->getCommentedEntityTypeId() == 'node') {
      $node = $comment->getCommentedEntity();
222
      $replacements += $token_service->generate('node', $node_tokens, array('node' => $node), $options);
223 224
    }

225
    if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) {
226
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getCreatedTime()), $options);
227 228
    }

229
    if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
230
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getChangedTime()), $options);
231 232
    }

233
    if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->getParentComment()) {
234
      $replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options);
235 236
    }

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

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

        case 'comment-count-new':
260
          $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity);
261 262 263 264 265 266 267
          break;
      }
    }
  }

  return $replacements;
}