This repository has been archived by the owner on Jan 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
media_entity.tokens.inc
193 lines (166 loc) · 6.08 KB
/
media_entity.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* @file
* Builds placeholder replacement tokens for media_entity-related data.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\media_entity\Entity\MediaBundle;
/**
* Implements hook_token_info().
*/
function media_entity_token_info() {
$type = [
'name' => t('Media'),
'description' => t('Tokens related to individual media items.'),
'needs-data' => 'media',
];
// Core tokens for media.
$media['mid'] = [
'name' => t("Media ID"),
'description' => t('The unique ID of the media item.'),
];
$media['uuid'] = [
'name' => t("Media UUID"),
'description' => t('The unique UUID of the media item.'),
];
$media['vid'] = [
'name' => t("Revision ID"),
'description' => t("The unique ID of the media's latest revision."),
];
$media['bundle'] = [
'name' => t("Media bundle"),
];
$media['bundle-name'] = [
'name' => t("Media bundle name"),
'description' => t("The human-readable name of the media bundle."),
];
$media['langcode'] = [
'name' => t('Language code'),
'description' => t('The language code of the language the media is written in.'),
];
$media['name'] = [
'name' => t('Name'),
'description' => t('The name of this media.'),
];
$media['type'] = [
'name' => t("Type"),
'description' => t("The type of this media."),
];
$node['author'] = [
'name' => t("Author"),
'type' => 'user',
];
$media['url'] = [
'name' => t("URL"),
'description' => t("The URL of the media."),
];
$media['edit-url'] = [
'name' => t("Edit URL"),
'description' => t("The URL of the media's edit page."),
];
// Chained tokens for media.
$media['created'] = [
'name' => t("Date created"),
'type' => 'date',
];
$media['changed'] = [
'name' => t("Date changed"),
'description' => t("The date the media was most recently updated."),
'type' => 'date',
];
return [
'types' => ['media' => $type],
'tokens' => ['media' => $media],
];
}
/**
* Implements hook_tokens().
*/
function media_entity_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$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;
}
$sanitize = !empty($options['sanitize']);
$replacements = [];
if ($type == 'media' && !empty($data['media'])) {
/** @var \Drupal\media_entity\MediaInterface $media */
$media = \Drupal::service('entity.repository')->getTranslationFromContext($data['media'], $langcode, ['operation' => 'media_entity_tokens']);
foreach ($tokens as $name => $original) {
switch ($name) {
// Simple key values on the media_entity.
case 'mid':
$replacements[$original] = $media->id();
break;
case 'uuid':
$replacements[$original] = $media->uuid();
break;
case 'vid':
$replacements[$original] = $media->getRevisionId();
break;
case 'bundle':
$replacements[$original] = $sanitize ? Html::escape($media->bundle()) : $media->bundle();
break;
case 'bundle-name':
$bundle_name = MediaBundle::load($media->bundle())->label();
$replacements[$original] = $sanitize ? Html::escape($bundle_name) : $bundle_name;
break;
case 'langcode':
$replacements[$original] = $sanitize ? Html::escape($media->language()->getId()) : $media->language()->getId();
break;
case 'name':
$media_name = $media->get('name')->value;
$replacements[$original] = $sanitize ? Html::escape($media_name) : $media_name;
break;
case 'type':
$media_type = $media->get('name')->value;
$replacements[$original] = $sanitize ? Html::escape($media_type) : $media_type;
break;
case 'url':
$replacements[$original] = $media->toUrl('canonical', $url_options);
break;
case 'edit-url':
$replacements[$original] = $media->toUrl('edit-form', $url_options);
break;
// Default values for the chained tokens handled below.
case 'author':
/** @var \Drupal\user\UserInterface $account */
$account = $media->get('uid')->entity;
$bubbleable_metadata->addCacheableDependency($account);
$replacements[$original] = $sanitize ? Html::escape($account->label()) : $account->label();
break;
case 'created':
$date_format = DateFormat::load('medium');
$bubbleable_metadata->addCacheableDependency($date_format);
$replacements[$original] = \Drupal::service('date.formatter')
->format($media->getCreatedTime(), 'medium', '', NULL, $langcode);
break;
case 'changed':
$date_format = DateFormat::load('medium');
$bubbleable_metadata->addCacheableDependency($date_format);
$replacements[$original] = \Drupal::service('date.formatter')
->format($media->getChangedTime(), 'medium', '', NULL, $langcode);
break;
}
}
if ($author_tokens = $token_service->findWithPrefix($tokens, 'author')) {
$account = $media->get('uid')->entity;
$replacements += $token_service->generate('user', $author_tokens, ['user' => $account], $options, $bubbleable_metadata);
}
if ($created_tokens = $token_service->findWithPrefix($tokens, 'created')) {
$replacements += $token_service->generate('date', $created_tokens, ['date' => $media->getCreatedTime()], $options, $bubbleable_metadata);
}
if ($changed_tokens = $token_service->findWithPrefix($tokens, 'changed')) {
$replacements += $token_service->generate('date', $changed_tokens, ['date' => $media->getChangedTime()], $options, $bubbleable_metadata);
}
}
return $replacements;
}