-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopeny_hours_formatter.module
109 lines (97 loc) · 3.15 KB
/
openy_hours_formatter.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
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
<?php
/**
* @file
* Contains openy_hours_formatter.module.
*/
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_theme().
*/
function openy_hours_formatter_theme() {
return [
'openy_hours_formatter' => [
'variables' => [
'hours' => '',
'week' => [],
],
],
];
}
/**
* Implements hook_token_info().
*/
function openy_hours_formatter_token_info() {
$info = [];
$info['types']['openy_hours'] = [
'name' => t('Open Y Hours'),
'description' => t('Tokens related to Open Y branch_hours information.')
];
$info['tokens']['openy_hours']['day_of_week'] = [
'name' => t('Day of Week'),
'description' => t('An array of days for "dayOfWeek".')
];
$info['tokens']['openy_hours']['opens'] = [
'name' => t('Opens'),
'description' => t('An hours string for "open".')
];
$info['tokens']['openy_hours']['closes'] = [
'name' => t('Closes'),
'description' => t('An hours string for "closes".')
];
return $info;
}
/**
* Implements hook_tokens().
*/
function openy_hours_formatter_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'openy_hours') {
$node = $data['node'];
// Only continue if the node we're acting on has field_branch_hours.
if ($node->hasField('field_branch_hours') && !$node->get('field_branch_hours')->isEmpty()) {
$days = [
'mon' => 'Monday',
'tue' => 'Tuesday',
'wed' => 'Wednesday',
'thu' => 'Thursday',
'fri' => 'Friday',
'sat' => 'Saturday',
'sun' => 'Sunday',
];
$opens = [];
$closes = [];
foreach ($days as $key => $day) {
// If we can find hours, set them, otherwise Google recommends
// "set both opens and closes properties to '00:00'" for closed.
if ($hours = $node->field_branch_hours->{'hours_'.$key}) {
// Find a non-digit, non-word, non-whitespace character and split,
// and automagically return the full string if no match.
$hours = preg_split('/\s?[^\s\w:\.]\s?/',$hours, -1, PREG_SPLIT_NO_EMPTY);
$opens[$key] = strtotime($hours[0]) ? date('H:i', strtotime($hours[0])) : '00:00';
// If $hours[1] is not set, use an empty string as the default value.
$closes[$key] = date('H:i', strtotime($hours[1] ?? ''));
}
}
foreach ($tokens as $name => $original) {
// Our goal is to end up with matching arrays for day, open, close.
// To show a business as open 24 hours a day, set the open property
// to "00:00" and the closes property to "23:59". To show a business is
// closed all day, set both opens and closes properties to "00:00".
switch ($name) {
case 'day_of_week':
$replacements[$original] = implode(',', $days);
break;
case 'opens':
$replacements[$original] = implode(',', $opens);
break;
case 'closes':
$replacements[$original] = implode(',', $closes);
break;
default:
break;
}
}
}
}
return $replacements;
}