-
Notifications
You must be signed in to change notification settings - Fork 50
/
message.tokens.inc
106 lines (88 loc) · 2.88 KB
/
message.tokens.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?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;
}