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