-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews_countdown_timer.module
57 lines (49 loc) · 1.92 KB
/
views_countdown_timer.module
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
<?php
/**
* Implements hook_field_formatter_info().
*/
function views_countdown_timer_field_formatter_info() {
return array(
'countdown_timer' => array(
'label' => t('Countdown Timer'),
'field types' => array('datetime'),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function views_countdown_timer_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$output = array();
foreach ($items as $delta => $item) {
$event_timestamp = strtotime($item['value']);
$output[$delta] = array(
'#markup' => '<div id="timezoneElement" data-timezone="Europe/London" style="display: none;"></div>' .
'<span class="views-countdown-timer" data-time="' . ($event_timestamp * 1000) . '"></span>',
'#attached' => array(
'js' => array(
backdrop_get_path('module', 'views_countdown_timer') . '/views_countdown_timer.js',
),
),
);
}
return $output;
}
function views_countdown_timer_preprocess_views_view(&$variables) {
// Ensure we are modifying the correct view.
if ($variables['view']->name == 'your_view_name') {
foreach ($variables['rows'] as &$row) {
// Get the event date field value.
if (!empty($row['field_event_date'])) {
$event_timestamp = strtotime($row['field_event_date']);
// Inject the countdown field into the row.
$row['views_countdown_timer'] = '<span class="views-countdown-timer" data-time="' . $event_timestamp * 1000 . '"></span>';
}
}
}
$site_timezone = config_get('system.date', 'default_timezone');
// Append JavaScript to Views output
$variables['view']->result[] = [
'#markup' => '<script>var siteTimezone = "' . htmlspecialchars($site_timezone, ENT_QUOTES) . '";</script>',
];
}