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