You are here

function date_field in Date 5

Same name and namespace in other branches
  1. 5.2 date/date.module \date_field()
  2. 6.2 date/date.module \date_field()
  3. 6 date/date.module \date_field()

Implementation of hook_field().

File

./date.module, line 74
Defines a date/time field type.

Code

function date_field($op, &$node, $field, &$items, $teaser, $page) {
  $field_name = $field['field_name'];
  if ($field['todate']) {
    $process = array(
      'value',
      'value2',
    );
  }
  else {
    $process = array(
      'value',
    );
  }
  include_once drupal_get_path('module', 'date_api') . '/date.inc';
  switch ($op) {
    case 'validate':
      $formats = date_get_formats($field);
      foreach ($items as $delta => $item) {
        foreach ($process as $processed) {
          $error_field = $field['field_name'] . '][' . $delta . '][' . $processed;
          $error_field .= $field['widget']['type'] == 'date_select' ? '][year' : '';
          if ($processed == 'value' && $field['todate'] && !date_is_valid($item['value'], $field['type']) && date_is_valid($item['value2'], $field['type'])) {
            form_set_error($error_field, t("A 'From date' date is required for %field %delta", array(
              '%delta' => $field['multiple'] ? intval($delta + 1) : '',
              '%field' => t($field['widget']['label']),
            )));
          }
          if ($processed == 'value2' && $field['todate'] == 'required' && ($field['required'] && date_is_valid($item['value'], $field['type']) && !date_is_valid($item['value2'], $field['type']))) {
            form_set_error($error_field, t("A 'To date' is required for %field %delta", array(
              '%delta' => $field['multiple'] ? intval($delta + 1) : '',
              '%field' => t($field['widget']['label']),
            )));
          }
          if ($item[$processed] || date_is_required($field, $processed, $delta, $item['value'], $item['value2'])) {
            $date = date_make_date($item[$processed], $field['timezone'], 'db', $field['type']);
            if (!$date->db->parts['year']) {
              form_set_error($error_field, t('%name %delta %column is not a valid date.', array(
                '%name' => t($field['widget']['label']),
                '%delta' => $field['multiple'] ? $delta : '',
                '%column' => $processed == 'value2' ? t('To date') : (sizeof($process) == 2 ? t('From date') : ''),
              )));
            }
          }
        }
      }
      return $items;
    case 'update':
    case 'insert':

      // Unset undesired date part values in ISO dates.
      // Not sure this is the best place to do this, but form_set_value requires knowlege of the widget structure,
      // so that won't work in the field, and doing it in the widget get much more complex, so leaving it here for now.
      foreach ($items as $delta => $item) {
        foreach ($process as $processed) {
          if (!form_get_errors()) {
            $replace = date_unset_granularity($item[$processed], date_granularity_array($field), $field['type']);
            $items[$delta][$processed] = $replace !== 'ERROR' && $replace != $items[$delta][$processed] ? $replace : $items[$delta][$processed];
          }
        }
      }
      return $items;
  }
}