function date_parts_element in Date 6
Same name and namespace in other branches
- 5.2 date_api_elements.inc \date_parts_element()
- 6.2 date_api_elements.inc \date_parts_element()
- 7.3 date_api/date_api_elements.inc \date_parts_element()
- 7 date_api/date_api_elements.inc \date_parts_element()
- 7.2 date_api/date_api_elements.inc \date_parts_element()
Create 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:
object $date:
array $granularity:
string $format:
Return value
array the form array for the submitted date parts
1 call to date_parts_element()
- date_select_process in ./
date_api_elements.inc - Flexible date/time drop-down selector.
File
- ./
date_api_elements.inc, line 215 - Date API elements themes and validation. This file is only included during the edit process to reduce memory usage.
Code
function date_parts_element($element, $date, $format) {
$sub_element = array();
$granularity = date_format_order($format);
$order = array_flip($granularity);
$hours_format = strpos(strtolower($element['#date_format']), 'a') ? 'g' : 'G';
$month_function = strpos($element['#date_format'], 'F') ? 'date_month_names' : 'date_month_names_abbr';
$count = 0;
$increment = min(intval($element['#date_increment']), 1);
foreach ($granularity as $field) {
// Allow empty value as option if date is not required
// or if empty value was provided as a starting point.
$part_required = $element['#required'] && is_object($date) ? TRUE : FALSE;
$part_type = in_array($field, $element['#date_text_parts']) ? 'textfield' : 'select';
$sub_element[$field] = array(
'#weight' => $order[$field],
'#required' => $element['#required'],
'#attributes' => array(
'class' => (isset($element['#attributes']['class']) ? $element['#attributes']['class'] : '') . ' date-' . $field,
),
);
switch ($field) {
case 'year':
// Center the range around the current year, but expand it far
// enough so it will pick up the year value in the field in case
// the value in the field is outside the initial range.
$this_year = date_format(date_now(), 'Y');
$value_year = is_object($date) ? date_format($date, 'Y') : '';
$range = explode(':', $element['#date_year_range']);
$min_year = $this_year + $range[0];
$max_year = $this_year + $range[1];
if (!empty($value_year)) {
$min_year = min($value_year, $min_year);
$max_year = max($value_year, $max_year);
}
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'Y') : '';
$sub_element[$field]['#options'] = drupal_map_assoc(date_years($min_year, $max_year, $part_required));
break;
case 'month':
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'n') : '';
$sub_element[$field]['#options'] = $month_function($part_required);
break;
case 'day':
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'j') : '';
$sub_element[$field]['#options'] = drupal_map_assoc(date_days($part_required));
break;
case 'hour':
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, $hours_format) : '';
$sub_element[$field]['#options'] = drupal_map_assoc(date_hours($hours_format, $part_required));
$sub_element[$field]['#prefix'] = theme('date_part_hour_prefix', $element);
break;
case 'minute':
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'i') : '';
$sub_element[$field]['#options'] = drupal_map_assoc(date_minutes('i', $part_required, $element['#date_increment']));
$sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element);
break;
case 'second':
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 's') : '';
$sub_element[$field]['#options'] = drupal_map_assoc(date_seconds('s', $part_required, $element['#date_increment']));
$sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element);
break;
}
// Add handling for the date part label.
$label = theme('date_part_label_' . $field, $part_type, $element);
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;
if ($element['#date_label_position'] == 'within') {
$sub_element[$field]['#options'] = array(
'-' . $label => '-' . $label,
) + $sub_element[$field]['#options'];
if (empty($sub_element[$field]['#default_value'])) {
$sub_element[$field]['#default_value'] = '-' . $label;
}
}
elseif ($element['#date_label_position'] != 'none') {
$sub_element[$field]['#title'] = $label;
}
}
else {
$sub_element[$field]['#type'] = 'select';
$sub_element[$field]['#theme'] = 'date_select_element';
if ($element['#date_label_position'] == 'within') {
$sub_element[$field]['#options'] = array(
'' => '-' . $label,
) + $sub_element[$field]['#options'];
}
elseif ($element['#date_label_position'] != 'none') {
$sub_element[$field]['#title'] = $label;
}
}
}
if (($hours_format == 'g' || $hours_format == 'h') && date_has_time($granularity)) {
$sub_element['ampm'] = array(
'#type' => 'select',
'#default_value' => is_object($date) ? date_format($date, 'G') >= 12 ? 'pm' : 'am' : '',
'#options' => drupal_map_assoc(date_ampm()),
'#weight' => 8,
'#attributes' => array(
'class' => 'date-ampm',
),
);
if ($element['#date_label_position'] == 'within') {
$sub_element['ampm']['#options'] = array(
'' => '-' . theme('date_part_label_ampm', 'ampm', $element),
) + $sub_element['ampm']['#options'];
}
elseif ($element['#date_label_position'] != 'none') {
$sub_element['ampm']['#title'] = theme('date_part_label_ampm', 'ampm', $element);
}
}
return $sub_element;
}