This repository has been archived by the owner on Feb 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
rng.tokens.inc
99 lines (81 loc) · 2.69 KB
/
rng.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
<?php
/**
* @file
* Builds tokens for RNG.
*/
use Drupal\rng\RegistrationInterface;
/**
* Implements hook_token_info().
*/
function rng_token_info() {
// Token types.
$tokens['types']['registration'] = [
'name' => t('Registrations'),
'description' => t('Tokens for registrations.'),
'needs-data' => 'registration',
];
// Tokens.
$tokens['tokens']['registration']['id'] = [
'name' => t("ID"),
'description' => t("Unique ID of the registration."),
];
$tokens['tokens']['registration']['created'] = [
'name' => t("Created"),
'description' => t("The date the registration was created."),
'type' => 'date',
];
$tokens['tokens']['registration']['changed'] = [
'name' => t("Updated"),
'description' => t("The date the registration was last updated."),
'type' => 'date',
];
$tokens['tokens']['registration']['url'] = [
'name' => t("URL"),
'description' => t("The URL of the registration view page."),
'type' => 'url',
];
$tokens['tokens']['registration']['edit-url'] = [
'name' => t("Edit URL"),
'description' => t("The URL of the registration edit page."),
'type' => 'url',
];
return $tokens;
}
/**
* Implements hook_tokens().
*/
function rng_tokens($type, $tokens, array $data = [], array $options = []) {
$replacements = [];
$url_options = array('absolute' => TRUE);
if (isset($options['langcode'])) {
$url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
$langcode = $options['langcode'];
}
else {
$langcode = NULL;
}
if ($type == 'registration' && !empty($data['registration'])) {
if (($registration = $data['registration']) && $registration instanceof RegistrationInterface) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'id':
$replacements[$original] = $registration->id() ?: t('Unassigned');
break;
case 'created':
$replacements[$original] = $registration->id() ? format_date($registration->getCreatedTime(), 'medium', '', NULL, $langcode) : t('Unassigned');
break;
case 'changed':
$replacements[$original] = $registration->id() ? format_date($registration->getChangedTime(), 'medium', '', NULL, $langcode) : t('Unassigned');
break;
case 'url':
$replacements[$original] = $registration->id() ? $registration->url('canonical', $url_options) : t('Unassigned');
break;
case 'edit-url':
$replacements[$original] = $registration->id() ? $registration->url('edit-form', $url_options) : t('Unassigned');
break;
}
}
}
}
return $replacements;
}