2009-08-19 20:19:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Builds placeholder replacement tokens for comment-related data.
|
|
|
|
*/
|
|
|
|
|
2015-08-31 07:51:38 +00:00
|
|
|
use Drupal\Component\Utility\UrlHelper;
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
use Drupal\Core\Datetime\Entity\DateFormat;
|
|
|
|
use Drupal\Core\Render\BubbleableMetadata;
|
2014-04-07 15:01:20 +00:00
|
|
|
|
2009-08-19 20:19:37 +00:00
|
|
|
/**
|
2009-12-04 16:49:48 +00:00
|
|
|
* Implements hook_token_info().
|
2009-08-19 20:19:37 +00:00
|
|
|
*/
|
|
|
|
function comment_token_info() {
|
|
|
|
$type = array(
|
|
|
|
'name' => t('Comments'),
|
|
|
|
'description' => t('Tokens for comments posted on the site.'),
|
|
|
|
'needs-data' => 'comment',
|
|
|
|
);
|
|
|
|
|
2015-05-18 21:08:10 +00:00
|
|
|
// @todo Make this work per field. See https://www.drupal.org/node/2031903.
|
2013-09-27 15:34:47 +00:00
|
|
|
$entity['comment-count'] = array(
|
2009-08-19 20:19:37 +00:00
|
|
|
'name' => t("Comment count"),
|
2013-09-27 15:34:47 +00:00
|
|
|
'description' => t("The number of comments posted on an entity."),
|
2009-08-19 20:19:37 +00:00
|
|
|
);
|
2013-09-27 15:34:47 +00:00
|
|
|
$entity['comment-count-new'] = array(
|
2009-08-19 20:19:37 +00:00
|
|
|
'name' => t("New comment count"),
|
2013-09-27 15:34:47 +00:00
|
|
|
'description' => t("The number of comments posted on an entity since the reader last viewed it."),
|
2009-08-19 20:19:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Core comment tokens
|
|
|
|
$comment['cid'] = array(
|
|
|
|
'name' => t("Comment ID"),
|
|
|
|
'description' => t("The unique ID of the comment."),
|
|
|
|
);
|
|
|
|
$comment['hostname'] = array(
|
|
|
|
'name' => t("IP Address"),
|
|
|
|
'description' => t("The IP address of the computer the comment was posted from."),
|
|
|
|
);
|
|
|
|
$comment['mail'] = array(
|
|
|
|
'name' => t("Email address"),
|
|
|
|
'description' => t("The email address left by the comment author."),
|
|
|
|
);
|
|
|
|
$comment['homepage'] = array(
|
|
|
|
'name' => t("Home page"),
|
|
|
|
'description' => t("The home page URL left by the comment author."),
|
|
|
|
);
|
|
|
|
$comment['title'] = array(
|
|
|
|
'name' => t("Title"),
|
|
|
|
'description' => t("The title of the comment."),
|
|
|
|
);
|
|
|
|
$comment['body'] = array(
|
|
|
|
'name' => t("Content"),
|
|
|
|
'description' => t("The formatted content of the comment itself."),
|
|
|
|
);
|
2015-01-05 15:43:29 +00:00
|
|
|
$comment['langcode'] = array(
|
|
|
|
'name' => t('Language code'),
|
|
|
|
'description' => t('The language code of the language the comment is written in.'),
|
|
|
|
);
|
2009-08-19 20:19:37 +00:00
|
|
|
$comment['url'] = array(
|
|
|
|
'name' => t("URL"),
|
|
|
|
'description' => t("The URL of the comment."),
|
|
|
|
);
|
|
|
|
$comment['edit-url'] = array(
|
|
|
|
'name' => t("Edit URL"),
|
|
|
|
'description' => t("The URL of the comment's edit page."),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Chained tokens for comments
|
|
|
|
$comment['created'] = array(
|
|
|
|
'name' => t("Date created"),
|
|
|
|
'description' => t("The date the comment was posted."),
|
|
|
|
'type' => 'date',
|
|
|
|
);
|
2010-04-20 09:48:06 +00:00
|
|
|
$comment['changed'] = array(
|
|
|
|
'name' => t("Date changed"),
|
|
|
|
'description' => t("The date the comment was most recently updated."),
|
|
|
|
'type' => 'date',
|
|
|
|
);
|
2009-08-19 20:19:37 +00:00
|
|
|
$comment['parent'] = array(
|
|
|
|
'name' => t("Parent"),
|
|
|
|
'description' => t("The comment's parent, if comment threading is active."),
|
|
|
|
'type' => 'comment',
|
|
|
|
);
|
2013-09-27 15:34:47 +00:00
|
|
|
$comment['entity'] = array(
|
|
|
|
'name' => t("Entity"),
|
|
|
|
'description' => t("The entity the comment was posted to."),
|
|
|
|
'type' => 'entity',
|
|
|
|
);
|
2009-08-19 20:19:37 +00:00
|
|
|
$comment['author'] = array(
|
|
|
|
'name' => t("Author"),
|
2014-11-09 00:59:57 +00:00
|
|
|
'description' => t("The author name of the comment."),
|
2009-08-19 20:19:37 +00:00
|
|
|
'type' => 'user',
|
|
|
|
);
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'types' => array('comment' => $type),
|
|
|
|
'tokens' => array(
|
2013-09-27 15:34:47 +00:00
|
|
|
'entity' => $entity,
|
2009-08-19 20:19:37 +00:00
|
|
|
'comment' => $comment,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-04 16:49:48 +00:00
|
|
|
* Implements hook_tokens().
|
2009-08-19 20:19:37 +00:00
|
|
|
*/
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
function comment_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
|
2013-09-16 03:58:06 +00:00
|
|
|
$token_service = \Drupal::token();
|
2013-04-18 07:24:35 +00:00
|
|
|
|
2009-08-19 20:19:37 +00:00
|
|
|
$url_options = array('absolute' => TRUE);
|
2012-08-27 03:25:18 +00:00
|
|
|
if (isset($options['langcode'])) {
|
2015-01-18 09:47:33 +00:00
|
|
|
$url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
|
2012-08-27 03:25:18 +00:00
|
|
|
$langcode = $options['langcode'];
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-02-21 15:55:52 +00:00
|
|
|
$langcode = NULL;
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
$replacements = array();
|
|
|
|
|
|
|
|
if ($type == 'comment' && !empty($data['comment'])) {
|
2014-02-03 16:02:31 +00:00
|
|
|
/** @var \Drupal\comment\CommentInterface $comment */
|
2009-08-19 20:19:37 +00:00
|
|
|
$comment = $data['comment'];
|
|
|
|
|
|
|
|
foreach ($tokens as $name => $original) {
|
|
|
|
switch ($name) {
|
|
|
|
// Simple key values on the comment.
|
|
|
|
case 'cid':
|
2013-01-14 10:59:47 +00:00
|
|
|
$replacements[$original] = $comment->id();
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
2014-02-16 07:20:53 +00:00
|
|
|
// Poster identity information for comments.
|
2009-08-19 20:19:37 +00:00
|
|
|
case 'hostname':
|
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
|
|
|
$replacements[$original] = $comment->getHostname();
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'mail':
|
2014-02-16 07:20:53 +00:00
|
|
|
$mail = $comment->getAuthorEmail();
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +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());
|
|
|
|
}
|
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
|
|
|
$replacements[$original] = $mail;
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'homepage':
|
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
|
|
|
$replacements[$original] = UrlHelper::stripDangerousProtocols($comment->getHomepage());
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'title':
|
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
|
|
|
$replacements[$original] = $comment->getSubject();
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'body':
|
2015-10-01 23:25:03 +00:00
|
|
|
// "processed" returns a \Drupal\Component\Render\MarkupInterface via
|
|
|
|
// check_markup().
|
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
|
|
|
$replacements[$original] = $comment->comment_body->processed;
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
2015-01-05 15:43:29 +00:00
|
|
|
case 'langcode':
|
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
|
|
|
$replacements[$original] = $comment->language()->getId();
|
2015-01-05 15:43:29 +00:00
|
|
|
break;
|
|
|
|
|
2009-08-19 20:19:37 +00:00
|
|
|
// Comment related URLs.
|
|
|
|
case 'url':
|
2013-01-14 10:59:47 +00:00
|
|
|
$url_options['fragment'] = 'comment-' . $comment->id();
|
2014-09-27 07:03:46 +00:00
|
|
|
$replacements[$original] = $comment->url('canonical', $url_options);
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'edit-url':
|
2010-04-20 09:48:06 +00:00
|
|
|
$url_options['fragment'] = NULL;
|
2014-09-27 07:03:46 +00:00
|
|
|
$replacements[$original] = $comment->url('edit-form', $url_options);
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'author':
|
2014-02-16 07:20:53 +00:00
|
|
|
$name = $comment->getAuthorName();
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +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());
|
|
|
|
}
|
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
|
|
|
$replacements[$original] = $name;
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'parent':
|
2014-02-16 07:20:53 +00:00
|
|
|
if ($comment->hasParentComment()) {
|
|
|
|
$parent = $comment->getParentComment();
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$bubbleable_metadata->addCacheableDependency($parent);
|
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
|
|
|
$replacements[$original] = $parent->getSubject();
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'created':
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$date_format = DateFormat::load('medium');
|
|
|
|
$bubbleable_metadata->addCacheableDependency($date_format);
|
2014-02-16 07:20:53 +00:00
|
|
|
$replacements[$original] = format_date($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
|
2009-10-10 13:37:11 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'changed':
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$date_format = DateFormat::load('medium');
|
|
|
|
$bubbleable_metadata->addCacheableDependency($date_format);
|
2014-02-16 07:20:53 +00:00
|
|
|
$replacements[$original] = format_date($comment->getChangedTime(), 'medium', '', NULL, $langcode);
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
2013-09-27 15:34:47 +00:00
|
|
|
case 'entity':
|
2014-02-16 07:20:53 +00:00
|
|
|
$entity = $comment->getCommentedEntity();
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$bubbleable_metadata->addCacheableDependency($entity);
|
2013-09-27 15:34:47 +00:00
|
|
|
$title = $entity->label();
|
Issue #2567257 by dawehner, stefan.r, effulgentsia, pwolanin, catch, Xano, mr.baileys, Wim Leers, k4v, Dave Reid, chx, googletorp, plach, lauriii, Berdir, webchick, alexpott, stefan.r: hook_tokens() $sanitize option incompatible with Html sanitisation requirements
2015-10-01 13:01:21 +00:00
|
|
|
$replacements[$original] = $title;
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chained token relationships.
|
2013-09-27 15:34:47 +00:00
|
|
|
if ($entity_tokens = $token_service->findwithPrefix($tokens, 'entity')) {
|
2014-02-16 07:20:53 +00:00
|
|
|
$entity = $comment->getCommentedEntity();
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$replacements += $token_service->generate($comment->getCommentedEntityTypeId(), $entity_tokens, array($comment->getCommentedEntityTypeId() => $entity), $options, $bubbleable_metadata);
|
2013-09-27 15:34:47 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 07:24:35 +00:00
|
|
|
if ($date_tokens = $token_service->findwithPrefix($tokens, 'created')) {
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getCreatedTime()), $options, $bubbleable_metadata);
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 07:24:35 +00:00
|
|
|
if ($date_tokens = $token_service->findwithPrefix($tokens, 'changed')) {
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$replacements += $token_service->generate('date', $date_tokens, array('date' => $comment->getChangedTime()), $options, $bubbleable_metadata);
|
2010-04-20 09:48:06 +00:00
|
|
|
}
|
|
|
|
|
2014-02-16 07:20:53 +00:00
|
|
|
if (($parent_tokens = $token_service->findwithPrefix($tokens, 'parent')) && $parent = $comment->getParentComment()) {
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$replacements += $token_service->generate('comment', $parent_tokens, array('comment' => $parent), $options, $bubbleable_metadata);
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 16:02:31 +00:00
|
|
|
if (($author_tokens = $token_service->findwithPrefix($tokens, 'author')) && $account = $comment->getOwner()) {
|
Issue #2525910 by dawehner, effulgentsia, Berdir, lauriii, larowlan, timmillwood, Wim Leers, chx, arlinsandbulte, Fabianx, Gábor Hojtsy, Dave Reid, alexpott, catch: Ensure token replacements have cacheability + attachments metadata and that it is bubbled in any case
2015-07-22 14:16:01 +00:00
|
|
|
$replacements += $token_service->generate('user', $author_tokens, array('user' => $account), $options, $bubbleable_metadata);
|
2009-08-19 20:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-12 17:21:07 +00:00
|
|
|
elseif ($type == 'entity' & !empty($data['entity'])) {
|
2014-10-01 14:11:14 +00:00
|
|
|
/** @var $entity \Drupal\Core\Entity\FieldableEntityInterface */
|
2014-11-12 17:21:07 +00:00
|
|
|
$entity = $data['entity'];
|
2009-08-19 20:19:37 +00:00
|
|
|
|
|
|
|
foreach ($tokens as $name => $original) {
|
2014-02-16 07:20:53 +00:00
|
|
|
switch ($name) {
|
2009-08-19 20:19:37 +00:00
|
|
|
case 'comment-count':
|
2013-09-27 15:34:47 +00:00
|
|
|
$count = 0;
|
2014-01-30 12:06:58 +00:00
|
|
|
$fields = array_keys(\Drupal::service('comment.manager')->getFields($entity->getEntityTypeId()));
|
2014-02-24 11:42:13 +00:00
|
|
|
$definitions = array_keys($entity->getFieldDefinitions());
|
2013-09-27 15:34:47 +00:00
|
|
|
$valid_fields = array_intersect($fields, $definitions);
|
|
|
|
foreach ($valid_fields as $field_name) {
|
|
|
|
$count += $entity->get($field_name)->comment_count;
|
|
|
|
}
|
|
|
|
$replacements[$original] = $count;
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'comment-count-new':
|
2014-07-03 12:05:51 +00:00
|
|
|
$replacements[$original] = \Drupal::service('comment.manager')->getCountNewComments($entity);
|
2009-08-19 20:19:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $replacements;
|
|
|
|
}
|