comment.tokens.inc 7.78 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 87
  $comment['author'] = array(
    'name' => t("Author"),
88
    'description' => t("The author name of the comment."),
89 90 91 92 93 94
    'type' => 'user',
  );

  return array(
    'types' => array('comment' => $type),
    'tokens' => array(
95
      'entity' => $entity,
96 97 98 99 100 101
      'comment' => $comment,
    ),
  );
}

/**
102
 * Implements hook_tokens().
103 104
 */
function comment_tokens($type, $tokens, array $data = array(), array $options = array()) {
105
  $token_service = \Drupal::token();
106

107
  $url_options = array('absolute' => TRUE);
108 109 110
  if (isset($options['langcode'])) {
    $url_options['language'] = language_load($options['langcode']);
    $langcode = $options['langcode'];
111 112
  }
  else {
113
    $langcode = NULL;
114 115 116 117 118 119
  }
  $sanitize = !empty($options['sanitize']);

  $replacements = array();

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

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

130
        // Poster identity information for comments.
131
        case 'hostname':
132
          $replacements[$original] = $sanitize ? String::checkPlain($comment->getHostname()) : $comment->getHostname();
133 134 135
          break;

        case 'mail':
136
          $mail = $comment->getAuthorEmail();
137
          $replacements[$original] = $sanitize ? String::checkPlain($mail) : $mail;
138 139 140
          break;

        case 'homepage':
141
          $replacements[$original] = $sanitize ? check_url($comment->getHomepage()) : $comment->getHomepage();
142 143 144
          break;

        case 'title':
145
          $replacements[$original] = $sanitize ? Xss::filter($comment->getSubject()) : $comment->getSubject();
146 147 148
          break;

        case 'body':
149
          $replacements[$original] = $sanitize ? $comment->comment_body->processed : $comment->comment_body->value;
150 151 152 153
          break;

        // Comment related URLs.
        case 'url':
154
          $url_options['fragment']  = 'comment-' . $comment->id();
155
          $replacements[$original] = $comment->url('canonical', $url_options);
156 157 158
          break;

        case 'edit-url':
159
          $url_options['fragment'] = NULL;
160
          $replacements[$original] = $comment->url('edit-form', $url_options);
161 162 163
          break;

        case 'author':
164
          $name = $comment->getAuthorName();
165
          $replacements[$original] = $sanitize ? Xss::filter($name) : $name;
166 167 168
          break;

        case 'parent':
169 170
          if ($comment->hasParentComment()) {
            $parent = $comment->getParentComment();
171
            $replacements[$original] = $sanitize ? Xss::filter($parent->getSubject()) : $parent->getSubject();
172 173 174 175
          }
          break;

        case 'created':
176
          $replacements[$original] = format_date($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
177 178 179
          break;

        case 'changed':
180
          $replacements[$original] = format_date($comment->getChangedTime(), 'medium', '', NULL, $langcode);
181 182
          break;

183
        case 'entity':
184
          $entity = $comment->getCommentedEntity();
185
          $title = $entity->label();
186
          $replacements[$original] = $sanitize ? Xss::filter($title) : $title;
187 188 189 190 191
          break;
      }
    }

    // Chained token relationships.
192
    if ($entity_tokens = $token_service->findwithPrefix($tokens, 'entity')) {
193 194
      $entity = $comment->getCommentedEntity();
      $replacements += $token_service->generate($comment->getCommentedEntityTypeId(), $entity_tokens, array($comment->getCommentedEntityTypeId() => $entity), $options);
195 196
    }

197
    if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) {
198
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getCreatedTime()), $options);
199 200
    }

201
    if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
202
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getChangedTime()), $options);
203 204
    }

205
    if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->getParentComment()) {
206
      $replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options);
207 208
    }

209
    if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->getOwner()) {
210
      $replacements += $token_service->generate('user', $author_tokens, array('user' => $account), $options);
211 212
    }
  }
213
  elseif ($type == 'entity' & !empty($data['entity'])) {
214
    /** @var $entity \Drupal\Core\Entity\FieldableEntityInterface */
215
    $entity = $data['entity'];
216 217

    foreach ($tokens as $name => $original) {
218
      switch ($name) {
219
        case 'comment-count':
220
          $count = 0;
221
          $fields = array_keys(\Drupal::service('comment.manager')->getFields($entity->getEntityTypeId()));
222
          $definitions = array_keys($entity->getFieldDefinitions());
223 224 225 226 227
          $valid_fields = array_intersect($fields, $definitions);
          foreach ($valid_fields as $field_name) {
            $count += $entity->get($field_name)->comment_count;
          }
          $replacements[$original] = $count;
228 229 230
          break;

        case 'comment-count-new':
231
          $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity);
232 233 234 235 236 237 238
          break;
      }
    }
  }

  return $replacements;
}