-
Notifications
You must be signed in to change notification settings - Fork 0
/
recurring_events.tokens.inc
133 lines (111 loc) · 3.76 KB
/
recurring_events.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
<?php
/**
* @file
* Tokens for the recurring_events module.
*/
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function recurring_events_token_info() {
$eventinstance = [];
$eventinstance_type = [
'name' => t('Event Instance'),
'description' => t('Tokens for the eventinstance entity type.'),
'needs-data' => 'eventinstance',
];
$eventinstance['title'] = [
'name' => t('Event Instance Inherited Title'),
'description' => t('The title of the event instance.'),
];
$eventinstance['description'] = [
'name' => t('Event Instance Inherited Description'),
'description' => t('The description of the event instance.'),
];
$eventinstance['date'] = [
'name' => t('Event Instance Start Date'),
'description' => t('The start date of the event instance.'),
];
$eventinstance['end_date'] = [
'name' => t('Event Instance End Date'),
'description' => t('The end date of the event instance.'),
];
$eventinstance['url'] = [
'name' => t('Event Instance URL'),
'description' => t('The URL of the event instance.'),
];
$eventseries = [];
$eventseries_type = [
'name' => t('Event Series'),
'description' => t('Tokens for the eventseries entity type.'),
'needs-data' => 'eventseries',
];
$eventseries['title'] = [
'name' => t('Event Series Title'),
'description' => t('The title of the event series.'),
];
$eventseries['body'] = [
'name' => t('Event Series Body'),
'description' => t('The description of the event series.'),
];
return [
'types' => [
'eventinstance' => $eventinstance_type,
'eventseries' => $eventseries_type,
],
'tokens' => [
'eventinstance' => $eventinstance,
'eventseries' => $eventseries,
],
];
}
/**
* Implements hook_tokens().
*/
function recurring_events_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'eventinstance' && !empty($data['eventinstance'])) {
$event_instance = $data['eventinstance'];
$timezone = new \DateTimeZone(date_default_timezone_get());
foreach ($tokens as $name => $original) {
$date_format = \Drupal::config('recurring_events.eventinstance.config')->get('date_format');
// Extract the date format if provided.
$custom_date_format_indicator = ":custom:";
if (strpos($name, $custom_date_format_indicator) !== FALSE) {
$date_format = substr($name, strpos($name, $custom_date_format_indicator) + strlen($custom_date_format_indicator));
$name = substr($name, 0, strpos($name, $custom_date_format_indicator));
}
switch ($name) {
case 'title':
$replacements[$original] = $event_instance->title->value;
break;
case 'description':
$replacements[$original] = $event_instance->description->value;
break;
case 'date':
$replacements[$original] = $event_instance->date->start_date->setTimezone($timezone)->format($date_format);
break;
case 'end_date':
$replacements[$original] = $event_instance->date->end_date->setTimezone($timezone)->format($date_format);
break;
case 'url':
$replacements[$original] = $event_instance->toUrl('canonical')->setAbsolute(TRUE)->toString();
break;
}
}
}
if ($type == 'eventseries' && !empty($data['eventseries'])) {
$event_series = $data['eventseries'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'title':
$replacements[$original] = $event_series->title->value;
break;
case 'body':
$replacements[$original] = $event_series->body->value;
break;
}
}
}
return $replacements;
}