annotate core/modules/comment/comment.tokens.inc @ 19:fa3358dc1485 tip

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