comment.tokens.inc 9.3 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 71 72 73 74
  );

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

  return array(
    'types' => array('comment' => $type),
    'tokens' => array(
106
      'entity' => $entity,
107
      'comment' => $comment,
108 109
      // Support deprecated node tokens.
      'node' => $entity,
110 111 112 113 114
    ),
  );
}

/**
115
 * Implements hook_tokens().
116 117
 */
function comment_tokens($type, $tokens, array $data = array(), array $options = array()) {
118
  $token_service = \Drupal::token();
119

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

  $replacements = array();

  if ($type == 'comment' && !empty($data['comment'])) {
133
    /** @var \Drupal\comment\CommentInterface $comment */
134 135 136 137 138 139
    $comment = $data['comment'];

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

143
        // Poster identity information for comments.
144
        case 'hostname':
145
          $replacements[$original] = $sanitize ? String::checkPlain($comment->getHostname()) : $comment->getHostname();
146 147 148
          break;

        case 'mail':
149
          $mail = $comment->getAuthorEmail();
150
          $replacements[$original] = $sanitize ? String::checkPlain($mail) : $mail;
151 152 153
          break;

        case 'homepage':
154
          $replacements[$original] = $sanitize ? check_url($comment->getHomepage()) : $comment->getHomepage();
155 156 157
          break;

        case 'title':
158
          $replacements[$original] = $sanitize ? Xss::filter($comment->getSubject()) : $comment->getSubject();
159 160 161
          break;

        case 'body':
162
          $replacements[$original] = $sanitize ? $comment->comment_body->processed : $comment->comment_body->value;
163 164 165 166
          break;

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

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

176 177 178
        // @todo Remove 'name' token in favour of 'author'. See
        //   https://drupal.org/node/920056.
        case 'name':
179
        case 'author':
180
          $name = $comment->getAuthorName();
181
          $replacements[$original] = $sanitize ? Xss::filter($name) : $name;
182 183 184
          break;

        case 'parent':
185 186
          if ($comment->hasParentComment()) {
            $parent = $comment->getParentComment();
187
            $replacements[$original] = $sanitize ? Xss::filter($parent->getSubject()) : $parent->getSubject();
188 189 190 191
          }
          break;

        case 'created':
192
          $replacements[$original] = format_date($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
193 194 195
          break;

        case 'changed':
196
          $replacements[$original] = format_date($comment->getChangedTime(), 'medium', '', NULL, $langcode);
197 198
          break;

199
        case 'entity':
200
          $entity = $comment->getCommentedEntity();
201
          $title = $entity->label();
202
          $replacements[$original] = $sanitize ? Xss::filter($title) : $title;
203
          break;
204 205

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

    // Chained token relationships.
222
    if ($entity_tokens = $token_service->findwithPrefix($tokens, 'entity')) {
223 224
      $entity = $comment->getCommentedEntity();
      $replacements += $token_service->generate($comment->getCommentedEntityTypeId(), $entity_tokens, array($comment->getCommentedEntityTypeId() => $entity), $options);
225 226
    }

227 228
    if (($node_tokens = $token_service->findwithPrefix($tokens, 'node')) && $comment->getCommentedEntityTypeId() == 'node') {
      $node = $comment->getCommentedEntity();
229
      $replacements += $token_service->generate('node', $node_tokens, array('node' => $node), $options);
230 231
    }

232
    if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) {
233
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getCreatedTime()), $options);
234 235
    }

236
    if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
237
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getChangedTime()), $options);
238 239
    }

240
    if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->getParentComment()) {
241
      $replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options);
242 243
    }

244
    if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->getOwner()) {
245
      $replacements += $token_service->generate('user', $author_tokens, array('user' => $account), $options);
246 247
    }
  }
248 249
  elseif (($type == 'entity' & !empty($data['entity'])) ||
    ($type == 'node' & !empty($data['node']))) {
250
    /** @var $entity \Drupal\Core\Entity\ContentEntityInterface */
251
    $entity = !empty($data['entity']) ? $data['entity'] : $data['node'];
252 253

    foreach ($tokens as $name => $original) {
254
      switch ($name) {
255
        case 'comment-count':
256
          $count = 0;
257
          $fields = array_keys(\Drupal::service('comment.manager')->getFields($entity->getEntityTypeId()));
258
          $definitions = array_keys($entity->getFieldDefinitions());
259 260 261 262 263
          $valid_fields = array_intersect($fields, $definitions);
          foreach ($valid_fields as $field_name) {
            $count += $entity->get($field_name)->comment_count;
          }
          $replacements[$original] = $count;
264 265 266
          break;

        case 'comment-count-new':
267
          $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity);
268 269 270 271 272 273 274
          break;
      }
    }
  }

  return $replacements;
}