message.tokens.inc in Message 8
Builds placeholder replacement tokens for message-related data.
File
message.tokens.incView source
<?php
/**
* @file
* Builds placeholder replacement tokens for message-related data.
*/
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\user\Entity\User;
/**
* Implements hook_token_info().
*/
function message_token_info() {
$type = [
'name' => t('messages'),
'description' => t('Tokens related to individual content items, or "messages".'),
'needs-data' => 'message',
];
// Core tokens for messages.
$message['mid'] = [
'name' => t("Message ID"),
'description' => t('The unique ID of the message.'),
];
$message['template'] = [
'name' => t("Message template"),
];
$message['template-name'] = [
'name' => t("message template name"),
'description' => t("The human-readable name of the message template."),
];
$message['uuid'] = [
'name' => t("Message UID"),
'description' => t("The message UUID."),
];
$message['created'] = [
'name' => t("Date created"),
'type' => 'date',
];
$message['author'] = [
'name' => t("Author"),
'type' => 'user',
];
return [
'types' => [
'message' => $type,
],
'tokens' => [
'message' => $message,
],
];
}
/**
* Implements hook_tokens().
*/
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;
}
Functions
Name | Description |
---|---|
message_tokens | Implements hook_tokens(). |
message_token_info | Implements hook_token_info(). |