1 date.elements.inc _date_popup_process_date_part(&$element)

Process the date portion of the element.

File

core/modules/date/date.elements.inc, line 1041
Date forms and form themes and validation.

Code

function _date_popup_process_date_part(&$element) {
  $date_granularity = _date_popup_date_granularity($element);
  if (empty($date_granularity)) {
    return array();
  }

  // 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'])) {
    $element['#value']['date'] = $callback_values['date'];
  }

  // 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.
  $date = '';
  if (!empty($element['#value']['date'])) {
    $date = new BackdropDateTime($element['#value']['date'], $element['#date_timezone'], _date_popup_date_format($element));
  }
  $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(config_get('system.date', 'first_day')),
    'dateFormat' => _date_popup_format_to_popup(_date_popup_date_format($element)),
    'yearRange' => $year_range,
  );

  // Make sure that any defaultDate is within the yearRange.
  if (!empty($settings['yearRange'])) {
    $parts = explode(':', $settings['yearRange']);
    // Set the default date to 0 or the lowest bound if
    // the date ranges do not include the current year.
    // Necessary for the datepicker to render and select dates correctly.
    $default_date = ($parts[0] > 0 || 0 > $parts[1]) ? $parts[0] : 0;
    $settings += array('defaultDate' => (string) $default_date . 'y');
  }

  // Manually build this element and set the value -
  // this will prevent corrupting the parent value.
  $parents = array_merge($element['#parents'], array('date'));
  $sub_element = array(
    '#type' => 'textfield',
    '#title' => $element['#title'],
    '#title_display' => $element['#date_label_position'] == 'above' ? 'before' : 'invisible',
    '#default_value' => date_format_date($date, 'custom', _date_popup_date_format($element)),
    '#input' => FALSE,
    '#size' => !empty($element['#size']) ? $element['#size'] : 20,
    '#maxlength' => !empty($element['#maxlength']) ? $element['#maxlength'] : 30,
    '#attributes' => $element['#attributes'] + array('data-date-popup' => json_encode($settings)),
    '#parents' => $parents,
    '#name' => array_shift($parents) . '[' . implode('][', $parents) . ']',
    '#ajax' => !empty($element['#ajax']) ? $element['#ajax'] : FALSE,
    '#attached' => array('library' => array(
      array('system', 'ui.datepicker')
    )),
  );
  $sub_element['#value'] = $sub_element['#default_value'];

  // Add an example placeholder to the date field.
  if (!isset($sub_element['#attributes']['placeholder'])) {
    $sub_element['#attributes']['placeholder'] = t('e.g. @date', array(
      '@date' => date_format_date(
      date_example_date(), 
      'custom', 
      _date_popup_date_format($element)
      ),
    )
    );
  }

  return $sub_element;
}