comment.tokens.inc 7.59 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 18 19 20 21 22 23 24 25 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
 */
function comment_token_info() {
  $type = array(
    'name' => t('Comments'),
    'description' => t('Tokens for comments posted on the site.'),
    'needs-data' => 'comment',
  );

  // Comment-related tokens for nodes
  $node['comment-count'] = array(
    'name' => t("Comment count"),
    'description' => t("The number of comments posted on a node."),
  );
  $node['comment-count-new'] = array(
    'name' => t("New comment count"),
    'description' => t("The number of comments posted on a node since the reader last viewed it."),
  );

  // 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  $comment['parent'] = array(
    'name' => t("Parent"),
    'description' => t("The comment's parent, if comment threading is active."),
    'type' => 'comment',
  );
  $comment['node'] = array(
    'name' => t("Node"),
    'description' => t("The node the comment was posted to."),
    '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(
      'node' => $node,
      'comment' => $comment,
    ),
  );
}

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

108
  $url_options = array('absolute' => TRUE);
109 110 111
  if (isset($options['langcode'])) {
    $url_options['language'] = language_load($options['langcode']);
    $langcode = $options['langcode'];
112 113
  }
  else {
114
    $langcode = NULL;
115 116 117 118 119 120 121 122 123 124 125 126
  }
  $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':
127
          $replacements[$original] = $comment->id();
128 129 130 131
          break;

        // Poster identity information for comments
        case 'hostname':
132
          $replacements[$original] = $sanitize ? check_plain($comment->hostname->value) : $comment->hostname->value;
133 134 135
          break;

        case 'name':
136
          $name = ($comment->uid->target_id == 0) ? \Drupal::config('user.settings')->get('anonymous') : $comment->name->value;
137 138 139 140
          $replacements[$original] = $sanitize ? filter_xss($name) : $name;
          break;

        case 'mail':
141
          if ($comment->uid->target_id != 0) {
142
            $mail = $comment->uid->entity->getEmail();
143 144
          }
          else {
145
            $mail = $comment->mail->value;
146 147 148 149 150
          }
          $replacements[$original] = $sanitize ? check_plain($mail) : $mail;
          break;

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

        case 'title':
155
          $replacements[$original] = $sanitize ? filter_xss($comment->subject->value) : $comment->subject->value;
156 157 158
          break;

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

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

        case 'edit-url':
169
          $url_options['fragment'] = NULL;
170
          $replacements[$original] = url('comment/' . $comment->id() . '/edit', $url_options);
171 172 173 174
          break;

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

        case 'parent':
179 180
          if (!empty($comment->pid->target_id)) {
            $parent = comment_load($comment->pid->target_id);
181 182 183 184 185
            $replacements[$original] = $sanitize ? filter_xss($parent->subject) : $parent->subject;
          }
          break;

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

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

        case 'node':
194
          $node = $comment->nid->entity;
195
          $title = $node->label();
196
          $replacements[$original] = $sanitize ? filter_xss($title) : $title;
197 198 199 200 201
          break;
      }
    }

    // Chained token relationships.
202
    if ($node_tokens = $token_service->findwithPrefix($tokens, 'node')) {
203
      $node = $comment->nid->entity;
204
      $replacements += $token_service->generate('node', $node_tokens, array('node' => $node), $options);
205 206
    }

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

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

215 216
    if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->pid->entity) {
      $replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options);
217 218
    }

219
    if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->uid->entity) {
220
      $replacements += $token_service->generate('user', $author_tokens, array('user' => $account), $options);
221 222 223 224 225 226 227 228 229 230 231 232
    }
  }
  elseif ($type == 'node' & !empty($data['node'])) {
    $node = $data['node'];

    foreach ($tokens as $name => $original) {
      switch($name) {
        case 'comment-count':
          $replacements[$original] = $node->comment_count;
          break;

        case 'comment-count-new':
233
          $replacements[$original] = comment_num_new($node->id());
234 235 236 237 238 239 240
          break;
      }
    }
  }

  return $replacements;
}