-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcountdown.module
More file actions
167 lines (145 loc) · 3.73 KB
/
countdown.module
File metadata and controls
167 lines (145 loc) · 3.73 KB
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
<?php
/**
* countdown.module
*
* countdown
*/
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\countdown\Element\CountdownRenderElementBase;
require 'includes/helpers.php';
/**
* Implements hook_help().
*/
function countdown_help($route_name,RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.countdown':
return '
<h3>About</h3>
<p>This module provides countdown timers. Countdown timers can be utilized via the following mechanisms:</p>
<ul>
<li>Render Element (i.e. programmatically)</li>
<li>Field Type</li>
<li>Token</li>
</ul>';
}
}
/**
* Implements hook_theme().
*/
function countdown_theme($existing,$type,$theme,$path) {
return [
'countdown_widget' => [
'variables' => [
'attributes' => [
'class' => 'countdown-widget',
],
'countdown_date' => CountdownRenderElementBase::makeDefaultCountdownDate(),
'countdown_settings' => [
'include_seconds' => true,
],
],
],
'countdown_inline' => [
'variables' => [
'attributes' => [
'class' => 'countdown-inline',
],
'countdown_date' => CountdownRenderElementBase::makeDefaultCountdownDate(),
'countdown_settings' => [
'format' => 'clock',
'include_seconds' => true,
],
],
],
];
}
/**
* Implements hook_token_info().
*/
function countdown_token_info() {
$info = [
'types' => [],
'tokens' => [],
];
$info['types']['countdown'] = [
'name' => t('Countdown Timers'),
'description' => t('Tokens that generate countdown timers'),
];
$info['tokens']['countdown']['timer'] = [
'name' => t('Timer'),
'description' => t('Timer that counts down to a specified date'),
];
return $info;
}
/**
* Implements hook_tokens().
*/
function countdown_tokens($type,$tokens,array $data,array $options,BubbleableMetadata $bubbleable_metadata) {
if ($type != 'countdown') {
return [];
}
$renderer = Drupal::service('renderer');
$token = Drupal::service('token');
$replacements = [];
$timerTokens = $token->findWithPrefix($tokens,'timer');
foreach ($timerTokens as $tokenKey => $tokenValue) {
$params = explode(':',$tokenKey);
if (count($params) < 3) {
continue;
}
$date = new \DateTime(implode('',[...array_splice($params,0,3)]));
$settings = [
'format' => ['clock',false],
'include_seconds' => ['true','_countdown_parse_bool'],
];
while (true) {
$value = current($params);
if ($value === false) {
$value = null;
}
else {
next($params);
}
$key = key($settings);
if (is_null($key)) {
break;
}
list($default,$fn) = current($settings);
if (is_callable($fn)) {
$settings[$key] = $fn($value ?? $default);
}
else {
$settings[$key] = $value ?? $default;
}
next($settings);
}
if ($settings['format'] == 'widget') {
$render = [
'#type' => 'countdown',
'#countdown_date' => $date,
'#countdown_include_seconds' => $settings['include_seconds'],
];
}
else {
$render = [
'#type' => 'countdown_inline',
'#countdown_date' => $date,
'#countdown_format' => $settings['format'],
'#countdown_include_seconds' => $settings['include_seconds'],
];
}
if ($renderer->hasRenderContext()) {
$replacements["[countdown:timer:$tokenKey]"] = $renderer->render($render);
}
else {
$replacements["[countdown:timer:$tokenKey]"] = $renderer->renderRoot($render);
}
}
return $replacements;
}
/**
* Local Variables:
* mode:php
* End:
*/