1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
599a39cd
|
Assos Assos
|
|
3 |
85ad3d82
|
Assos Assos
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Date forms and form themes and validation.
|
6 |
|
|
*
|
7 |
|
|
* All code used in form editing and processing is in this file,
|
8 |
|
|
* included only during form editing.
|
9 |
|
|
*/
|
10 |
|
|
|
11 |
|
|
/**
|
12 |
|
|
* Private implementation of hook_widget().
|
13 |
|
|
*
|
14 |
|
|
* The widget builds out a complex date element in the following way:
|
15 |
|
|
*
|
16 |
|
|
* - A field is pulled out of the database which is comprised of one or
|
17 |
|
|
* more collections of start/end dates.
|
18 |
|
|
*
|
19 |
|
|
* - The dates in this field are all converted from the UTC values stored
|
20 |
|
|
* in the database back to the local time. This is done in #process
|
21 |
|
|
* to avoid making this change to dates that are not being processed,
|
22 |
|
|
* like those hidden with #access.
|
23 |
|
|
*
|
24 |
|
|
* - If values are empty, the field settings rules are used to determine
|
25 |
|
|
* if the default_values should be empty, now, the same, or use strtotime.
|
26 |
|
|
*
|
27 |
|
|
* - Each start/end combination is created using the date_combo element type
|
28 |
|
|
* defined by the date module. If the timezone is date-specific, a
|
29 |
|
|
* timezone selector is added to the first combo element.
|
30 |
|
|
*
|
31 |
|
|
* - The date combo element creates two individual date elements, one each
|
32 |
|
|
* for the start and end field, using the appropriate individual Date API
|
33 |
|
|
* date elements, like selects, textfields, or popups.
|
34 |
|
|
*
|
35 |
|
|
* - In the individual element validation, the data supplied by the user is
|
36 |
|
|
* used to update the individual date values.
|
37 |
|
|
*
|
38 |
|
|
* - In the combo date validation, the timezone is updated, if necessary,
|
39 |
|
|
* then the user input date values are used with that timezone to create
|
40 |
|
|
* date objects, which are used update combo date timezone and offset values.
|
41 |
|
|
*
|
42 |
|
|
* - In the field's submission processing, the new date values, which are in
|
43 |
|
|
* the local timezone, are converted back to their UTC values and stored.
|
44 |
|
|
*/
|
45 |
|
|
function date_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
|
46 |
|
|
$element = $base;
|
47 |
|
|
$field_name = $field['field_name'];
|
48 |
|
|
$entity_type = $instance['entity_type'];
|
49 |
|
|
|
50 |
|
|
// If this is a new entity, populate the field with the right default values.
|
51 |
599a39cd
|
Assos Assos
|
// This happens early so even fields later hidden with #access get those
|
52 |
|
|
// values. We should only add default values to new entities, to avoid
|
53 |
|
|
// over-writing a value that has already been set. This means we can't just
|
54 |
|
|
// check to see if $items is empty, because it might have been set that way
|
55 |
|
|
// on purpose. In date_field_widget_properties_alter() this is flagged if
|
56 |
|
|
// this is a new entity. Check !isset($items[$delta]['value']) because entity
|
57 |
|
|
// translation may create a new translation entity for an existing entity and
|
58 |
|
|
// we don't want to clobber values that were already set in that case.
|
59 |
|
|
// @see date_field_widget_properties_alter()
|
60 |
85ad3d82
|
Assos Assos
|
// @see http://drupal.org/node/1478848.
|
61 |
|
|
$is_default = FALSE;
|
62 |
599a39cd
|
Assos Assos
|
if (!isset($items[$delta]['value'])) {
|
63 |
|
|
if (!empty($instance['widget']['is_new'])) {
|
64 |
|
|
// New entity; use default values defined on instance.
|
65 |
|
|
$items = date_default_value($field, $instance, $langcode);
|
66 |
|
|
$is_default = TRUE;
|
67 |
|
|
}
|
68 |
|
|
else {
|
69 |
|
|
// Date is empty; create array structure for value keys.
|
70 |
|
|
$keys = date_process_values($field);
|
71 |
|
|
$items[$delta] = array_fill_keys($keys, '');
|
72 |
|
|
}
|
73 |
85ad3d82
|
Assos Assos
|
}
|
74 |
|
|
|
75 |
599a39cd
|
Assos Assos
|
// @todo Repeating dates should probably be made into their own field type
|
76 |
|
|
// and completely separated out. That will have to wait for a new branch
|
77 |
|
|
// since it may break other things, including other modules that have an
|
78 |
|
|
// expectation of what the date field types are. Since repeating dates cannot
|
79 |
|
|
// use the default Add more button, we have to handle our own behaviors here.
|
80 |
|
|
// Return only the first multiple value for repeating dates, then clean up
|
81 |
|
|
// the 'Add more' bits in #after_build. The repeating values will be
|
82 |
|
|
// re-generated when the repeat widget form is validated. At this point we
|
83 |
|
|
// can't tell if this form element is going to be hidden by #access, and
|
84 |
|
|
// we're going to lose all but the first value by doing this, so store the
|
85 |
|
|
// original values in case we need to replace them later.
|
86 |
ee46a8ed
|
Assos Assos
|
if (!empty($field['settings']['repeat']) && module_exists('date_repeat_field')) {
|
87 |
85ad3d82
|
Assos Assos
|
if ($delta == 0) {
|
88 |
|
|
$form['#after_build'][] = 'date_repeat_after_build';
|
89 |
|
|
$form_state['storage']['repeat_fields'][$field_name] = array_merge($form['#parents'], array($field_name));
|
90 |
|
|
$form_state['storage']['date_items'][$field_name][$langcode] = $items;
|
91 |
|
|
}
|
92 |
|
|
else {
|
93 |
|
|
return;
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
module_load_include('inc', 'date_api', 'date_api_elements');
|
98 |
b720ea3e
|
Assos Assos
|
$timezone = date_get_timezone($field['settings']['tz_handling'], isset($items[$delta]['timezone']) ? $items[$delta]['timezone'] : date_default_timezone());
|
99 |
85ad3d82
|
Assos Assos
|
|
100 |
|
|
// TODO see if there's a way to keep the timezone element from ever being
|
101 |
|
|
// nested as array('timezone' => 'timezone' => value)). After struggling
|
102 |
|
|
// with this a while, I can find no way to get it displayed in the form
|
103 |
|
|
// correctly and get it to use the timezone element without ending up
|
104 |
|
|
// with nesting.
|
105 |
|
|
if (is_array($timezone)) {
|
106 |
|
|
$timezone = $timezone['timezone'];
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
$element += array(
|
110 |
|
|
'#type' => 'date_combo',
|
111 |
|
|
'#theme_wrappers' => array('date_combo'),
|
112 |
|
|
'#weight' => $delta,
|
113 |
599a39cd
|
Assos Assos
|
'#default_value' => isset($items[$delta]) ? $items[$delta] : array(),
|
114 |
85ad3d82
|
Assos Assos
|
'#date_timezone' => $timezone,
|
115 |
|
|
'#element_validate' => array('date_combo_validate'),
|
116 |
|
|
'#date_is_default' => $is_default,
|
117 |
|
|
|
118 |
|
|
// Store the original values, for use with disabled and hidden fields.
|
119 |
599a39cd
|
Assos Assos
|
'#date_items' => isset($items[$delta]) ? $items[$delta] : array(),
|
120 |
85ad3d82
|
Assos Assos
|
);
|
121 |
|
|
|
122 |
|
|
$element['#title'] = $instance['label'];
|
123 |
|
|
|
124 |
|
|
if ($field['settings']['tz_handling'] == 'date') {
|
125 |
|
|
$element['timezone'] = array(
|
126 |
|
|
'#type' => 'date_timezone',
|
127 |
|
|
'#theme_wrappers' => array('date_timezone'),
|
128 |
|
|
'#delta' => $delta,
|
129 |
|
|
'#default_value' => $timezone,
|
130 |
|
|
'#weight' => $instance['widget']['weight'] + 1,
|
131 |
|
|
'#attributes' => array('class' => array('date-no-float')),
|
132 |
|
|
'#date_label_position' => $instance['widget']['settings']['label_position'],
|
133 |
b720ea3e
|
Assos Assos
|
);
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
// Make changes if instance is set to be rendered as a regular field.
|
137 |
|
|
if (!empty($instance['widget']['settings']['no_fieldset'])) {
|
138 |
599a39cd
|
Assos Assos
|
$element['#title'] = check_plain($instance['label']);
|
139 |
|
|
$element['#theme_wrappers'] = ($field['cardinality'] == 1) ? array('date_form_element') : array();
|
140 |
85ad3d82
|
Assos Assos
|
}
|
141 |
|
|
|
142 |
|
|
return $element;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
/**
|
146 |
|
|
* Create local date object.
|
147 |
|
|
*
|
148 |
599a39cd
|
Assos Assos
|
* Create a date object set to local time from the field and widget settings and
|
149 |
|
|
* item values. Default values for new entities are set by the default value
|
150 |
|
|
* callback, so don't need to be accounted for here.
|
151 |
85ad3d82
|
Assos Assos
|
*/
|
152 |
|
|
function date_local_date($item, $timezone, $field, $instance, $part = 'value') {
|
153 |
|
|
$value = $item[$part];
|
154 |
|
|
|
155 |
|
|
// If the value is empty, don't try to create a date object because it will
|
156 |
|
|
// end up being the current day.
|
157 |
|
|
if (empty($value)) {
|
158 |
|
|
return NULL;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
599a39cd
|
Assos Assos
|
// @todo Figure out how to replace date_fuzzy_datetime() function.
|
162 |
85ad3d82
|
Assos Assos
|
// Special case for ISO dates to create a valid date object for formatting.
|
163 |
|
|
// Is this still needed?
|
164 |
b720ea3e
|
Assos Assos
|
// @codingStandardsIgnoreStart
|
165 |
85ad3d82
|
Assos Assos
|
/*
|
166 |
|
|
if ($field['type'] == DATE_ISO) {
|
167 |
|
|
$value = date_fuzzy_datetime($value);
|
168 |
|
|
}
|
169 |
|
|
else {
|
170 |
|
|
$db_timezone = date_get_timezone_db($field['settings']['tz_handling']);
|
171 |
|
|
$value = date_convert($value, $field['type'], DATE_DATETIME, $db_timezone);
|
172 |
|
|
}
|
173 |
|
|
*/
|
174 |
b720ea3e
|
Assos Assos
|
// @codingStandardsIgnoreEnd
|
175 |
85ad3d82
|
Assos Assos
|
|
176 |
|
|
$date = new DateObject($value, date_get_timezone_db($field['settings']['tz_handling']));
|
177 |
|
|
$date->limitGranularity($field['settings']['granularity']);
|
178 |
|
|
if (empty($date)) {
|
179 |
|
|
return NULL;
|
180 |
|
|
}
|
181 |
|
|
date_timezone_set($date, timezone_open($timezone));
|
182 |
|
|
|
183 |
|
|
return $date;
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
/**
|
187 |
|
|
* The callback for setting a default value for an empty date field.
|
188 |
|
|
*/
|
189 |
|
|
function date_default_value($field, $instance, $langcode) {
|
190 |
|
|
$item = array();
|
191 |
|
|
$db_format = date_type_format($field['type']);
|
192 |
|
|
$date = date_default_value_part($item, $field, $instance, $langcode, 'value');
|
193 |
|
|
$item[0]['value'] = is_object($date) ? date_format($date, $db_format) : '';
|
194 |
|
|
if (!empty($field['settings']['todate'])) {
|
195 |
|
|
$date = date_default_value_part($item, $field, $instance, $langcode, 'value2');
|
196 |
|
|
$item[0]['value2'] = is_object($date) ? date_format($date, $db_format) : '';
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
// Make sure the default value has the same construct as a loaded field value
|
200 |
|
|
// to avoid errors if the default value is used on a hidden element.
|
201 |
|
|
$item[0]['timezone'] = date_get_timezone($field['settings']['tz_handling']);
|
202 |
|
|
$item[0]['timezone_db'] = date_get_timezone_db($field['settings']['tz_handling']);
|
203 |
|
|
$item[0]['date_type'] = $field['type'];
|
204 |
|
|
if (!isset($item[0]['value2'])) {
|
205 |
|
|
$item[0]['value2'] = $item[0]['value'];
|
206 |
|
|
}
|
207 |
|
|
return $item;
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
/**
|
211 |
599a39cd
|
Assos Assos
|
* Default value callback to set either 'value', 'value2' to its default value.
|
212 |
85ad3d82
|
Assos Assos
|
*/
|
213 |
|
|
function date_default_value_part($item, $field, $instance, $langcode, $part = 'value') {
|
214 |
|
|
$timezone = date_get_timezone($field['settings']['tz_handling']);
|
215 |
|
|
$timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
|
216 |
|
|
$date = NULL;
|
217 |
|
|
if ($part == 'value') {
|
218 |
|
|
$default_value = $instance['settings']['default_value'];
|
219 |
|
|
$default_value_code = $instance['settings']['default_value_code'];
|
220 |
|
|
}
|
221 |
|
|
else {
|
222 |
|
|
$default_value = $instance['settings']['default_value2'];
|
223 |
|
|
$default_value_code = $instance['settings']['default_value_code2'];
|
224 |
|
|
}
|
225 |
|
|
if (empty($default_value) || $default_value == 'blank') {
|
226 |
|
|
return NULL;
|
227 |
|
|
}
|
228 |
|
|
elseif ($default_value == 'strtotime' && !empty($default_value_code)) {
|
229 |
|
|
$date = new DateObject($default_value_code, date_default_timezone());
|
230 |
|
|
}
|
231 |
|
|
elseif ($part == 'value2' && $default_value == 'same') {
|
232 |
|
|
if ($instance['settings']['default_value'] == 'blank' || empty($item[0]['value'])) {
|
233 |
|
|
return NULL;
|
234 |
|
|
}
|
235 |
|
|
else {
|
236 |
599a39cd
|
Assos Assos
|
// The date stored in 'value' has already been switched to the db
|
237 |
|
|
// timezone.
|
238 |
85ad3d82
|
Assos Assos
|
$date = new DateObject($item[0]['value'], $timezone_db, DATE_FORMAT_DATETIME);
|
239 |
|
|
}
|
240 |
|
|
}
|
241 |
599a39cd
|
Assos Assos
|
// Special case for 'now' when using dates with no timezone, make sure 'now'
|
242 |
|
|
// isn't adjusted to UTC value of 'now'.
|
243 |
85ad3d82
|
Assos Assos
|
elseif ($field['settings']['tz_handling'] == 'none') {
|
244 |
|
|
$date = date_now();
|
245 |
|
|
}
|
246 |
|
|
else {
|
247 |
|
|
$date = date_now($timezone);
|
248 |
|
|
}
|
249 |
|
|
// The default value needs to be in the database timezone.
|
250 |
|
|
date_timezone_set($date, timezone_open($timezone_db));
|
251 |
|
|
$date->limitGranularity($field['settings']['granularity']);
|
252 |
|
|
return $date;
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
/**
|
256 |
|
|
* Process an individual date element.
|
257 |
|
|
*/
|
258 |
|
|
function date_combo_element_process($element, &$form_state, $form) {
|
259 |
|
|
if (date_hidden_element($element)) {
|
260 |
|
|
// A hidden value for a new entity that had its end date set to blank
|
261 |
|
|
// will not get processed later to populate the end date, so set it here.
|
262 |
|
|
if (isset($element['#value']['value2']) && empty($element['#value']['value2'])) {
|
263 |
|
|
$element['#value']['value2'] = $element['#value']['value'];
|
264 |
|
|
}
|
265 |
|
|
return $element;
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
$field_name = $element['#field_name'];
|
269 |
|
|
$delta = $element['#delta'];
|
270 |
|
|
$bundle = $element['#bundle'];
|
271 |
|
|
$entity_type = $element['#entity_type'];
|
272 |
|
|
$langcode = $element['#language'];
|
273 |
|
|
$date_is_default = $element['#date_is_default'];
|
274 |
|
|
|
275 |
|
|
$field = field_widget_field($element, $form_state);
|
276 |
|
|
$instance = field_widget_instance($element, $form_state);
|
277 |
|
|
|
278 |
599a39cd
|
Assos Assos
|
// Figure out how many items are in the form, including new ones added by
|
279 |
|
|
// ajax.
|
280 |
85ad3d82
|
Assos Assos
|
$field_state = field_form_get_state($element['#field_parents'], $field_name, $element['#language'], $form_state);
|
281 |
|
|
$items_count = $field_state['items_count'];
|
282 |
|
|
|
283 |
|
|
$columns = $element['#columns'];
|
284 |
|
|
if (isset($columns['rrule'])) {
|
285 |
|
|
unset($columns['rrule']);
|
286 |
|
|
}
|
287 |
|
|
$from_field = 'value';
|
288 |
|
|
$to_field = 'value2';
|
289 |
|
|
$tz_field = 'timezone';
|
290 |
|
|
$offset_field = 'offset';
|
291 |
|
|
$offset_field2 = 'offset2';
|
292 |
|
|
|
293 |
599a39cd
|
Assos Assos
|
// Convert UTC dates to their local values in DATETIME format, and adjust the
|
294 |
|
|
// default values as specified in the field settings. It would seem to make
|
295 |
|
|
// sense to do this conversion when the data is loaded instead of when the
|
296 |
|
|
// form is created, but the loaded field data is cached and we can't cache
|
297 |
|
|
// dates that have been converted to the timezone of an individual user, so
|
298 |
|
|
// we cache the UTC values instead and do our conversion to local dates in
|
299 |
|
|
// the form and in the formatters.
|
300 |
85ad3d82
|
Assos Assos
|
$process = date_process_values($field, $instance);
|
301 |
|
|
foreach ($process as $processed) {
|
302 |
|
|
if (!isset($element['#default_value'][$processed])) {
|
303 |
599a39cd
|
Assos Assos
|
if (empty($element['#default_value']) || !is_array($element['#default_value'])) {
|
304 |
|
|
$element['#default_value'] = array();
|
305 |
|
|
}
|
306 |
85ad3d82
|
Assos Assos
|
$element['#default_value'][$processed] = '';
|
307 |
|
|
}
|
308 |
|
|
$date = date_local_date($element['#default_value'], $element['#date_timezone'], $field, $instance, $processed);
|
309 |
|
|
$element['#default_value'][$processed] = is_object($date) ? date_format($date, DATE_FORMAT_DATETIME) : '';
|
310 |
|
|
}
|
311 |
|
|
|
312 |
|
|
// Blank out the end date for optional end dates that match the start date,
|
313 |
599a39cd
|
Assos Assos
|
// except when this is a new node that has default values that should be
|
314 |
|
|
// honored.
|
315 |
85ad3d82
|
Assos Assos
|
if (!$date_is_default && $field['settings']['todate'] != 'required'
|
316 |
599a39cd
|
Assos Assos
|
&& is_array($element['#default_value'])
|
317 |
|
|
&& !empty($element['#default_value'][$to_field])
|
318 |
|
|
&& $element['#default_value'][$to_field] == $element['#default_value'][$from_field]) {
|
319 |
85ad3d82
|
Assos Assos
|
unset($element['#default_value'][$to_field]);
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
$show_todate = !empty($form_state['values']['show_todate']) || !empty($element['#default_value'][$to_field]) || $field['settings']['todate'] == 'required';
|
323 |
|
|
$element['show_todate'] = array(
|
324 |
|
|
'#title' => t('Show End Date'),
|
325 |
|
|
'#type' => 'checkbox',
|
326 |
|
|
'#default_value' => $show_todate,
|
327 |
|
|
'#weight' => -20,
|
328 |
|
|
'#access' => $field['settings']['todate'] == 'optional',
|
329 |
|
|
'#prefix' => '<div class="date-float">',
|
330 |
|
|
'#suffix' => '</div>',
|
331 |
|
|
);
|
332 |
|
|
|
333 |
|
|
$parents = $element['#parents'];
|
334 |
|
|
$first_parent = array_shift($parents);
|
335 |
|
|
$show_id = $first_parent . '[' . implode('][', $parents) . '][show_todate]';
|
336 |
|
|
|
337 |
|
|
$element[$from_field] = array(
|
338 |
|
|
'#field' => $field,
|
339 |
|
|
'#instance' => $instance,
|
340 |
|
|
'#weight' => $instance['widget']['weight'],
|
341 |
|
|
'#required' => ($element['#required'] && $delta == 0) ? 1 : 0,
|
342 |
|
|
'#default_value' => isset($element['#default_value'][$from_field]) ? $element['#default_value'][$from_field] : '',
|
343 |
|
|
'#delta' => $delta,
|
344 |
|
|
'#date_timezone' => $element['#date_timezone'],
|
345 |
|
|
'#date_format' => date_limit_format(date_input_format($element, $field, $instance), $field['settings']['granularity']),
|
346 |
|
|
'#date_text_parts' => (array) $instance['widget']['settings']['text_parts'],
|
347 |
|
|
'#date_increment' => $instance['widget']['settings']['increment'],
|
348 |
|
|
'#date_year_range' => $instance['widget']['settings']['year_range'],
|
349 |
|
|
'#date_label_position' => $instance['widget']['settings']['label_position'],
|
350 |
b720ea3e
|
Assos Assos
|
);
|
351 |
85ad3d82
|
Assos Assos
|
|
352 |
1f683914
|
Assos Assos
|
// Date repeat is a multiple value field. So the description is removed from
|
353 |
|
|
// the single element earlier. Let's get it back.
|
354 |
|
|
if (isset($element['show_repeat_settings']) && !empty($element['value']['#instance']['description'])) {
|
355 |
|
|
$element['#description'] = $element['value']['#instance']['description'];
|
356 |
|
|
}
|
357 |
85ad3d82
|
Assos Assos
|
|
358 |
599a39cd
|
Assos Assos
|
// Give this element the right type, using a Date API or a Date Popup element
|
359 |
|
|
// type.
|
360 |
85ad3d82
|
Assos Assos
|
$element[$from_field]['#attributes'] = array('class' => array('date-clear'));
|
361 |
|
|
$element[$from_field]['#wrapper_attributes'] = array('class' => array());
|
362 |
|
|
$element[$from_field]['#wrapper_attributes']['class'][] = 'date-no-float';
|
363 |
|
|
|
364 |
|
|
switch ($instance['widget']['type']) {
|
365 |
|
|
case 'date_select':
|
366 |
|
|
$element[$from_field]['#type'] = 'date_select';
|
367 |
|
|
$element[$from_field]['#theme_wrappers'] = array('date_select');
|
368 |
|
|
$element['#attached']['js'][] = drupal_get_path('module', 'date') . '/date.js';
|
369 |
|
|
$element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
|
370 |
|
|
break;
|
371 |
b720ea3e
|
Assos Assos
|
|
372 |
85ad3d82
|
Assos Assos
|
case 'date_popup':
|
373 |
|
|
$element[$from_field]['#type'] = 'date_popup';
|
374 |
|
|
$element[$from_field]['#theme_wrappers'] = array('date_popup');
|
375 |
599a39cd
|
Assos Assos
|
// Disable autocomplete in browsers.
|
376 |
|
|
// @see https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
|
377 |
|
|
$element[$from_field]['#attributes']['autocomplete'] = 'off';
|
378 |
85ad3d82
|
Assos Assos
|
$element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
|
379 |
|
|
break;
|
380 |
b720ea3e
|
Assos Assos
|
|
381 |
85ad3d82
|
Assos Assos
|
default:
|
382 |
|
|
$element[$from_field]['#type'] = 'date_text';
|
383 |
|
|
$element[$from_field]['#theme_wrappers'] = array('date_text');
|
384 |
|
|
$element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
|
385 |
|
|
}
|
386 |
|
|
|
387 |
599a39cd
|
Assos Assos
|
// If this field uses the 'End', add matching element for the 'End' date, and
|
388 |
|
|
// adapt titles to make it clear which is the 'Start' and which is the 'End' .
|
389 |
85ad3d82
|
Assos Assos
|
if (!empty($field['settings']['todate'])) {
|
390 |
|
|
$element[$to_field] = $element[$from_field];
|
391 |
|
|
$element[$from_field]['#title_display'] = 'none';
|
392 |
|
|
$element[$to_field]['#title'] = t('to:');
|
393 |
|
|
$element[$from_field]['#wrapper_attributes']['class'][] = 'start-date-wrapper';
|
394 |
|
|
$element[$to_field]['#wrapper_attributes']['class'][] = 'end-date-wrapper';
|
395 |
|
|
$element[$to_field]['#default_value'] = isset($element['#default_value'][$to_field]) ? $element['#default_value'][$to_field] : '';
|
396 |
|
|
$element[$to_field]['#required'] = ($element[$from_field]['#required'] && $field['settings']['todate'] == 'required');
|
397 |
|
|
$element[$to_field]['#weight'] += .2;
|
398 |
|
|
$element[$to_field]['#prefix'] = '';
|
399 |
|
|
// Users with JS enabled will never see initially blank values for the end
|
400 |
|
|
// date (see Drupal.date.EndDateHandler()), so hide the message for them.
|
401 |
1f683914
|
Assos Assos
|
$element['#description'] .= '<span class="js-hide"> ' . t("Empty 'End date' values will use the 'Start date' values.") . '</span>';
|
402 |
85ad3d82
|
Assos Assos
|
if ($field['settings']['todate'] == 'optional') {
|
403 |
|
|
$element[$to_field]['#states'] = array(
|
404 |
|
|
'visible' => array(
|
405 |
b720ea3e
|
Assos Assos
|
'input[name="' . $show_id . '"]' => array(
|
406 |
|
|
'checked' => TRUE,
|
407 |
|
|
),
|
408 |
|
|
),
|
409 |
|
|
);
|
410 |
85ad3d82
|
Assos Assos
|
}
|
411 |
|
|
}
|
412 |
|
|
|
413 |
|
|
// Create label for error messages that make sense in multiple values
|
414 |
|
|
// and when the title field is left blank.
|
415 |
|
|
if ($field['cardinality'] <> 1 && empty($field['settings']['repeat'])) {
|
416 |
|
|
$element[$from_field]['#date_title'] = t('@field_name Start date value #@delta', array('@field_name' => $instance['label'], '@delta' => $delta + 1));
|
417 |
|
|
if (!empty($field['settings']['todate'])) {
|
418 |
|
|
$element[$to_field]['#date_title'] = t('@field_name End date value #@delta', array('@field_name' => $instance['label'], '@delta' => $delta + 1));
|
419 |
|
|
}
|
420 |
|
|
}
|
421 |
|
|
elseif (!empty($field['settings']['todate'])) {
|
422 |
|
|
$element[$from_field]['#date_title'] = t('@field_name Start date', array('@field_name' => $instance['label']));
|
423 |
|
|
$element[$to_field]['#date_title'] = t('@field_name End date', array('@field_name' => $instance['label']));
|
424 |
|
|
}
|
425 |
|
|
else {
|
426 |
db9ffd17
|
Assos Assos
|
$element[$from_field]['#date_title'] = t('@field_name', array('@field_name' => $instance['label']));
|
427 |
85ad3d82
|
Assos Assos
|
}
|
428 |
|
|
|
429 |
b720ea3e
|
Assos Assos
|
// Make changes if instance is set to be rendered as a regular field.
|
430 |
|
|
if (!empty($instance['widget']['settings']['no_fieldset'])) {
|
431 |
|
|
unset($element[$from_field]['#description']);
|
432 |
|
|
if (!empty($field['settings']['todate']) && isset($element['#description'])) {
|
433 |
|
|
$element['#description'] .= '<span class="js-hide"> ' . t("Empty 'End date' values will use the 'Start date' values.") . '</span>';
|
434 |
|
|
}
|
435 |
|
|
}
|
436 |
|
|
|
437 |
85ad3d82
|
Assos Assos
|
$context = array(
|
438 |
599a39cd
|
Assos Assos
|
'field' => $field,
|
439 |
|
|
'instance' => $instance,
|
440 |
|
|
'form' => $form,
|
441 |
85ad3d82
|
Assos Assos
|
);
|
442 |
|
|
drupal_alter('date_combo_process', $element, $form_state, $context);
|
443 |
|
|
|
444 |
|
|
return $element;
|
445 |
|
|
}
|
446 |
|
|
|
447 |
b720ea3e
|
Assos Assos
|
/**
|
448 |
|
|
* Empty a date element.
|
449 |
|
|
*/
|
450 |
85ad3d82
|
Assos Assos
|
function date_element_empty($element, &$form_state) {
|
451 |
|
|
$item = array();
|
452 |
|
|
$item['value'] = NULL;
|
453 |
599a39cd
|
Assos Assos
|
$item['value2'] = NULL;
|
454 |
|
|
$item['timezone'] = NULL;
|
455 |
85ad3d82
|
Assos Assos
|
$item['offset'] = NULL;
|
456 |
|
|
$item['offset2'] = NULL;
|
457 |
|
|
$item['rrule'] = NULL;
|
458 |
|
|
form_set_value($element, $item, $form_state);
|
459 |
|
|
return $item;
|
460 |
|
|
}
|
461 |
|
|
|
462 |
|
|
/**
|
463 |
|
|
* Validate and update a combo element.
|
464 |
b720ea3e
|
Assos Assos
|
*
|
465 |
85ad3d82
|
Assos Assos
|
* Don't try this if there were errors before reaching this point.
|
466 |
|
|
*/
|
467 |
|
|
function date_combo_validate($element, &$form_state) {
|
468 |
599a39cd
|
Assos Assos
|
// Disabled and hidden elements won't have any input and don't need
|
469 |
|
|
// validation, we just need to re-save the original values, from before they
|
470 |
|
|
// were processed into widget arrays and timezone-adjusted.
|
471 |
85ad3d82
|
Assos Assos
|
if (date_hidden_element($element) || !empty($element['#disabled'])) {
|
472 |
|
|
form_set_value($element, $element['#date_items'], $form_state);
|
473 |
|
|
return;
|
474 |
|
|
}
|
475 |
|
|
|
476 |
|
|
$field_name = $element['#field_name'];
|
477 |
|
|
$delta = $element['#delta'];
|
478 |
|
|
$langcode = $element['#language'];
|
479 |
|
|
|
480 |
b720ea3e
|
Assos Assos
|
// Related issue: https://drupal.org/node/2279831.
|
481 |
|
|
if (!is_array($element['#field_parents'])) {
|
482 |
|
|
$element['#field_parents'] = array();
|
483 |
|
|
}
|
484 |
85ad3d82
|
Assos Assos
|
$form_values = drupal_array_get_nested_value($form_state['values'], $element['#field_parents']);
|
485 |
|
|
$form_input = drupal_array_get_nested_value($form_state['input'], $element['#field_parents']);
|
486 |
|
|
|
487 |
ee46a8ed
|
Assos Assos
|
// Programmatically calling drupal_submit_form() does not always add the date
|
488 |
|
|
// combo to $form_state['input'].
|
489 |
|
|
if (empty($form_input[$field_name]) && !empty($form_values[$field_name])) {
|
490 |
|
|
form_set_value($element, $element['#date_items'], $form_state);
|
491 |
|
|
return;
|
492 |
|
|
}
|
493 |
85ad3d82
|
Assos Assos
|
// If the whole field is empty and that's OK, stop now.
|
494 |
|
|
if (empty($form_input[$field_name]) && !$element['#required']) {
|
495 |
|
|
return;
|
496 |
|
|
}
|
497 |
|
|
|
498 |
|
|
$item = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
|
499 |
|
|
$posted = drupal_array_get_nested_value($form_state['input'], $element['#parents']);
|
500 |
|
|
|
501 |
|
|
$field = field_widget_field($element, $form_state);
|
502 |
|
|
$instance = field_widget_instance($element, $form_state);
|
503 |
|
|
|
504 |
|
|
$context = array(
|
505 |
|
|
'field' => $field,
|
506 |
|
|
'instance' => $instance,
|
507 |
|
|
'item' => $item,
|
508 |
|
|
);
|
509 |
|
|
|
510 |
|
|
drupal_alter('date_combo_pre_validate', $element, $form_state, $context);
|
511 |
|
|
|
512 |
|
|
$from_field = 'value';
|
513 |
|
|
$to_field = 'value2';
|
514 |
|
|
$tz_field = 'timezone';
|
515 |
|
|
$offset_field = 'offset';
|
516 |
|
|
$offset_field2 = 'offset2';
|
517 |
|
|
|
518 |
599a39cd
|
Assos Assos
|
// Check for empty 'Start date', which could either be an empty value or an
|
519 |
|
|
// array of empty values, depending on the widget.
|
520 |
85ad3d82
|
Assos Assos
|
$empty = TRUE;
|
521 |
|
|
if (!empty($item[$from_field])) {
|
522 |
|
|
if (!is_array($item[$from_field])) {
|
523 |
|
|
$empty = FALSE;
|
524 |
|
|
}
|
525 |
|
|
else {
|
526 |
|
|
foreach ($item[$from_field] as $key => $value) {
|
527 |
|
|
if (!empty($value)) {
|
528 |
|
|
$empty = FALSE;
|
529 |
|
|
break;
|
530 |
|
|
}
|
531 |
|
|
}
|
532 |
|
|
}
|
533 |
|
|
}
|
534 |
|
|
|
535 |
|
|
// An 'End' date without a 'Start' date is a validation error.
|
536 |
|
|
if ($empty && !empty($item[$to_field])) {
|
537 |
|
|
if (!is_array($item[$to_field])) {
|
538 |
|
|
form_error($element, t("A 'Start date' date is required if an 'end date' is supplied for field %field #%delta.", array('%delta' => $field['cardinality'] ? intval($delta + 1) : '', '%field' => $instance['label'])));
|
539 |
|
|
$empty = FALSE;
|
540 |
|
|
}
|
541 |
|
|
else {
|
542 |
|
|
foreach ($item[$to_field] as $key => $value) {
|
543 |
|
|
if (!empty($value)) {
|
544 |
|
|
form_error($element, t("A 'Start date' date is required if an 'End date' is supplied for field %field #%delta.", array('%delta' => $field['cardinality'] ? intval($delta + 1) : '', '%field' => $instance['label'])));
|
545 |
|
|
$empty = FALSE;
|
546 |
|
|
break;
|
547 |
|
|
}
|
548 |
|
|
}
|
549 |
|
|
}
|
550 |
|
|
}
|
551 |
|
|
|
552 |
|
|
// If the user chose the option to not show the end date, just swap in the
|
553 |
|
|
// start date as that value so the start and end dates are the same.
|
554 |
|
|
if ($field['settings']['todate'] == 'optional' && empty($item['show_todate'])) {
|
555 |
|
|
$item[$to_field] = $item[$from_field];
|
556 |
|
|
$posted[$to_field] = $posted[$from_field];
|
557 |
|
|
}
|
558 |
|
|
|
559 |
|
|
if ($empty) {
|
560 |
|
|
$item = date_element_empty($element, $form_state);
|
561 |
|
|
if (!$element['#required']) {
|
562 |
|
|
return;
|
563 |
|
|
}
|
564 |
|
|
}
|
565 |
ee46a8ed
|
Assos Assos
|
else {
|
566 |
85ad3d82
|
Assos Assos
|
$timezone = !empty($item[$tz_field]) ? $item[$tz_field] : $element['#date_timezone'];
|
567 |
|
|
$timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
|
568 |
|
|
$element[$from_field]['#date_timezone'] = $timezone;
|
569 |
|
|
$from_date = date_input_date($field, $instance, $element[$from_field], $posted[$from_field]);
|
570 |
|
|
|
571 |
|
|
if (!empty($field['settings']['todate'])) {
|
572 |
|
|
$element[$to_field]['#date_timezone'] = $timezone;
|
573 |
|
|
$to_date = date_input_date($field, $instance, $element[$to_field], $posted[$to_field]);
|
574 |
|
|
}
|
575 |
|
|
else {
|
576 |
|
|
$to_date = $from_date;
|
577 |
|
|
}
|
578 |
|
|
|
579 |
|
|
// Neither the start date nor the end date should be empty at this point
|
580 |
|
|
// unless they held values that couldn't be evaluated.
|
581 |
|
|
if (!$instance['required'] && (!date_is_date($from_date) || !date_is_date($to_date))) {
|
582 |
|
|
$item = date_element_empty($element, $form_state);
|
583 |
|
|
$errors[] = t('The dates are invalid.');
|
584 |
|
|
}
|
585 |
|
|
elseif (!empty($field['settings']['todate']) && $from_date > $to_date) {
|
586 |
|
|
form_set_value($element[$to_field], $to_date, $form_state);
|
587 |
|
|
$errors[] = t('The End date must be greater than the Start date.');
|
588 |
|
|
}
|
589 |
|
|
else {
|
590 |
599a39cd
|
Assos Assos
|
// Convert input dates back to their UTC values and re-format to ISO or
|
591 |
|
|
// UNIX instead of the DATETIME format used in element processing.
|
592 |
85ad3d82
|
Assos Assos
|
$item[$tz_field] = $timezone;
|
593 |
|
|
|
594 |
|
|
// Update the context for changes in the $item, and allow other modules to
|
595 |
|
|
// alter the computed local dates.
|
596 |
|
|
$context['item'] = $item;
|
597 |
|
|
// We can only pass two additional values to drupal_alter, so $element
|
598 |
|
|
// needs to be included in $context.
|
599 |
|
|
$context['element'] = $element;
|
600 |
599a39cd
|
Assos Assos
|
|
601 |
|
|
// Trigger hook_date_combo_validate_date_start_alter().
|
602 |
85ad3d82
|
Assos Assos
|
drupal_alter('date_combo_validate_date_start', $from_date, $form_state, $context);
|
603 |
599a39cd
|
Assos Assos
|
|
604 |
|
|
// Trigger hook_date_combo_validate_date_end_alter().
|
605 |
85ad3d82
|
Assos Assos
|
drupal_alter('date_combo_validate_date_end', $to_date, $form_state, $context);
|
606 |
|
|
|
607 |
|
|
$item[$offset_field] = date_offset_get($from_date);
|
608 |
|
|
|
609 |
|
|
$test_from = date_format($from_date, 'r');
|
610 |
|
|
$test_to = date_format($to_date, 'r');
|
611 |
|
|
|
612 |
|
|
$item[$offset_field2] = date_offset_get($to_date);
|
613 |
|
|
date_timezone_set($from_date, timezone_open($timezone_db));
|
614 |
|
|
date_timezone_set($to_date, timezone_open($timezone_db));
|
615 |
|
|
$item[$from_field] = date_format($from_date, date_type_format($field['type']));
|
616 |
|
|
$item[$to_field] = date_format($to_date, date_type_format($field['type']));
|
617 |
|
|
if (isset($form_values[$field_name]['rrule'])) {
|
618 |
|
|
$item['rrule'] = $form_values[$field['field_name']]['rrule'];
|
619 |
|
|
}
|
620 |
|
|
|
621 |
599a39cd
|
Assos Assos
|
// If the db timezone is not the same as the display timezone and we are
|
622 |
|
|
// using a date with time granularity, test a roundtrip back to the
|
623 |
|
|
// original timezone to catch invalid dates, like 2AM on the day that
|
624 |
|
|
// spring daylight savings time begins in the US.
|
625 |
85ad3d82
|
Assos Assos
|
$granularity = date_format_order($element[$from_field]['#date_format']);
|
626 |
|
|
if ($timezone != $timezone_db && date_has_time($granularity)) {
|
627 |
|
|
date_timezone_set($from_date, timezone_open($timezone));
|
628 |
|
|
date_timezone_set($to_date, timezone_open($timezone));
|
629 |
|
|
|
630 |
|
|
if ($test_from != date_format($from_date, 'r')) {
|
631 |
|
|
$errors[] = t('The Start date is invalid.');
|
632 |
|
|
}
|
633 |
|
|
if ($test_to != date_format($to_date, 'r')) {
|
634 |
|
|
$errors[] = t('The End date is invalid.');
|
635 |
|
|
}
|
636 |
|
|
}
|
637 |
|
|
if (empty($errors)) {
|
638 |
|
|
form_set_value($element, $item, $form_state);
|
639 |
|
|
}
|
640 |
|
|
}
|
641 |
|
|
}
|
642 |
599a39cd
|
Assos Assos
|
|
643 |
|
|
// Don't show further errors if errors are already flagged because otherwise
|
644 |
|
|
// we'll show errors on the nested elements more than once.
|
645 |
ee46a8ed
|
Assos Assos
|
if (!form_get_errors() && !empty($errors)) {
|
646 |
85ad3d82
|
Assos Assos
|
if ($field['cardinality']) {
|
647 |
|
|
form_error($element, t('There are errors in @field_name value #@delta:', array('@field_name' => $instance['label'], '@delta' => $delta + 1)) . theme('item_list', array('items' => $errors)));
|
648 |
|
|
}
|
649 |
|
|
else {
|
650 |
|
|
form_error($element, t('There are errors in @field_name:', array('@field_name' => $instance['label'])) . theme('item_list', array('items' => $errors)));
|
651 |
|
|
}
|
652 |
|
|
}
|
653 |
|
|
}
|
654 |
|
|
|
655 |
|
|
/**
|
656 |
|
|
* Determine the input format for this element.
|
657 |
|
|
*/
|
658 |
|
|
function date_input_format($element, $field, $instance) {
|
659 |
|
|
if (!empty($instance['widget']['settings']['input_format_custom'])) {
|
660 |
|
|
return $instance['widget']['settings']['input_format_custom'];
|
661 |
|
|
}
|
662 |
|
|
elseif (!empty($instance['widget']['settings']['input_format']) && $instance['widget']['settings']['input_format'] != 'site-wide') {
|
663 |
|
|
return $instance['widget']['settings']['input_format'];
|
664 |
|
|
}
|
665 |
|
|
return variable_get('date_format_short', 'm/d/Y - H:i');
|
666 |
|
|
}
|
667 |
|
|
|
668 |
|
|
/**
|
669 |
|
|
* Implements hook_date_select_pre_validate_alter().
|
670 |
|
|
*/
|
671 |
|
|
function date_date_select_pre_validate_alter(&$element, &$form_state, &$input) {
|
672 |
|
|
date_empty_end_date($element, $form_state, $input);
|
673 |
|
|
}
|
674 |
|
|
|
675 |
|
|
/**
|
676 |
|
|
* Implements hook_date_text_pre_validate_alter().
|
677 |
|
|
*/
|
678 |
|
|
function date_date_text_pre_validate_alter(&$element, &$form_state, &$input) {
|
679 |
|
|
date_empty_end_date($element, $form_state, $input);
|
680 |
|
|
}
|
681 |
|
|
|
682 |
|
|
/**
|
683 |
|
|
* Implements hook_date_popup_pre_validate_alter().
|
684 |
|
|
*/
|
685 |
|
|
function date_date_popup_pre_validate_alter(&$element, &$form_state, &$input) {
|
686 |
|
|
date_empty_end_date($element, $form_state, $input);
|
687 |
|
|
}
|
688 |
|
|
|
689 |
|
|
/**
|
690 |
|
|
* Helper function to clear out end date when not being used.
|
691 |
|
|
*/
|
692 |
|
|
function date_empty_end_date(&$element, &$form_state, &$input) {
|
693 |
599a39cd
|
Assos Assos
|
// If this is the end date and the option to show an end date has not been
|
694 |
|
|
// selected, empty the end date to surpress validation errors and stop
|
695 |
|
|
// further processing.
|
696 |
85ad3d82
|
Assos Assos
|
$parents = $element['#parents'];
|
697 |
|
|
$parent = array_pop($parents);
|
698 |
|
|
if ($parent == 'value2') {
|
699 |
|
|
$parent_values = drupal_array_get_nested_value($form_state['values'], $parents);
|
700 |
|
|
if (isset($parent_values['show_todate']) && $parent_values['show_todate'] != 1) {
|
701 |
|
|
$input = array();
|
702 |
|
|
form_set_value($element, NULL, $form_state);
|
703 |
|
|
}
|
704 |
|
|
}
|
705 |
|
|
} |