function _date_widget in Date 6.2
Same name and namespace in other branches
- 5.2 date/date_elements.inc \_date_widget()
- 6 date/date_elements.inc \_date_widget()
Private implementation of hook_widget().
The widget builds out a complex date element in the following way:
- A field is pulled out of the database which is comprised of one or more collections of from/to dates.
- The dates in this field are all converted from the UTC values stored in the database back to the local time before passing their values to FAPI.
- If values are empty, the field settings rules are used to determine if the default_values should be empty, now, the same, or use strtotime.
- Each from/to combination is created using the date_combo element type defined by the date module. If the timezone is date-specific, a timezone selector is added to the first combo element.
- If repeating dates are defined, a form to create a repeat rule is added to the field element.
- The date combo element creates two individual date elements, one each for the from and to field, using the appropriate individual Date API date elements, like selects, textfields, or popups.
- In the individual element validation, the data supplied by the user is used to update the individual date values.
- In the combo date validation, the timezone is updated, if necessary, then the user input date values are used with that timezone to create date objects, which are used update combo date timezone and offset values.
- In the field's submission processing, the new date values, which are in the local timezone, are converted back to their UTC values and stored.
1 call to _date_widget()
- date_widget in date/
date.module - Implementation of hook_widget().
File
- date/
date_elements.inc, line 102 - Date forms and form themes and validation.
Code
function _date_widget(&$form, &$form_state, &$field, $items, $delta = 0) {
// Be sure users that haven't run the update yet don't get
// a badly broken value.
if (!empty($field['repeat']) && !strstr($field['widget']['type'], '_repeat')) {
$field['multiple'] = 0;
return;
}
require_once './' . drupal_get_path('module', 'date_api') . '/date_api_elements.inc';
$timezone = date_get_timezone($field['tz_handling'], isset($items[0]['timezone']) ? $items[0]['timezone'] : date_default_timezone_name());
// TODO see if there's a way to keep the timezone element from ever being
// nested as array('timezone' => 'timezone' => value)). After struggling
// with this a while, I can find no way to get it displayed in the form
// correctly and get it to use the timezone element without ending up
// with nesting.
if (is_array($timezone)) {
$timezone = $timezone['timezone'];
}
// Convert UTC dates to their local values in DATETIME format,
// and adjust the default values as specified in the field settings.
// It would seem to make sense to do this conversion when the data
// is loaded instead of when the form is created, but the loaded
// field data is cached and we can't cache dates that have been converted
// to the timezone of an individual user, so we cache the UTC values
// instead and do our conversion to local dates in the form and
// in the formatters.
$process = date_process_values($field);
foreach ($process as $processed) {
if (!isset($items[$delta][$processed])) {
$items[$delta][$processed] = '';
}
$date = date_local_date($form, $form_state, $delta, $items[$delta], $timezone, $field, $processed);
$items[$delta][$processed] = is_object($date) ? date_format($date, DATE_FORMAT_DATETIME) : '';
}
$element = array(
'#type' => 'date_combo',
'#weight' => $delta,
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
'#date_timezone' => $timezone,
'#element_validate' => array(
'date_combo_validate',
'date_widget_validate',
),
);
if ($field['tz_handling'] == 'date') {
$element['timezone'] = array(
'#type' => 'date_timezone',
'#delta' => $delta,
'#default_value' => $timezone,
'#weight' => $field['widget']['weight'] + 0.2,
);
}
// Add a date repeat form element, if needed.
if (module_exists('date_repeat') && $field['repeat'] == 1) {
require_once './' . drupal_get_path('module', 'date') . '/date_repeat.inc';
_date_repeat_widget($element, $field, $items, $delta);
$element['rrule']['#weight'] = $field['widget']['weight'] + 0.4;
}
return $element;
}