function date_parts_element in Date 7
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()
- 6 date_api_elements.inc \date_parts_element()
- 7.3 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_element_process in date_api/
date_api_elements.inc - Flexible date/time drop-down selector.
File
- date_api/
date_api_elements.inc, line 384 - 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) {
$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';
$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'] += array(
'date-' . $field,
) : array(
'date-' . $field,
),
),
);
switch ($field) {
case 'year':
$range = date_range_years($element['#date_year_range'], $date);
$min_year = $range[0];
$max_year = $range[1];
$sub_element[$field]['#default_value'] = is_object($date) ? $date
->format('Y') : '';
if ($part_type == 'select') {
$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('n') : '';
if ($part_type == 'select') {
$sub_element[$field]['#options'] = $month_function($part_required);
}
break;
case 'day':
$sub_element[$field]['#default_value'] = is_object($date) ? $date
->format('j') : '';
if ($part_type == 'select') {
$sub_element[$field]['#options'] = drupal_map_assoc(date_days($part_required));
}
break;
case 'hour':
$sub_element[$field]['#default_value'] = is_object($date) ? $date
->format($hours_format) : '';
if ($part_type == 'select') {
$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('i') : '';
if ($part_type == 'select') {
$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('s') : '';
if ($part_type == 'select') {
$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, array(
'part_type' => $part_type,
'element' => $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') {
if (!empty($sub_element[$field]['#options']) && is_array($sub_element[$field]['#options'])) {
$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;
}
}
}
// 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'])) {
$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)) {
$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' => array(
'date-ampm',
),
),
);
if ($element['#date_label_position'] == 'within') {
$sub_element['ampm']['#options'] = array(
'' => '-' . theme('date_part_label_ampm', array(
'part_type' => 'ampm',
'eleement' => $element,
)),
) + $sub_element['ampm']['#options'];
}
elseif ($element['#date_label_position'] != 'none') {
$sub_element['ampm']['#title'] = theme('date_part_label_ampm', array(
'part_type' => 'ampm',
'element' => $element,
));
}
}
return $sub_element;
}