Chris@0: t('Comments'), Chris@0: 'description' => t('Tokens for comments posted on the site.'), Chris@0: 'needs-data' => 'comment', Chris@0: ]; Chris@0: Chris@0: $tokens = []; Chris@0: // Provide a integration for each entity type except comment. Chris@0: foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) { Chris@0: if ($entity_type_id == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class)) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if (\Drupal::service('comment.manager')->getFields($entity_type_id)) { Chris@0: // Get the correct token type. Chris@0: $token_type = ($entity_type_id == 'taxonomy_term') ? 'term' : $entity_type_id; Chris@0: Chris@0: // @todo Make this work per field. See https://www.drupal.org/node/2031903. Chris@0: $tokens[$token_type]['comment-count'] = [ Chris@0: 'name' => t("Comment count"), Chris@0: 'description' => t("The number of comments posted on an entity."), Chris@0: ]; Chris@0: $tokens[$token_type]['comment-count-new'] = [ Chris@0: 'name' => t("New comment count"), Chris@0: 'description' => t("The number of comments posted on an entity since the reader last viewed it."), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: // Core comment tokens Chris@0: $comment['cid'] = [ Chris@0: 'name' => t("Comment ID"), Chris@0: 'description' => t("The unique ID of the comment."), Chris@0: ]; Chris@0: $comment['hostname'] = [ Chris@0: 'name' => t("IP Address"), Chris@0: 'description' => t("The IP address of the computer the comment was posted from."), Chris@0: ]; Chris@0: $comment['mail'] = [ Chris@0: 'name' => t("Email address"), Chris@0: 'description' => t("The email address left by the comment author."), Chris@0: ]; Chris@0: $comment['homepage'] = [ Chris@0: 'name' => t("Home page"), Chris@0: 'description' => t("The home page URL left by the comment author."), Chris@0: ]; Chris@0: $comment['title'] = [ Chris@0: 'name' => t("Title"), Chris@0: 'description' => t("The title of the comment."), Chris@0: ]; Chris@0: $comment['body'] = [ Chris@0: 'name' => t("Content"), Chris@0: 'description' => t("The formatted content of the comment itself."), Chris@0: ]; Chris@0: $comment['langcode'] = [ Chris@0: 'name' => t('Language code'), Chris@0: 'description' => t('The language code of the language the comment is written in.'), Chris@0: ]; Chris@0: $comment['url'] = [ Chris@0: 'name' => t("URL"), Chris@0: 'description' => t("The URL of the comment."), Chris@0: ]; Chris@0: $comment['edit-url'] = [ Chris@0: 'name' => t("Edit URL"), Chris@0: 'description' => t("The URL of the comment's edit page."), Chris@0: ]; Chris@0: Chris@0: // Chained tokens for comments Chris@0: $comment['created'] = [ Chris@0: 'name' => t("Date created"), Chris@0: 'description' => t("The date the comment was posted."), Chris@0: 'type' => 'date', Chris@0: ]; Chris@0: $comment['changed'] = [ Chris@0: 'name' => t("Date changed"), Chris@0: 'description' => t("The date the comment was most recently updated."), Chris@0: 'type' => 'date', Chris@0: ]; Chris@0: $comment['parent'] = [ Chris@0: 'name' => t("Parent"), Chris@0: 'description' => t("The comment's parent, if comment threading is active."), Chris@0: 'type' => 'comment', Chris@0: ]; Chris@0: $comment['entity'] = [ Chris@0: 'name' => t("Entity"), Chris@0: 'description' => t("The entity the comment was posted to."), Chris@0: 'type' => 'entity', Chris@0: ]; Chris@0: $comment['author'] = [ Chris@0: 'name' => t("Author"), Chris@0: 'description' => t("The author name of the comment."), Chris@0: 'type' => 'user', Chris@0: ]; Chris@0: Chris@0: return [ Chris@0: 'types' => ['comment' => $type], Chris@0: 'tokens' => [ Chris@0: 'comment' => $comment, Chris@0: ] + $tokens, Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements hook_tokens(). Chris@0: */ Chris@0: function comment_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { Chris@0: $token_service = \Drupal::token(); Chris@0: Chris@0: $url_options = ['absolute' => TRUE]; Chris@0: if (isset($options['langcode'])) { Chris@0: $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']); Chris@0: $langcode = $options['langcode']; Chris@0: } Chris@0: else { Chris@0: $langcode = NULL; Chris@0: } Chris@0: $replacements = []; Chris@0: Chris@0: if ($type == 'comment' && !empty($data['comment'])) { Chris@0: /** @var \Drupal\comment\CommentInterface $comment */ Chris@0: $comment = $data['comment']; Chris@0: Chris@0: foreach ($tokens as $name => $original) { Chris@0: switch ($name) { Chris@0: // Simple key values on the comment. Chris@0: case 'cid': Chris@0: $replacements[$original] = $comment->id(); Chris@0: break; Chris@0: Chris@0: // Poster identity information for comments. Chris@0: case 'hostname': Chris@0: $replacements[$original] = $comment->getHostname(); Chris@0: break; Chris@0: Chris@0: case 'mail': Chris@0: $mail = $comment->getAuthorEmail(); Chris@0: // Add the user cacheability metadata in case the author of the comment Chris@0: // is not the anonymous user. Chris@0: if ($comment->getOwnerId()) { Chris@0: $bubbleable_metadata->addCacheableDependency($comment->getOwner()); Chris@0: } Chris@0: $replacements[$original] = $mail; Chris@0: break; Chris@0: Chris@0: case 'homepage': Chris@0: $replacements[$original] = UrlHelper::stripDangerousProtocols($comment->getHomepage()); Chris@0: break; Chris@0: Chris@0: case 'title': Chris@0: $replacements[$original] = $comment->getSubject(); Chris@0: break; Chris@0: Chris@0: case 'body': Chris@0: // "processed" returns a \Drupal\Component\Render\MarkupInterface via Chris@0: // check_markup(). Chris@0: $replacements[$original] = $comment->comment_body->processed; Chris@0: break; Chris@0: Chris@0: case 'langcode': Chris@0: $replacements[$original] = $comment->language()->getId(); Chris@0: break; Chris@0: Chris@0: // Comment related URLs. Chris@0: case 'url': Chris@17: $url_options['fragment'] = 'comment-' . $comment->id(); Chris@18: $replacements[$original] = $comment->toUrl('canonical', $url_options)->toString(); Chris@0: break; Chris@0: Chris@0: case 'edit-url': Chris@0: $url_options['fragment'] = NULL; Chris@18: $replacements[$original] = $comment->toUrl('edit-form', $url_options)->toString(); Chris@0: break; Chris@0: Chris@0: case 'author': Chris@0: $name = $comment->getAuthorName(); Chris@0: // Add the user cacheability metadata in case the author of the comment Chris@0: // is not the anonymous user. Chris@0: if ($comment->getOwnerId()) { Chris@0: $bubbleable_metadata->addCacheableDependency($comment->getOwner()); Chris@0: } Chris@0: $replacements[$original] = $name; Chris@0: break; Chris@0: Chris@0: case 'parent': Chris@0: if ($comment->hasParentComment()) { Chris@0: $parent = $comment->getParentComment(); Chris@0: $bubbleable_metadata->addCacheableDependency($parent); Chris@0: $replacements[$original] = $parent->getSubject(); Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'created': Chris@0: $date_format = DateFormat::load('medium'); Chris@0: $bubbleable_metadata->addCacheableDependency($date_format); Chris@18: $replacements[$original] = \Drupal::service('date.formatter')->format($comment->getCreatedTime(), 'medium', '', NULL, $langcode); Chris@0: break; Chris@0: Chris@0: case 'changed': Chris@0: $date_format = DateFormat::load('medium'); Chris@0: $bubbleable_metadata->addCacheableDependency($date_format); Chris@18: $replacements[$original] = \Drupal::service('date.formatter')->format($comment->getChangedTime(), 'medium', '', NULL, $langcode); Chris@0: break; Chris@0: Chris@0: case 'entity': Chris@0: $entity = $comment->getCommentedEntity(); Chris@0: $bubbleable_metadata->addCacheableDependency($entity); Chris@0: $title = $entity->label(); Chris@0: $replacements[$original] = $title; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: // Chained token relationships. Chris@0: if ($entity_tokens = $token_service->findwithPrefix($tokens, 'entity')) { Chris@0: $entity = $comment->getCommentedEntity(); Chris@0: $replacements += $token_service->generate($comment->getCommentedEntityTypeId(), $entity_tokens, [$comment->getCommentedEntityTypeId() => $entity], $options, $bubbleable_metadata); Chris@0: } Chris@0: Chris@0: if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) { Chris@0: $replacements += $token_service->generate('date', $date_tokens, ['date' => $comment->getCreatedTime()], $options, $bubbleable_metadata); Chris@0: } Chris@0: Chris@0: if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) { Chris@0: $replacements += $token_service->generate('date', $date_tokens, ['date' => $comment->getChangedTime()], $options, $bubbleable_metadata); Chris@0: } Chris@0: Chris@0: if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->getParentComment()) { Chris@0: $replacements += $token_service->generate('comment', $parent_tokens, ['comment' => $parent], $options, $bubbleable_metadata); Chris@0: } Chris@0: Chris@0: if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->getOwner()) { Chris@0: $replacements += $token_service->generate('user', $author_tokens, ['user' => $account], $options, $bubbleable_metadata); Chris@0: } Chris@0: } Chris@0: // Replacement tokens for any content entities that have comment field. Chris@0: elseif (!empty($data[$type]) && $data[$type] instanceof FieldableEntityInterface) { Chris@0: /** @var $entity \Drupal\Core\Entity\FieldableEntityInterface */ Chris@0: $entity = $data[$type]; Chris@0: Chris@0: foreach ($tokens as $name => $original) { Chris@0: switch ($name) { Chris@0: case 'comment-count': Chris@0: $count = 0; Chris@0: $fields = array_keys(\Drupal::service('comment.manager')->getFields($entity->getEntityTypeId())); Chris@0: $definitions = array_keys($entity->getFieldDefinitions()); Chris@0: $valid_fields = array_intersect($fields, $definitions); Chris@0: foreach ($valid_fields as $field_name) { Chris@0: $count += $entity->get($field_name)->comment_count; Chris@0: } Chris@0: $replacements[$original] = $count; Chris@0: break; Chris@0: Chris@0: case 'comment-count-new': Chris@0: $replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity); Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $replacements; Chris@0: }