comment.tokens.inc 8.13 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
  );

  // 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."),
  );
56 57 58 59
  $comment['langcode'] = array(
    'name' => t('Language code'),
    'description' => t('The language code of the language the comment is written in.'),
  );
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  $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 91
  $comment['author'] = array(
    'name' => t("Author"),
92
    'description' => t("The author name of the comment."),
93 94 95 96 97 98
    'type' => 'user',
  );

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

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

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

  $replacements = array();

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

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

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

        case 'mail':
140
          $mail = $comment->getAuthorEmail();
141
          $replacements[$original] = $sanitize ? String::checkPlain($mail) : $mail;
142 143 144
          break;

        case 'homepage':
145
          $replacements[$original] = $sanitize ? check_url($comment->getHomepage()) : $comment->getHomepage();
146 147 148
          break;

        case 'title':
149
          $replacements[$original] = $sanitize ? Xss::filter($comment->getSubject()) : $comment->getSubject();
150 151 152
          break;

        case 'body':
153
          $replacements[$original] = $sanitize ? $comment->comment_body->processed : $comment->comment_body->value;
154 155
          break;

156 157 158 159
        case 'langcode':
          $replacements[$original] = $sanitize ? String::checkPlain($comment->language()->getId()) : $comment->language()->getId();
          break;

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

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

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

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

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

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

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

    // Chained token relationships.
200
    if ($entity_tokens = $token_service->findwithPrefix($tokens, 'entity')) {
201 202
      $entity = $comment->getCommentedEntity();
      $replacements += $token_service->generate($comment->getCommentedEntityTypeId(), $entity_tokens, array($comment->getCommentedEntityTypeId() => $entity), $options);
203 204
    }

205
    if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) {
206
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getCreatedTime()), $options);
207 208
    }

209
    if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
210
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getChangedTime()), $options);
211 212
    }

213
    if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->getParentComment()) {
214
      $replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options);
215 216
    }

217
    if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->getOwner()) {
218
      $replacements += $token_service->generate('user', $author_tokens, array('user' => $account), $options);
219 220
    }
  }
221
  elseif ($type == 'entity' & !empty($data['entity'])) {
222
    /** @var $entity \Drupal\Core\Entity\FieldableEntityInterface */
223
    $entity = $data['entity'];
224 225

    foreach ($tokens as $name => $original) {
226
      switch ($name) {
227
        case 'comment-count':
228
          $count = 0;
229
          $fields = array_keys(\Drupal::service('comment.manager')->getFields($entity->getEntityTypeId()));
230
          $definitions = array_keys($entity->getFieldDefinitions());
231 232 233 234 235
          $valid_fields = array_intersect($fields, $definitions);
          foreach ($valid_fields as $field_name) {
            $count += $entity->get($field_name)->comment_count;
          }
          $replacements[$original] = $count;
236 237 238
          break;

        case 'comment-count-new':
239
          $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity);
240 241 242 243 244 245 246
          break;
      }
    }
  }

  return $replacements;
}