comparison core/modules/comment/comment.tokens.inc @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 /**
4 * @file
5 * Builds placeholder replacement tokens for comment-related data.
6 */
7
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;
13
14 /**
15 * Implements hook_token_info().
16 */
17 function comment_token_info() {
18 $type = [
19 'name' => t('Comments'),
20 'description' => t('Tokens for comments posted on the site.'),
21 'needs-data' => 'comment',
22 ];
23
24 $tokens = [];
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)) {
28 continue;
29 }
30
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;
34
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."),
39 ];
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."),
43 ];
44 }
45 }
46
47 // Core comment tokens
48 $comment['cid'] = [
49 'name' => t("Comment ID"),
50 'description' => t("The unique ID of the comment."),
51 ];
52 $comment['hostname'] = [
53 'name' => t("IP Address"),
54 'description' => t("The IP address of the computer the comment was posted from."),
55 ];
56 $comment['mail'] = [
57 'name' => t("Email address"),
58 'description' => t("The email address left by the comment author."),
59 ];
60 $comment['homepage'] = [
61 'name' => t("Home page"),
62 'description' => t("The home page URL left by the comment author."),
63 ];
64 $comment['title'] = [
65 'name' => t("Title"),
66 'description' => t("The title of the comment."),
67 ];
68 $comment['body'] = [
69 'name' => t("Content"),
70 'description' => t("The formatted content of the comment itself."),
71 ];
72 $comment['langcode'] = [
73 'name' => t('Language code'),
74 'description' => t('The language code of the language the comment is written in.'),
75 ];
76 $comment['url'] = [
77 'name' => t("URL"),
78 'description' => t("The URL of the comment."),
79 ];
80 $comment['edit-url'] = [
81 'name' => t("Edit URL"),
82 'description' => t("The URL of the comment's edit page."),
83 ];
84
85 // Chained tokens for comments
86 $comment['created'] = [
87 'name' => t("Date created"),
88 'description' => t("The date the comment was posted."),
89 'type' => 'date',
90 ];
91 $comment['changed'] = [
92 'name' => t("Date changed"),
93 'description' => t("The date the comment was most recently updated."),
94 'type' => 'date',
95 ];
96 $comment['parent'] = [
97 'name' => t("Parent"),
98 'description' => t("The comment's parent, if comment threading is active."),
99 'type' => 'comment',
100 ];
101 $comment['entity'] = [
102 'name' => t("Entity"),
103 'description' => t("The entity the comment was posted to."),
104 'type' => 'entity',
105 ];
106 $comment['author'] = [
107 'name' => t("Author"),
108 'description' => t("The author name of the comment."),
109 'type' => 'user',
110 ];
111
112 return [
113 'types' => ['comment' => $type],
114 'tokens' => [
115 'comment' => $comment,
116 ] + $tokens,
117 ];
118 }
119
120 /**
121 * Implements hook_tokens().
122 */
123 function comment_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
124 $token_service = \Drupal::token();
125
126 $url_options = ['absolute' => TRUE];
127 if (isset($options['langcode'])) {
128 $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
129 $langcode = $options['langcode'];
130 }
131 else {
132 $langcode = NULL;
133 }
134 $replacements = [];
135
136 if ($type == 'comment' && !empty($data['comment'])) {
137 /** @var \Drupal\comment\CommentInterface $comment */
138 $comment = $data['comment'];
139
140 foreach ($tokens as $name => $original) {
141 switch ($name) {
142 // Simple key values on the comment.
143 case 'cid':
144 $replacements[$original] = $comment->id();
145 break;
146
147 // Poster identity information for comments.
148 case 'hostname':
149 $replacements[$original] = $comment->getHostname();
150 break;
151
152 case 'mail':
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());
158 }
159 $replacements[$original] = $mail;
160 break;
161
162 case 'homepage':
163 $replacements[$original] = UrlHelper::stripDangerousProtocols($comment->getHomepage());
164 break;
165
166 case 'title':
167 $replacements[$original] = $comment->getSubject();
168 break;
169
170 case 'body':
171 // "processed" returns a \Drupal\Component\Render\MarkupInterface via
172 // check_markup().
173 $replacements[$original] = $comment->comment_body->processed;
174 break;
175
176 case 'langcode':
177 $replacements[$original] = $comment->language()->getId();
178 break;
179
180 // Comment related URLs.
181 case 'url':
182 $url_options['fragment'] = 'comment-' . $comment->id();
183 $replacements[$original] = $comment->url('canonical', $url_options);
184 break;
185
186 case 'edit-url':
187 $url_options['fragment'] = NULL;
188 $replacements[$original] = $comment->url('edit-form', $url_options);
189 break;
190
191 case 'author':
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());
197 }
198 $replacements[$original] = $name;
199 break;
200
201 case 'parent':
202 if ($comment->hasParentComment()) {
203 $parent = $comment->getParentComment();
204 $bubbleable_metadata->addCacheableDependency($parent);
205 $replacements[$original] = $parent->getSubject();
206 }
207 break;
208
209 case 'created':
210 $date_format = DateFormat::load('medium');
211 $bubbleable_metadata->addCacheableDependency($date_format);
212 $replacements[$original] = format_date($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
213 break;
214
215 case 'changed':
216 $date_format = DateFormat::load('medium');
217 $bubbleable_metadata->addCacheableDependency($date_format);
218 $replacements[$original] = format_date($comment->getChangedTime(), 'medium', '', NULL, $langcode);
219 break;
220
221 case 'entity':
222 $entity = $comment->getCommentedEntity();
223 $bubbleable_metadata->addCacheableDependency($entity);
224 $title = $entity->label();
225 $replacements[$original] = $title;
226 break;
227 }
228 }
229
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);
234 }
235
236 if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) {
237 $replacements += $token_service->generate('date', $date_tokens, ['date' => $comment->getCreatedTime()], $options, $bubbleable_metadata);
238 }
239
240 if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
241 $replacements += $token_service->generate('date', $date_tokens, ['date' => $comment->getChangedTime()], $options, $bubbleable_metadata);
242 }
243
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);
246 }
247
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);
250 }
251 }
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];
256
257 foreach ($tokens as $name => $original) {
258 switch ($name) {
259 case 'comment-count':
260 $count = 0;
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;
266 }
267 $replacements[$original] = $count;
268 break;
269
270 case 'comment-count-new':
271 $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity);
272 break;
273 }
274 }
275 }
276
277 return $replacements;
278 }