-
Notifications
You must be signed in to change notification settings - Fork 0
/
tzfield.module
224 lines (206 loc) · 6.43 KB
/
tzfield.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* @file
* Defines a time zone field type.
*/
/**
* Implements hook_menu().
*/
function tzfield_menu() {
$items['tzfield/autocomplete'] = array(
'title' => 'Time zone field autocomplete',
'page callback' => 'tzfield_autocomplete',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_field_info().
*/
function tzfield_field_info() {
return array(
'tzfield' => array(
'label' => t('Time zone'),
'description' => t('Store a time zone identifier in the database.'),
'settings' => array('exclude' => tzfield_identifiers_list()),
'default_widget' => 'options_select',
'default_formatter' => 'tzfield_default',
'property_type' => 'text',
),
);
}
/**
* Implements hook_field_settings_form().
*/
function tzfield_field_settings_form($field, $instance, $has_data) {
$form['exclude'] = array(
'#title' => t('Time zones to be excluded from the option list'),
'#type' => 'select',
'#options' => tzfield_identifiers_list(),
'#default_value' => $field['settings']['exclude'],
'#multiple' => TRUE,
'#size' => 20,
'#required' => FALSE,
'#description' => t('Any time zones selected here will be excluded from the allowed values.'),
);
return $form;
}
/**
* Implements hook_field_validate().
*/
function tzfield_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
$options = tzfield_options_list($field);
foreach ($items as $delta => $item) {
if (!empty($item['value']) && !isset($options[$item['value']])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'tzfield_illegal_value',
'message' => t('%name: This time zone is not valid.', array('%name' => $instance['label'])),
);
}
}
return $items;
}
/**
* Implements hook_field_is_empty().
*/
function tzfield_field_is_empty($item, $field) {
return empty($item['value']);
}
/**
* Implements hook_field_formatter_info().
*/
function tzfield_field_formatter_info() {
return array(
'tzfield_default' => array(
'label' => 'Time zone name',
'field types' => array('tzfield'),
),
'tzfield_date' => array(
'label' => 'Formatted current date',
'field types' => array('tzfield'),
'settings' => array(
'format' => 'T',
),
),
);
}
/**
* Implements hook_field_widget_info().
*/
function tzfield_field_widget_info() {
return array(
'tzfield_autocomplete' => array(
'label' => t('Autocomplete time zone widget'),
'field types' => array('tzfield'),
'settings' => array('size' => 60),
),
);
}
/**
* Implements hook_field_widget_info_alter().
*/
function tzfield_field_widget_info_alter(&$info) {
$info['options_select']['field types'][] = 'tzfield';
}
/**
* Implements hook_field_widget_form().
*/
function tzfield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => 'textfield',
// phpcs:ignore
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
'#size' => $instance['widget']['settings']['size'],
'#autocomplete_path' => 'tzfield/autocomplete/' . $field['field_name'],
);
return array('value' => $element);
}
/**
* Retrieve an array of autocomplete suggestions.
*/
function tzfield_autocomplete($field_name, $string = '', $arg4 = '', $arg5 = '') {
if ($arg4) {
$string .= '/' . $arg4;
}
if ($arg5) {
$string .= '/' . $arg5;
}
$string = preg_replace(';[^a-z0-9/_-];i', '', str_replace(' ', '_', trim($string)));
backdrop_json_output($string ? preg_grep(';' . $string . ';i', tzfield_options_list(field_info_field($field_name))) : array());
}
/**
* Implements hook_options_list().
*/
function tzfield_options_list($field) {
return empty($field['settings']['exclude']) ? tzfield_identifiers_list() : array_diff_key(tzfield_identifiers_list(), $field['settings']['exclude']);
}
/**
* Cache the time zone identifiers list as a static variable.
*/
function tzfield_identifiers_list() {
static $list;
if (empty($list)) {
$list = system_time_zones();
}
return $list;
}
/**
* Implements hook_field_formatter_view().
*/
function tzfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'tzfield_default':
foreach ($items as $delta => $item) {
// Time zones should not contain any characters that require HTML
// encoding.
$element[$delta] = array('#markup' => $item['value']);
}
break;
case 'tzfield_date':
foreach ($items as $delta => $item) {
// If the date format string contains a character that requires HTML
// encoding, then for plain text output from this formatter, you will
// need to HTML decode it.
$element[$delta]['#markup'] = $item['value'] ? check_plain(format_date(REQUEST_TIME, 'custom', $display['settings']['format'], $item['value'])) : '';
}
break;
}
return $element;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function tzfield_field_formatter_settings_form($field, $instance, $view_mode) {
$form = array();
if ($instance['display'][$view_mode]['type'] == 'tzfield_date') {
$form['format'] = array(
'#title' => t('Format'),
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $instance['display'][$view_mode]['settings']['format'],
'#description' => t('Enter a <a rel="noreferrer" href="https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters">PHP date format string</a>, e.g. <em>T</em> to display the current time zone abbreviation.'),
);
}
return $form;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function tzfield_field_formatter_settings_summary($field, $instance, $view_mode) {
$summary = '';
if ($instance['display'][$view_mode]['type'] == 'tzfield_date') {
$summary = t('Format string: %format<br />Sample date: %date', array(
'%format' => $instance['display'][$view_mode]['settings']['format'],
'%date' => format_date(REQUEST_TIME, 'custom', $instance['display'][$view_mode]['settings']['format']),
));
}
return $summary;
}
/**
* Implements hook_field_widget_error().
*/
function tzfield_field_widget_error($element, $error, $form, &$form_state) {
form_error($element, $error['message']);
}