comment.tokens.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for comment-related data.
  5. */
  6. use Drupal\Component\Utility\UrlHelper;
  7. use Drupal\Core\Datetime\Entity\DateFormat;
  8. use Drupal\Core\Render\BubbleableMetadata;
  9. /**
  10. * Implements hook_token_info().
  11. */
  12. function comment_token_info() {
  13. $type = array(
  14. 'name' => t('Comments'),
  15. 'description' => t('Tokens for comments posted on the site.'),
  16. 'needs-data' => 'comment',
  17. );
  18. // @todo Make this work per field. See https://www.drupal.org/node/2031903.
  19. $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. // Core comment tokens
  28. $comment['cid'] = array(
  29. 'name' => t("Comment ID"),
  30. 'description' => t("The unique ID of the comment."),
  31. );
  32. $comment['hostname'] = array(
  33. 'name' => t("IP Address"),
  34. 'description' => t("The IP address of the computer the comment was posted from."),
  35. );
  36. $comment['mail'] = array(
  37. 'name' => t("Email address"),
  38. 'description' => t("The email address left by the comment author."),
  39. );
  40. $comment['homepage'] = array(
  41. 'name' => t("Home page"),
  42. 'description' => t("The home page URL left by the comment author."),
  43. );
  44. $comment['title'] = array(
  45. 'name' => t("Title"),
  46. 'description' => t("The title of the comment."),
  47. );
  48. $comment['body'] = array(
  49. 'name' => t("Content"),
  50. 'description' => t("The formatted content of the comment itself."),
  51. );
  52. $comment['langcode'] = array(
  53. 'name' => t('Language code'),
  54. 'description' => t('The language code of the language the comment is written in.'),
  55. );
  56. $comment['url'] = array(
  57. 'name' => t("URL"),
  58. 'description' => t("The URL of the comment."),
  59. );
  60. $comment['edit-url'] = array(
  61. 'name' => t("Edit URL"),
  62. 'description' => t("The URL of the comment's edit page."),
  63. );
  64. // Chained tokens for comments
  65. $comment['created'] = array(
  66. 'name' => t("Date created"),
  67. 'description' => t("The date the comment was posted."),
  68. 'type' => 'date',
  69. );
  70. $comment['changed'] = array(
  71. 'name' => t("Date changed"),
  72. 'description' => t("The date the comment was most recently updated."),
  73. 'type' => 'date',
  74. );
  75. $comment['parent'] = array(
  76. 'name' => t("Parent"),
  77. 'description' => t("The comment's parent, if comment threading is active."),
  78. 'type' => 'comment',
  79. );
  80. $comment['entity'] = array(
  81. 'name' => t("Entity"),
  82. 'description' => t("The entity the comment was posted to."),
  83. 'type' => 'entity',
  84. );
  85. $comment['author'] = array(
  86. 'name' => t("Author"),
  87. 'description' => t("The author name of the comment."),
  88. 'type' => 'user',
  89. );
  90. return array(
  91. 'types' => array('comment' => $type),
  92. 'tokens' => array(
  93. 'entity' => $entity,
  94. 'comment' => $comment,
  95. ),
  96. );
  97. }
  98. /**
  99. * Implements hook_tokens().
  100. */
  101. function comment_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  102. $token_service = \Drupal::token();
  103. $url_options = array('absolute' => TRUE);
  104. if (isset($options['langcode'])) {
  105. $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
  106. $langcode = $options['langcode'];
  107. }
  108. else {
  109. $langcode = NULL;
  110. }
  111. $replacements = array();
  112. if ($type == 'comment' && !empty($data['comment'])) {
  113. /** @var \Drupal\comment\CommentInterface $comment */
  114. $comment = $data['comment'];
  115. foreach ($tokens as $name => $original) {
  116. switch ($name) {
  117. // Simple key values on the comment.
  118. case 'cid':
  119. $replacements[$original] = $comment->id();
  120. break;
  121. // Poster identity information for comments.
  122. case 'hostname':
  123. $replacements[$original] = $comment->getHostname();
  124. break;
  125. case 'mail':
  126. $mail = $comment->getAuthorEmail();
  127. // Add the user cacheability metadata in case the author of the comment
  128. // is not the anonymous user.
  129. if ($comment->getOwnerId()) {
  130. $bubbleable_metadata->addCacheableDependency($comment->getOwner());
  131. }
  132. $replacements[$original] = $mail;
  133. break;
  134. case 'homepage':
  135. $replacements[$original] = UrlHelper::stripDangerousProtocols($comment->getHomepage());
  136. break;
  137. case 'title':
  138. $replacements[$original] = $comment->getSubject();
  139. break;
  140. case 'body':
  141. // "processed" returns a \Drupal\Component\Render\MarkupInterface via
  142. // check_markup().
  143. $replacements[$original] = $comment->comment_body->processed;
  144. break;
  145. case 'langcode':
  146. $replacements[$original] = $comment->language()->getId();
  147. break;
  148. // Comment related URLs.
  149. case 'url':
  150. $url_options['fragment'] = 'comment-' . $comment->id();
  151. $replacements[$original] = $comment->url('canonical', $url_options);
  152. break;
  153. case 'edit-url':
  154. $url_options['fragment'] = NULL;
  155. $replacements[$original] = $comment->url('edit-form', $url_options);
  156. break;
  157. case 'author':
  158. $name = $comment->getAuthorName();
  159. // Add the user cacheability metadata in case the author of the comment
  160. // is not the anonymous user.
  161. if ($comment->getOwnerId()) {
  162. $bubbleable_metadata->addCacheableDependency($comment->getOwner());
  163. }
  164. $replacements[$original] = $name;
  165. break;
  166. case 'parent':
  167. if ($comment->hasParentComment()) {
  168. $parent = $comment->getParentComment();
  169. $bubbleable_metadata->addCacheableDependency($parent);
  170. $replacements[$original] = $parent->getSubject();
  171. }
  172. break;
  173. case 'created':
  174. $date_format = DateFormat::load('medium');
  175. $bubbleable_metadata->addCacheableDependency($date_format);
  176. $replacements[$original] = format_date($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
  177. break;
  178. case 'changed':
  179. $date_format = DateFormat::load('medium');
  180. $bubbleable_metadata->addCacheableDependency($date_format);
  181. $replacements[$original] = format_date($comment->getChangedTime(), 'medium', '', NULL, $langcode);
  182. break;
  183. case 'entity':
  184. $entity = $comment->getCommentedEntity();
  185. $bubbleable_metadata->addCacheableDependency($entity);
  186. $title = $entity->label();
  187. $replacements[$original] = $title;
  188. break;
  189. }
  190. }
  191. // Chained token relationships.
  192. if ($entity_tokens = $token_service->findwithPrefix($tokens, 'entity')) {
  193. $entity = $comment->getCommentedEntity();
  194. $replacements += $token_service->generate($comment->getCommentedEntityTypeId(), $entity_tokens, array($comment->getCommentedEntityTypeId() => $entity), $options, $bubbleable_metadata);
  195. }
  196. if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) {
  197. $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getCreatedTime()), $options, $bubbleable_metadata);
  198. }
  199. if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
  200. $replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getChangedTime()), $options, $bubbleable_metadata);
  201. }
  202. if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->getParentComment()) {
  203. $replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options, $bubbleable_metadata);
  204. }
  205. if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->getOwner()) {
  206. $replacements += $token_service->generate('user', $author_tokens, array('user' => $account), $options, $bubbleable_metadata);
  207. }
  208. }
  209. elseif ($type == 'entity' & !empty($data['entity'])) {
  210. /** @var $entity \Drupal\Core\Entity\FieldableEntityInterface */
  211. $entity = $data['entity'];
  212. foreach ($tokens as $name => $original) {
  213. switch ($name) {
  214. case 'comment-count':
  215. $count = 0;
  216. $fields = array_keys(\Drupal::service('comment.manager')->getFields($entity->getEntityTypeId()));
  217. $definitions = array_keys($entity->getFieldDefinitions());
  218. $valid_fields = array_intersect($fields, $definitions);
  219. foreach ($valid_fields as $field_name) {
  220. $count += $entity->get($field_name)->comment_count;
  221. }
  222. $replacements[$original] = $count;
  223. break;
  224. case 'comment-count-new':
  225. $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity);
  226. break;
  227. }
  228. }
  229. }
  230. return $replacements;
  231. }