function date_combo_element_process in Date 8
Same name and namespace in other branches
- 7.3 date_elements.inc \date_combo_element_process()
- 7 date_elements.inc \date_combo_element_process()
- 7.2 date_elements.inc \date_combo_element_process()
Process an individual date element.
1 string reference to 'date_combo_element_process'
- date_element_info in ./
date.module - Implements hook_element_info().
File
- ./
date_elements.inc, line 55 - Date forms and form themes and validation.
Code
function date_combo_element_process($element, &$form_state, $form) {
if (date_hidden_element($element)) {
// A hidden value for a new entity that had its end date set to blank
// will not get processed later to populate the end date, so set it here.
if (isset($element['#value']['value2']) && empty($element['#value']['value2'])) {
$element['#value']['value2'] = $element['#value']['value'];
}
return $element;
}
$field_name = $element['#field_name'];
$delta = $element['#delta'];
$bundle = $element['#bundle'];
$entity_type = $element['#entity_type'];
$langcode = $element['#language'];
$date_is_default = $element['#date_is_default'];
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
// Figure out how many items are in the form, including new ones added by ajax.
$field_state = field_form_get_state($element['#field_parents'], $field_name, $element['#language'], $form_state);
$items_count = $field_state['items_count'];
$columns = $element['#columns'];
if (isset($columns['rrule'])) {
unset($columns['rrule']);
}
$from_field = 'value';
$to_field = 'value2';
$tz_field = 'timezone';
$offset_field = 'offset';
$offset_field2 = 'offset2';
// Convert UTC dates to their local values in DATETIME format,
// and adjust the default values as specified in the field settings.
// It would seem to make sense to do this conversion when the data
// is loaded instead of when the form is created, but the loaded
// field data is cached and we can't cache dates that have been converted
// to the timezone of an individual user, so we cache the UTC values
// instead and do our conversion to local dates in the form and
// in the formatters.
$keys = date_available_values($field, $instance);
foreach ($keys as $key) {
if (!isset($element['#default_value'][$key])) {
$element['#default_value'][$key] = '';
}
$date = date_local_date($element['#default_value'], $element['#date_timezone'], $field, $instance, $key);
$element['#default_value'][$key] = $date instanceof DrupalDateTime ? date_format($date, DATE_FORMAT_DATETIME) : '';
}
// Blank out the end date for optional end dates that match the start date,
// except when this is a new node that has default values that should be honored.
if (!$date_is_default && $field['settings']['todate'] != 'required' && !empty($element['#default_value'][$to_field]) && $element['#default_value'][$to_field] == $element['#default_value'][$from_field]) {
unset($element['#default_value'][$to_field]);
}
$show_todate = !empty($form_state['values']['show_todate']) || !empty($element['#default_value'][$to_field]) || $field['settings']['todate'] == 'required';
$element['show_todate'] = array(
'#title' => t('Show End Date'),
'#type' => 'checkbox',
'#default_value' => $show_todate,
'#weight' => -20,
'#access' => $field['settings']['todate'] == 'optional',
'#prefix' => '<div class="date-float">',
'#suffix' => '</div>',
);
$parents = $element['#parents'];
$first_parent = array_shift($parents);
$show_id = $first_parent . '[' . implode('][', $parents) . '][show_todate]';
$element[$from_field] = array(
'#field' => $field,
'#instance' => $instance,
'#weight' => $instance['widget']['weight'],
'#required' => $instance['required'] && $delta == 0 ? 1 : 0,
'#default_value' => isset($element['#default_value'][$from_field]) ? $element['#default_value'][$from_field] : '',
'#delta' => $delta,
'#date_timezone' => $element['#date_timezone'],
'#date_date_format' => date_get_format($instance, 'date'),
'#date_increment' => $instance['widget']['settings']['increment'],
'#date_year_range' => $instance['widget']['settings']['year_range'],
'#date_label_position' => $instance['widget']['settings']['label_position'],
'#date_time_callbacks' => array(
'date_all_day_toggle_callback',
),
);
$element[$from_field]['all_day'] = array(
'#title' => t('All Day'),
'#type' => !empty($instance['settings']['all_day_toggle']) ? 'checkbox' : 'hidden',
'#default_value' => isset($items[$delta][$from_field]['all_day']) ? $items[$delta][$from_field]['all_day'] : NULL,
);
$description = !empty($instance['description']) ? t($instance['description']) : '';
// Give this element the right type, using a Date API
// or a Date Popup element type.
//$element[$from_field]['#attributes'] = array('class' => array('date-clear'));
//$element[$from_field]['#wrapper_attributes'] = array('class' => array());
//$element[$from_field]['#wrapper_attributes']['class'][] = 'date-no-float';
switch ($instance['widget']['type']) {
case 'date_select':
$element[$from_field]['#type'] = 'datelist';
$element[$from_field]['#date_text_parts'] = (array) $instance['widget']['settings']['text_parts'];
//$element[$from_field]['#theme_wrappers'] = array('datelist');
$element['#attached']['js'][] = drupal_get_path('module', 'date') . '/date.js';
$element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
break;
case 'date_popup':
$element[$from_field]['#type'] = 'datetime';
$element[$from_field]['#date_time_format'] = date_get_format($instance, 'time');
$element[$from_field]['#date_date_element'] = 'date';
$element[$from_field]['#date_time_element'] = 'time';
$element[$from_field]['#date_date_callbacks'] = array(
'datetime_jquery_datepicker',
);
$element[$from_field]['#date_time_callbacks'] = array(
'date_all_day_toggle_callback',
);
//$element[$from_field]['#theme_wrappers'] = array('datetime');
//$element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
break;
}
// If this field uses the 'End', add matching element
// for the 'End' date, and adapt titles to make it clear which
// is the 'Start' and which is the 'End' .
if (!empty($field['settings']['todate'])) {
$element[$from_field]['#title'] = '';
$element[$to_field] = $element[$from_field];
$element[$to_field]['#title'] = t('to:');
//$element[$from_field]['#wrapper_attributes']['class'][] = 'start-date-wrapper';
//$element[$to_field]['#wrapper_attributes']['class'][] = 'end-date-wrapper';
$element[$to_field]['#default_value'] = isset($element['#default_value'][$to_field]) ? $element['#default_value'][$to_field] : '';
$element[$to_field]['#required'] = $element[$from_field]['#required'] && $field['settings']['todate'] == 'required';
$element[$to_field]['#weight'] += 0.2;
$element[$to_field]['#prefix'] = '';
// Users with JS enabled will never see initially blank values for the end
// date (see Drupal.date.EndDateHandler()), so hide the message for them.
$description .= '<span class="js-hide"> ' . t("Empty 'End date' values will use the 'Start date' values.") . '</span>';
$element['#fieldset_description'] = $description;
if ($field['settings']['todate'] == 'optional') {
$element[$to_field]['#states'] = array(
'visible' => array(
'input[name="' . $show_id . '"]' => array(
'checked' => TRUE,
),
),
);
}
}
else {
$element[$from_field]['#description'] = $description;
}
// Create label for error messages that make sense in multiple values
// and when the title field is left blank.
if ($field['cardinality'] != 1 && $items_count > 1) {
$element[$from_field]['#date_title'] = t('@field_name Start date value #@delta', array(
'@field_name' => $instance['label'],
'@delta' => $delta + 1,
));
if (!empty($field['settings']['todate'])) {
$element[$to_field]['#date_title'] = t('@field_name End date value #@delta', array(
'@field_name' => $instance['label'],
'@delta' => $delta + 1,
));
}
}
elseif (!empty($field['settings']['todate'])) {
$element[$from_field]['#date_title'] = t('@field_name Start date', array(
'@field_name' => $instance['label'],
));
$element[$to_field]['#date_title'] = t('@field_name End date', array(
'@field_name' => $instance['label'],
));
}
else {
$element[$from_field]['#date_title'] = $instance['label'];
}
$context = array(
'field' => $field,
'instance' => $instance,
'form' => $form,
);
drupal_alter('date_combo_process', $element, $form_state, $context);
return $element;
}