1 date.elements.inc date_parts_element($element, $date, $format)

Creates form elements for one or more date parts.

Get the order of date elements from the provided format. If the format order omits any date parts in the granularity, alter the granularity array to match the format, then flip the $order array to get the position for each element. Then iterate through the elements and create a sub-form for each part.

Parameters

array $element: The date element.

object $date: The date object.

string $format: A date format string.

Return value

array: The form array for the submitted date parts.

File

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

Code

function date_parts_element($element, $date, $format) {
  $granularity = date_format_order($format);
  $sub_element = array('#granularity' => $granularity);
  $order = array_flip($granularity);

  $hours_format = strpos(strtolower($element['#date_format']), 'a') ? 'g' : 'G';
  $month_function = strpos($element['#date_format'], 'F') !== FALSE ? 'date_month_names' : 'date_month_names_abbr';

  // Allow empty value as option if date is not required or there is no date.
  $part_required = (bool) $element['#required'] && is_object($date);
  foreach ($granularity as $field) {
    $part_type = in_array($field, $element['#date_text_parts']) ? 'textfield' : 'select';
    $sub_element[$field] = array(
      '#weight' => $order[$field],
      '#required' => $part_required,
      '#attributes' => array('class' => isset($element['#attributes']['class']) ? $element['#attributes']['class'] += array('date-' . $field) : array('date-' . $field)),
      '#ajax' => !empty($element['#ajax']) ? $element['#ajax'] : FALSE,
    );
    switch ($field) {
      case 'year':
        $range = date_range_years($element['#date_year_range'], $date);
        $start_year = $range[0];
        $end_year = $range[1];

        $sub_element[$field]['#title'] = t('Year');
        $sub_element[$field]['#default_value'] = date_is_date($date) ? $date->format('Y') : '';
        if ($part_type == 'select') {
          $sub_element[$field]['#options'] = backdrop_map_assoc(date_years($start_year, $end_year, $part_required));
        }
        break;

      case 'month':
        $sub_element[$field]['#title'] = t('Month');
        $sub_element[$field]['#default_value'] = date_is_date($date) ? $date->format('n') : '';
        if ($part_type == 'select') {
          $sub_element[$field]['#options'] = $month_function($part_required);
        }
        break;

      case 'day':
        $sub_element[$field]['#title'] = t('Day');
        $sub_element[$field]['#default_value'] = date_is_date($date) ? $date->format('j') : '';
        if ($part_type == 'select') {
          $sub_element[$field]['#options'] = backdrop_map_assoc(date_days($part_required));
        }
        break;

      case 'hour':
        $sub_element[$field]['#title'] = t('Hour');
        $sub_element[$field]['#default_value'] = date_is_date($date) ? $date->format($hours_format) : '';
        if ($part_type == 'select') {
          $sub_element[$field]['#options'] = backdrop_map_assoc(date_hours($hours_format, $part_required));
        }
        if ($element['#date_label_position'] != 'above') {
          $sub_element[$field]['#prefix'] = '<span class="date-spacer date-parts-spacer">-</span>';
        }
        break;

      case 'minute':
        $sub_element[$field]['#title'] = t('Minute');
        $sub_element[$field]['#default_value'] = date_is_date($date) ? $date->format('i') : '';
        if ($part_type == 'select') {
          $sub_element[$field]['#options'] = backdrop_map_assoc(date_minutes('i', $part_required, $element['#date_increment']));
        }
        if ($element['#date_label_position'] != 'above') {
          $sub_element[$field]['#prefix'] = '<span class="date-spacer date-time-spacer">:</span>';
        }
        break;

      case 'second':
        $sub_element[$field]['#title'] = t('Second');
        $sub_element[$field]['#default_value'] = date_is_date($date) ? $date->format('s') : '';
        if ($part_type == 'select') {
          $sub_element[$field]['#options'] = backdrop_map_assoc(date_seconds('s', $part_required, $element['#date_increment']));
        }
        if ($element['#date_label_position'] != 'above') {
          $sub_element[$field]['#prefix'] = '<span class="date-spacer">:</span>';
        }
        break;
    }

    // Add handling for the date part label.
    $label = $sub_element[$field]['#title'];
    if (in_array($field, $element['#date_text_parts'])) {
      $sub_element[$field]['#type'] = 'textfield';
      $sub_element[$field]['#theme'] = 'date_textfield_element';
      $sub_element[$field]['#size'] = 7;
      $sub_element[$field]['#title_display'] = in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before';
      if ($element['#date_label_position'] == 'within') {
        if (!empty($sub_element[$field]['#options']) && is_array($sub_element[$field]['#options'])) {
          $sub_element[$field]['#options'] = array('' => $label) + $sub_element[$field]['#options'];
        }
      }
    }
    else {
      $sub_element[$field]['#type'] = 'select';
      $sub_element[$field]['#theme'] = 'date_select_element';
      $sub_element[$field]['#title_display'] = in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before';
      if ($element['#date_label_position'] == 'within') {
        $sub_element[$field]['#options'] = array('' => $label) + $sub_element[$field]['#options'];
      }
    }

    // Views exposed filters are treated as submitted even if not,
    // so force the #default value in that case. Make sure we set
    // a default that is in the option list.
    if (!empty($element['#force_value']) && isset($sub_element[$field]['#options'])) {
      $options = $sub_element[$field]['#options'];
      $default = !empty($sub_element[$field]['#default_value']) ? $sub_element[$field]['#default_value'] : array_shift($options);
      $sub_element[$field]['#value'] = $default;
    }
  }

  if (($hours_format == 'g' || $hours_format == 'h') && date_has_time($granularity)) {
    $label = '&nbsp;';
    $sub_element['ampm'] = array(
      '#type' => 'select',
      '#theme' => 'date_select_element',
      '#title' => $label,
      '#title_display' => in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before',
      '#default_value' => is_object($date) ? (date_format($date, 'G') >= 12 ? 'pm' : 'am') : '',
      '#options' => backdrop_map_assoc(date_ampm($part_required)),
      '#required' => $part_required,
      '#weight' => 8,
      '#attributes' => array('class' => array('date-ampm')),
    );
  }

  return $sub_element;
}