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

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

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

18 19
  // @todo Make this work per field. See http://drupal.org/node/2031903
  $entity['comment-count'] = array(
20
    'name' => t("Comment count"),
21
    'description' => t("The number of comments posted on an entity."),
22
  );
23
  $entity['comment-count-new'] = array(
24
    'name' => t("New comment count"),
25
    'description' => t("The number of comments posted on an entity since the reader last viewed it."),
26 27 28 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
  );

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

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

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

  $replacements = array();

  if ($type == 'comment' && !empty($data['comment'])) {
    $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 139 140
          break;

        // Poster identity information for comments
        case 'hostname':
141
          $replacements[$original] = $sanitize ? check_plain($comment->hostname->value) : $comment->hostname->value;
142 143 144
          break;

        case 'name':
145
          $name = ($comment->uid->target_id == 0) ? \Drupal::config('user.settings')->get('anonymous') : $comment->name->value;
146 147 148 149
          $replacements[$original] = $sanitize ? filter_xss($name) : $name;
          break;

        case 'mail':
150
          if ($comment->uid->target_id != 0) {
151
            $mail = $comment->uid->entity->getEmail();
152 153
          }
          else {
154
            $mail = $comment->mail->value;
155 156 157 158 159
          }
          $replacements[$original] = $sanitize ? check_plain($mail) : $mail;
          break;

        case 'homepage':
160
          $replacements[$original] = $sanitize ? check_url($comment->homepage->value) : $comment->homepage->value;
161 162 163
          break;

        case 'title':
164
          $replacements[$original] = $sanitize ? filter_xss($comment->subject->value) : $comment->subject->value;
165 166 167
          break;

        case 'body':
168
          $replacements[$original] = $sanitize ? $comment->comment_body->processed : $comment->comment_body->value;
169 170 171 172
          break;

        // Comment related URLs.
        case 'url':
173 174
          $url_options['fragment']  = 'comment-' . $comment->id();
          $replacements[$original] = url('comment/' . $comment->id(), $url_options);
175 176 177
          break;

        case 'edit-url':
178
          $url_options['fragment'] = NULL;
179
          $replacements[$original] = url('comment/' . $comment->id() . '/edit', $url_options);
180 181 182 183
          break;

        // Default values for the chained tokens handled below.
        case 'author':
184
          $replacements[$original] = $sanitize ? filter_xss($comment->name->value) : $comment->name->value;
185 186 187
          break;

        case 'parent':
188
          if (!empty($comment->pid->target_id)) {
189 190
            $parent = entity_load('comment', $comment->pid->target_id);
            $replacements[$original] = $sanitize ? filter_xss($parent->subject->value) : $parent->subject->value;
191 192 193 194
          }
          break;

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

        case 'changed':
199
          $replacements[$original] = format_date($comment->changed->value, 'medium', '', NULL, $langcode);
200 201
          break;

202 203 204
        case 'entity':
          $entity = entity_load($comment->entity_type->value, $comment->entity_id->value);
          $title = $entity->label();
205
          $replacements[$original] = $sanitize ? filter_xss($title) : $title;
206
          break;
207 208 209 210 211 212 213 214 215 216 217 218 219 220

        case 'node':
          // Support legacy comment node tokens, since tokes are embedded in
          // user data and can't be upgraded directly.
          // @todo Remove in Drupal 9, see https://drupal.org/node/2031901.
          if ($comment->entity_type->value == 'node') {
            $entity = entity_load($comment->entity_type->value, $comment->entity_id->value);
            $title = $entity->label();
            $replacements[$original] = $sanitize ? filter_xss($title) : $title;
          }
          else {
            $replacements[$original] = NULL;
          }
          break;
221 222 223 224
      }
    }

    // Chained token relationships.
225 226 227 228 229 230 231
    if ($entity_tokens = $token_service->findwithPrefix($tokens, 'entity')) {
      $entity = entity_load($comment->entity_type->value, $comment->entity_id->value);
      $replacements += $token_service->generate($comment->entity_type->value, $entity_tokens, array($comment->entity_type->value => $entity), $options);
    }

    if (($node_tokens = $token_service->findwithPrefix($tokens, 'node')) && $comment->entity_type->value == 'node') {
      $node = entity_load($comment->entity_type->value, $comment->entity_id->value);
232
      $replacements += $token_service->generate('node', $node_tokens, array('node' => $node), $options);
233 234
    }

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

239 240
    if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
      $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->changed->value), $options);
241 242
    }

243 244
    if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->pid->entity) {
      $replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options);
245 246
    }

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

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

        case 'comment-count-new':
270
          $replacements[$original] = comment_num_new($entity->id(), $entity->getEntityTypeId());
271 272 273 274 275 276 277
          break;
      }
    }
  }

  return $replacements;
}