You are here

function _date_widget in Date 6

Same name and namespace in other branches
  1. 5.2 date/date_elements.inc \_date_widget()
  2. 6.2 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 116
Date forms and form themes and validation.

Code

function _date_widget(&$form, &$form_state, &$field, $items, $delta = 0) {

  // If this is a multiple value form, get the Content module's
  // multiple value form with its AHAH add more element and return that.
  // Set a flag so we know to skip this step when the
  // content_multiple_value_form() re-invokes this widget.
  if (empty($field['repeat']) && empty($field['multiple_form'])) {
    $field_copy = $field;
    $field_copy['multiple_form'] = TRUE;
    return content_multiple_value_form($form, $form_state, $field_copy, $items);
  }
  include_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());

  // 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($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' && $delta == 0) {
    $element['timezone'] = array(
      '#type' => $delta == 0 ? 'date_timezone' : 'hidden',
      '#delta' => $delta,
      '#default_value' => $timezone,
      '#weight' => $delta + 0.2,
    );
  }

  // Add a date repeat form element, if needed.
  if (module_exists('date_repeat') && $field['repeat'] == 1) {
    include_once drupal_get_path('module', 'date') . '/date_repeat.inc';
    $element = array_merge($element, _date_repeat_widget($element, $field, $items, $delta));
  }
  return $element;
}