You are here

function date_popup_process_date_part in Date 7.2

Same name and namespace in other branches
  1. 7.3 date_popup/date_popup.module \date_popup_process_date_part()

Process the date portion of the element.

1 call to date_popup_process_date_part()
date_popup_element_process in date_popup/date_popup.module
Javascript popup element processing.

File

date_popup/date_popup.module, line 359
A module to enable jQuery calendar and time entry popups.

Code

function date_popup_process_date_part(&$element) {
  $granularity = date_format_order($element['#date_format']);
  $date_granularity = date_popup_date_granularity($element);
  if (empty($date_granularity)) {
    return array();
  }

  // The datepicker can't handle zero or negative values like 0:+1 even though
  // the Date API can handle them, so rework the value we pass to the
  // datepicker to use defaults it can accept (such as +0:+1)
  // date_range_string() adds the necessary +/- signs to the range string.
  $this_year = date_format(date_now(), 'Y');

  // When used as a Views exposed filter widget, $element['#value'] contains an
  // array instead an string. Fill the 'date' string in this case.
  $mock = NULL;
  $callback_values = date_popup_element_value_callback($element, FALSE, $mock);
  if (!isset($element['#value']['date']) && isset($callback_values['date'])) {
    if (!is_array($element['#value'])) {
      $element['#value'] = array();
    }
    $element['#value']['date'] = $callback_values['date'];
  }
  $date = '';
  if (!empty($element['#value']['date'])) {

    // @todo If date does not meet the granularity or format condtions, then
    // $date will have error condtions and '#default_value' below will have the
    // global default date (most likely formatted string from 'now'). There
    // will be an validation error message referring to the actual input date
    // but the form element will display formatted 'now'.
    $date = new DateObject($element['#value']['date'], $element['#date_timezone'], date_popup_date_format($element));
    if (!date_is_date($date)) {
      $date = '';
    }
  }
  $range = date_range_years($element['#date_year_range'], $date);
  $year_range = date_range_string($range);

  // Add the dynamic datepicker options. Allow element-specific datepicker
  // preferences to override these options for whatever reason they see fit.
  $settings = $element['#datepicker_options'] + array(
    'changeMonth' => TRUE,
    'changeYear' => TRUE,
    'autoPopUp' => 'focus',
    'closeAtTop' => FALSE,
    'speed' => 'immediate',
    'firstDay' => intval(variable_get('date_first_day', 0)),
    // 'buttonImage' => base_path()
    // . drupal_get_path('module', 'date_api') ."/images/calendar.png",
    // 'buttonImageOnly' => TRUE,
    'dateFormat' => date_popup_format_to_popup(date_popup_date_format($element), 'datepicker'),
    'yearRange' => $year_range,
    // Custom setting, will be expanded in Drupal.behaviors.date_popup()
    'fromTo' => isset($fromto),
  );
  if (!empty($element['#instance'])) {
    $settings['syncEndDate'] = $element['#instance']['settings']['default_value2'] == 'sync';
  }

  // Create a unique id for each set of custom settings.
  $id = date_popup_js_settings_id($element['#id'], 'datepicker', $settings);

  // Manually build this element and set the value - this will prevent
  // corrupting the parent value.
  // Do NOT set '#input' => FALSE as this prevents the form API from recursing
  // into this element (this was useful when the '#value' property was being
  // overwritten by the '#default_value' property).
  $parents = array_merge($element['#parents'], array(
    'date',
  ));
  $sub_element = array(
    '#type' => 'textfield',
    '#title' => theme('date_part_label_date', array(
      'part_type' => 'date',
      'element' => $element,
    )),
    '#title_display' => $element['#date_label_position'] == 'above' ? 'before' : 'invisible',
    '#default_value' => date_format_date($date, 'custom', date_popup_date_format($element)),
    '#id' => $id,
    '#size' => !empty($element['#size']) ? $element['#size'] : 20,
    '#maxlength' => !empty($element['#maxlength']) ? $element['#maxlength'] : 30,
    '#attributes' => $element['#attributes'],
    '#parents' => $parents,
    '#name' => array_shift($parents) . '[' . implode('][', $parents) . ']',
    '#ajax' => !empty($element['#ajax']) ? $element['#ajax'] : FALSE,
    '#required' => $element['#required'],
  );

  // Do NOT overwrite the actual input with the default value.
  // @todo Figure out exactly when this is needed, in many places it is not.
  $sub_element['#description'] = ' ' . t('E.g., @date', array(
    '@date' => date_format_date(date_example_date(), 'custom', date_popup_date_format($element)),
  ));
  return $sub_element;
}