function private_message_tokens in Private Message 8
Same name and namespace in other branches
- 8.2 private_message.tokens.inc \private_message_tokens()
Implements hook_tokens().
File
- ./
private_message.tokens.inc, line 92 - Provides tokens for the Private Message module.
Code
function private_message_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
$token_service = \Drupal::token();
$url_options = [
'absolute' => TRUE,
];
if (isset($options['langcode'])) {
$url_options['language'] = \Drupal::languageManager()
->getLanguage($options['langcode']);
$langcode = $options['langcode'];
}
else {
$langcode = LanguageInterface::LANGCODE_DEFAULT;
}
// Replace private message tokens.
if ($type == 'private_message' && !empty($data['private_message'])) {
$private_message = $data['private_message'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'id':
$replacements[$original] = $private_message
->id();
break;
case 'message':
$replacements[$original] = $private_message
->getMessage();
break;
case 'author-name':
$account = $private_message
->getOwner() ? $private_message
->getOwner() : User::load(0);
$bubbleable_metadata
->addCacheableDependency($account);
$replacements[$original] = $account
->label();
break;
case 'url':
$replacements[$original] = $private_message
->url('canonical', $url_options);
case 'created':
$date_format = DateFormat::load('medium');
$bubbleable_metadata
->addCacheableDependency($date_format);
$replacements[$original] = format_date($private_message
->getCreatedTime(), 'medium', '', NULL, $langcode);
break;
}
}
if ($created_tokens = $token_service
->findWithPrefix($tokens, 'created')) {
$replacements += $token_service
->generate('date', $created_tokens, [
'date' => $private_message
->getCreatedTime(),
], $options, $bubbleable_metadata);
}
}
elseif ($type == 'private_message_thread' && !empty($data['private_message_thread'])) {
$private_message_thread = $data['private_message_thread'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'id':
$replacements[$original] = $private_message_thread
->id();
break;
case 'url':
$replacements[$original] = Url::fromRoute('entity.private_message_thread.canonical', [
'private_message_thread' => $private_message_thread
->id(),
], $url_options)
->toString();
break;
case 'updated':
$date_format = DateFormat::load('medium');
$bubbleable_metadata
->addCacheableDependency($date_format);
$replacements[$original] = format_date($private_message_thread
->getUpdatedTime(), 'medium', '', NULL, $langcode);
break;
}
}
if ($updated_tokens = $token_service
->findWithPrefix($tokens, 'updated')) {
$replacements += $token_service
->generate('date', $updated_tokens, [
'date' => $private_message_thread
->getUpdatedTime(),
], $options, $bubbleable_metadata);
}
}
return $replacements;
}