You are here

function datex_date_select_process_alter in Datex 7.3

Same name and namespace in other branches
  1. 7.2 datex_date.inc \datex_date_select_process_alter()

Implements hook_date_select_process_alter().

File

./datex_date.inc, line 219
Provides support for date module.

Code

function datex_date_select_process_alter(&$element, &$form_state) {
  if (!_datex_granularity_supported($element['#granularity'])) {
    return;
  }
  $calendar = _datex_date_field_calendar($element, TRUE, 'en');
  if (!$calendar) {
    return;
  }
  $l_calendar = _datex_date_field_calendar($element);
  $year_now = intval($calendar
    ->format('Y'));
  $valid = !empty($element['#value']['year']);
  $element_value = $valid ? $element['#value'] : $element['#default_value'];
  $date_array = $valid ? $element_value : [];
  $input = _datex_get_fs($element, $form_state);

  // Non-empty input means we have processed the element already. But there
  // has been an error on the form (form_set_error probably), and user has
  // re-submitted their form and it's values.
  // At this point we do not process (convert dates) again. It just blows up
  // everything.
  // But if the input is empty, it means we should process and convert dates
  // (this is the first time we are visiting this element).
  if (!empty($element_value) && empty($input)) {
    $date = is_string($element_value) ? new DateObject($element_value) : date_select_input_date($element, $element_value);
    if ($date) {
      _datex_fix_missing_granularity($date, $element, $calendar
        ->getCalendarName());
      $calendar
        ->setTimestamp($date
        ->format("U"));
      $date_array = $calendar
        ->formatArray();
      $element['#value'] = $date_array;
    }
  }
  if (in_array('year', $element['#granularity'])) {
    list($year_before, $year_after) = explode(':', $element['#date_year_range']);
    $year_before = intval($year_before);
    $year_after = intval($year_after);
    $element['year']['#options'] = $element['#required'] ? [] : [
      array_shift($element['year']['#options']),
    ];
    for ($year = $year_now + $year_before; $year <= $year_now + $year_after; $year++) {
      $element['year']['#options'][$year] = $year;
    }
    if ($valid) {
      $element['year']['#default_value'] = $date_array['year'];
    }
  }
  if (in_array('month', $element['#granularity'])) {
    $element['month']['#options'] = $l_calendar
      ->listOptions('monthNames', $element['#required']);
    if ($valid) {
      $element['month']['#default_value'] = $date_array['month'];
    }
  }
  if (in_array('day', $element['#granularity'])) {
    if ($valid) {
      $element['day']['#default_value'] = $date_array['day'];
    }
  }
}