5 * Builds placeholder replacement tokens for comment-related data.
8 use Drupal\Component\Utility\UrlHelper;
9 use Drupal\Core\Datetime\Entity\DateFormat;
10 use Drupal\Core\Entity\ContentEntityInterface;
11 use Drupal\Core\Entity\FieldableEntityInterface;
12 use Drupal\Core\Render\BubbleableMetadata;
15 * Implements hook_token_info().
17 function comment_token_info() {
19 'name' => t('Comments'),
20 'description' => t('Tokens for comments posted on the site.'),
21 'needs-data' => 'comment',
25 // Provide a integration for each entity type except comment.
26 foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) {
27 if ($entity_type_id == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class)) {
31 if (\Drupal::service('comment.manager')->getFields($entity_type_id)) {
32 // Get the correct token type.
33 $token_type = ($entity_type_id == 'taxonomy_term') ? 'term' : $entity_type_id;
35 // @todo Make this work per field. See https://www.drupal.org/node/2031903.
36 $tokens[$token_type]['comment-count'] = [
37 'name' => t("Comment count"),
38 'description' => t("The number of comments posted on an entity."),
40 $tokens[$token_type]['comment-count-new'] = [
41 'name' => t("New comment count"),
42 'description' => t("The number of comments posted on an entity since the reader last viewed it."),
47 // Core comment tokens
49 'name' => t("Comment ID"),
50 'description' => t("The unique ID of the comment."),
52 $comment['hostname'] = [
53 'name' => t("IP Address"),
54 'description' => t("The IP address of the computer the comment was posted from."),
57 'name' => t("Email address"),
58 'description' => t("The email address left by the comment author."),
60 $comment['homepage'] = [
61 'name' => t("Home page"),
62 'description' => t("The home page URL left by the comment author."),
66 'description' => t("The title of the comment."),
69 'name' => t("Content"),
70 'description' => t("The formatted content of the comment itself."),
72 $comment['langcode'] = [
73 'name' => t('Language code'),
74 'description' => t('The language code of the language the comment is written in.'),
78 'description' => t("The URL of the comment."),
80 $comment['edit-url'] = [
81 'name' => t("Edit URL"),
82 'description' => t("The URL of the comment's edit page."),
85 // Chained tokens for comments
86 $comment['created'] = [
87 'name' => t("Date created"),
88 'description' => t("The date the comment was posted."),
91 $comment['changed'] = [
92 'name' => t("Date changed"),
93 'description' => t("The date the comment was most recently updated."),
96 $comment['parent'] = [
97 'name' => t("Parent"),
98 'description' => t("The comment's parent, if comment threading is active."),
101 $comment['entity'] = [
102 'name' => t("Entity"),
103 'description' => t("The entity the comment was posted to."),
106 $comment['author'] = [
107 'name' => t("Author"),
108 'description' => t("The author name of the comment."),
113 'types' => ['comment' => $type],
115 'comment' => $comment,
121 * Implements hook_tokens().
123 function comment_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
124 $token_service = \Drupal::token();
126 $url_options = ['absolute' => TRUE];
127 if (isset($options['langcode'])) {
128 $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
129 $langcode = $options['langcode'];
136 if ($type == 'comment' && !empty($data['comment'])) {
137 /** @var \Drupal\comment\CommentInterface $comment */
138 $comment = $data['comment'];
140 foreach ($tokens as $name => $original) {
142 // Simple key values on the comment.
144 $replacements[$original] = $comment->id();
147 // Poster identity information for comments.
149 $replacements[$original] = $comment->getHostname();
153 $mail = $comment->getAuthorEmail();
154 // Add the user cacheability metadata in case the author of the comment
155 // is not the anonymous user.
156 if ($comment->getOwnerId()) {
157 $bubbleable_metadata->addCacheableDependency($comment->getOwner());
159 $replacements[$original] = $mail;
163 $replacements[$original] = UrlHelper::stripDangerousProtocols($comment->getHomepage());
167 $replacements[$original] = $comment->getSubject();
171 // "processed" returns a \Drupal\Component\Render\MarkupInterface via
173 $replacements[$original] = $comment->comment_body->processed;
177 $replacements[$original] = $comment->language()->getId();
180 // Comment related URLs.
182 $url_options['fragment'] = 'comment-' . $comment->id();
183 $replacements[$original] = $comment->url('canonical', $url_options);
187 $url_options['fragment'] = NULL;
188 $replacements[$original] = $comment->url('edit-form', $url_options);
192 $name = $comment->getAuthorName();
193 // Add the user cacheability metadata in case the author of the comment
194 // is not the anonymous user.
195 if ($comment->getOwnerId()) {
196 $bubbleable_metadata->addCacheableDependency($comment->getOwner());
198 $replacements[$original] = $name;
202 if ($comment->hasParentComment()) {
203 $parent = $comment->getParentComment();
204 $bubbleable_metadata->addCacheableDependency($parent);
205 $replacements[$original] = $parent->getSubject();
210 $date_format = DateFormat::load('medium');
211 $bubbleable_metadata->addCacheableDependency($date_format);
212 $replacements[$original] = format_date($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
216 $date_format = DateFormat::load('medium');
217 $bubbleable_metadata->addCacheableDependency($date_format);
218 $replacements[$original] = format_date($comment->getChangedTime(), 'medium', '', NULL, $langcode);
222 $entity = $comment->getCommentedEntity();
223 $bubbleable_metadata->addCacheableDependency($entity);
224 $title = $entity->label();
225 $replacements[$original] = $title;
230 // Chained token relationships.
231 if ($entity_tokens = $token_service->findwithPrefix($tokens, 'entity')) {
232 $entity = $comment->getCommentedEntity();
233 $replacements += $token_service->generate($comment->getCommentedEntityTypeId(), $entity_tokens, [$comment->getCommentedEntityTypeId() => $entity], $options, $bubbleable_metadata);
236 if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) {
237 $replacements += $token_service->generate('date', $date_tokens, ['date' => $comment->getCreatedTime()], $options, $bubbleable_metadata);
240 if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
241 $replacements += $token_service->generate('date', $date_tokens, ['date' => $comment->getChangedTime()], $options, $bubbleable_metadata);
244 if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->getParentComment()) {
245 $replacements += $token_service->generate('comment', $parent_tokens, ['comment' => $parent], $options, $bubbleable_metadata);
248 if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->getOwner()) {
249 $replacements += $token_service->generate('user', $author_tokens, ['user' => $account], $options, $bubbleable_metadata);
252 // Replacement tokens for any content entities that have comment field.
253 elseif (!empty($data[$type]) && $data[$type] instanceof FieldableEntityInterface) {
254 /** @var $entity \Drupal\Core\Entity\FieldableEntityInterface */
255 $entity = $data[$type];
257 foreach ($tokens as $name => $original) {
259 case 'comment-count':
261 $fields = array_keys(\Drupal::service('comment.manager')->getFields($entity->getEntityTypeId()));
262 $definitions = array_keys($entity->getFieldDefinitions());
263 $valid_fields = array_intersect($fields, $definitions);
264 foreach ($valid_fields as $field_name) {
265 $count += $entity->get($field_name)->comment_count;
267 $replacements[$original] = $count;
270 case 'comment-count-new':
271 $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity);
277 return $replacements;