Newer
Older
Greg Knaddison
committed
<?php
/**
* @file
* Builds placeholder replacement tokens for comment_notify.module.
*/
Arild Matsson
committed
use Drupal\Core\Render\BubbleableMetadata;
Greg Knaddison
committed
/**
* Implements hook_token_info().
*/
function comment_notify_token_info() {
Greg Knaddison
committed
// Comment tokens.
$info['tokens']['comment']['unsubscribe-url'] = [
Greg Knaddison
committed
'name' => t('Unsubscribe URL'),
'description' => t('The URL to disable notifications for the comment.'),
Greg Knaddison
committed
'type' => 'url',
Greg Knaddison
committed
// Comment subscriber token type (extends the comment token type).
$info['types']['comment-subscribed'] = [
Greg Knaddison
committed
'name' => t('Subscribed comment'),
'description' => t('Tokens related to a comment that is subscribed to new comments.'),
'type' => 'comment',
Greg Knaddison
committed
Greg Knaddison
committed
return $info;
}
/**
* Implements hook_tokens().
*/
function comment_notify_tokens($type, $tokens, array $data = [], array $options = [], BubbleableMetadata $bubbleable_metadata) {
$url_options = ['absolute' => TRUE];
Greg Knaddison
committed
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$sanitize = !empty($options['sanitize']);
$replacements = [];
Greg Knaddison
committed
if ($type == 'comment' && !empty($data['comment'])) {
$comment = $data['comment'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'unsubscribe-url':
Greg Knaddison
committed
if ($unsubscribe_url = comment_notify_get_unsubscribe_url($comment)) {
Arild Matsson
committed
$replacements[$original] = $unsubscribe_url;
Greg Knaddison
committed
}
break;
}
}
Greg Knaddison
committed
// [comment:unsubscribe-url:*] chained token replacements.
Arild Matsson
committed
if (($unsubscribe_url_tokens = \Drupal::token()->findWithPrefix($tokens, 'unsubscribe-url')) && $unsubscribe_url = comment_notify_get_unsubscribe_url($comment)) {
$replacements += \Drupal::token()->generate('url', $unsubscribe_url_tokens, ['path' => $unsubscribe_url], $options, $bubbleable_metadata);
Greg Knaddison
committed
}
}
// Comment subscriber tokens (pass through to comment token replacement).
if ($type == 'comment-subscribed' && !empty($data['comment-subscribed'])) {
$replacements += \Drupal::token()->generate('comment', $tokens, ['comment' => $data['comment-subscribed']], $options, $bubbleable_metadata);
Greg Knaddison
committed
}
Greg Knaddison
committed
return $replacements;