You are here

function form_process_datelist in Date 8

Expands a date element into an array of individual elements (i.e. year, month, day). All form elements are designed to have sane defaults so any or all can be omitted.

Required settings: #default_value The default value should be expressed in ISO datetime format, using the format used by DrupalDateTime's __toString() function. Provide a date with or without a timzone name appended to it, adjusted to the proper local timezone, i.e. '2012-01-31 10:30:00 America/Chicago'. NOTE - Converting a date stored in the database from UTC to the local zone and converting it back to UTC before storing it is not handled here. This element simply explodes and displays the string date that is provided to it and converts the user input back into a string value on submission. Optional properties in the $form may include: #date_date_format A date format string that describes the sub-elements that should be displayed to the end user and their order. Punctuation and other values in the format string will be ignored. Defaults to the site 'short' date format. #date_year_range A description of the range of years to display, like '1900:2050', '-3:+3' or '2000:+3', where the first value describes the earliest year and the second the latest year in the range. A year in either position means that specific year. A +/- value describes a dynamic value that is that many years earlier or later than the current year at the time the form is displayed. Defaults to '1900:2050'. #date_increment The increment to use for minutes and seconds, i.e. '15' would show only :00, :15, :30 and :45. Defaults to 1 to show every minute and second. #date_calendar The calendar system to use for displaying possible date parts. Depends on the calendar plugins available on this site. Defaults to 'gregorian'.

Example: $form = array( '#type' => 'date', '#default_value' => '2000-01-01 00:00:00', '#date_input_format' => 'd.m.Y H:i', '#date_year_range' => '2010:2020', '#date_increment' => 15, );

1 string reference to 'form_process_datelist'
_date_api_element_info in date_api/date_api_elements.inc
Implements hook_element_info().

File

date_api/date_api_elements.inc, line 287
Date API elements themes and validation. This file is only included during the edit process to reduce memory usage.

Code

function form_process_datelist($element, &$form_state) {

  // Load translated date part labels from the appropriate calendar plugin.
  $calendar = system_calendar($element['#date_calendar']);

  // If empty, this will default to the current date.
  $date = new \Drupal\Core\Datetime\DrupalDateTime($element['#value']);
  $element['#tree'] = TRUE;
  $hours_format = strpos(strtolower($element['#date_date_format']), 'a') ? 'g' : 'G';
  $month_function = strpos($element['#date_date_format'], 'F') !== FALSE ? 'month_names' : 'month_names_abbr';

  // Determine the order of day, month, year in the site's chosen date format.
  $format = $element['#date_date_format'];
  $order = date_format_order($format);
  $has_time = FALSE;

  // Output multi-selector for date.
  foreach ($order as $part) {
    switch ($part) {
      case 'day':
        $options = $calendar
          ->days($element['#required']);
        $format = 'j';
        $title = t('Day');
        break;
      case 'month':
        $options = $calendar
          ->{$month_function}($element['#required']);
        $format = 'n';
        $title = t('Month');
        break;
      case 'year':
        $range = date_range_years($element['#date_year_range'], $date);
        $options = $calendar
          ->years($range[0], $range[1], $element['#required']);
        $format = 'Y';
        $title = t('Year');
        break;
      case 'hour':
        $format = $hours_format;
        $options = $calendar
          ->hours($hours_format, $element['#required']);
        $has_time = TRUE;
        $title = t('Hour');
        break;
      case 'minute':
        $format = 'i';
        $options = $calendar
          ->minutes('i', $element['#required'], $element['#date_increment']);
        $has_time = TRUE;
        $title = t('Minute');
        break;
      case 'second':
        $format = 's';
        $options = $calendar
          ->seconds('s', $element['#required'], $element['#date_increment']);
        $has_time = TRUE;
        $title = t('Second');
        break;
    }
    $default = !empty($element['#value'][$part]) ? $element['#value'][$part] : '';
    $value = $date instanceof \Drupal\Core\Datetime\DrupalDateTime && !$date
      ->hasErrors() ? intval($date
      ->format($format)) : $default;
    $element['#attributes']['title'] = $title;
    $element[$part] = array(
      '#type' => 'select',
      '#title' => $title,
      '#title_display' => 'invisible',
      '#value' => $value,
      '#attributes' => $element['#attributes'],
      '#options' => $options,
      '#required' => $element['#required'],
    );
  }

  // Add the am/pm element if the format requires it.
  if (($hours_format == 'g' || $hours_format == 'h') && $has_time) {
    $default = !empty($element['#value']['ampm']) ? $element['#value']['ampm'] : 'am';
    $value = $date instanceof \Drupal\Core\Datetime\DrupalDateTime && !$date
      ->hasErrors() ? $date
      ->format('G') >= 12 ? 'pm' : 'am' : $default;
    $element['ampm'] = array(
      '#type' => 'select',
      '#title' => t('AM/PM'),
      '#title_display' => 'invisible',
      '#theme' => 'date_select_element',
      '#default_value' => $value,
      '#options' => $calendar
        ->ampm($element['#required']),
      '#required' => $element['#required'],
      '#weight' => 10,
    );
  }
  return $element;
}