function message_tokens in Message 8
Implements hook_tokens().
File
- ./
message.tokens.inc, line 56 - Builds placeholder replacement tokens for message-related data.
Code
function message_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$token_service = \Drupal::token();
$replacements = [];
if ($type == 'message' && !empty($data['message'])) {
/** @var \Drupal\message\Entity\Message $message */
$message = $data['message'];
foreach ($tokens as $name => $original) {
switch ($name) {
// Simple key values on the message.
case 'mid':
$replacements[$original] = $message
->id();
break;
case 'template':
$replacements[$original] = $message
->getTemplate()
->id();
break;
case 'template-name':
$replacements[$original] = $message
->getTemplate()
->label();
break;
case 'uuid':
$replacements[$original] = $message
->getUuid();
break;
// Default values for the chained tokens handled below.
case 'author':
$account = $message
->getOwner() ? $message
->getOwner() : User::load(0);
$replacements[$original] = $account
->label();
break;
case 'created':
$replacements[$original] = \Drupal::service('date.formatter')
->format($message
->getCreatedTime(), 'medium', '', NULL);
break;
}
}
if ($author_tokens = $token_service
->findWithPrefix($tokens, 'author')) {
$replacements += $token_service
->generate('user', $author_tokens, [
'user' => $message
->getOwner(),
], $options, $bubbleable_metadata);
}
if ($created_tokens = $token_service
->findWithPrefix($tokens, 'created')) {
$replacements += $token_service
->generate('date', $created_tokens, [
'date' => $message
->getCreatedTime(),
], $options, $bubbleable_metadata);
}
}
return $replacements;
}