You are here

function date_combo_element_process in Date 7

Same name and namespace in other branches
  1. 8 date_elements.inc \date_combo_element_process()
  2. 7.3 date_elements.inc \date_combo_element_process()
  3. 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 188
Date forms and form themes and validation.

Code

function date_combo_element_process($element, &$form_state, $form) {
  if (isset($element['#access']) && empty($element['#access'])) {
    return;
  }
  $field_name = $element['#field_name'];
  $delta = $element['#delta'];
  $bundle = $element['#bundle'];
  $entity_type = $element['#entity_type'];
  $field = field_widget_field($element, $form_state);
  $instance = field_widget_instance($element, $form_state);
  $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';
  if ($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]);
  }
  $element[$from_field] = array(
    '#field' => $field,
    '#title' => t($instance['label']),
    '#weight' => $instance['widget']['weight'],
    '#required' => $instance['required'] && $delta == 0 ? 1 : 0,
    '#default_value' => isset($element['#default_value'][$from_field]) ? $element['#default_value'][$from_field] : '',
    '#field' => $field,
    '#delta' => $delta,
    '#date_timezone' => $element['#date_timezone'],
    '#date_format' => date_limit_format(date_input_format($element, $field, $instance), $field['settings']['granularity']),
    '#date_text_parts' => (array) $instance['widget']['settings']['text_parts'],
    '#date_increment' => $instance['widget']['settings']['increment'],
    '#date_year_range' => $instance['widget']['settings']['year_range'],
    '#date_label_position' => $instance['widget']['settings']['label_position'],
  );
  $description = !empty($instance['description']) ? t($instance['description']) : '';

  // Give this element the right type, using a Date API
  // or a Date Popup element type.
  switch ($instance['widget']['type']) {
    case 'date_select':
    case 'date_select_repeat':

      // From/to selectors with lots of parts will look better if displayed
      // on two rows instead of in a single row.
      if (!empty($field['settings']['todate']) && count($field['settings']['granularity']) > 3) {
        $element[$from_field]['#attributes'] = array(
          'class' => array(
            'date-clear',
          ),
        );
      }
      $element[$from_field]['#type'] = 'date_select';
      $element[$from_field]['#theme_wrappers'] = array(
        'date_select',
      );
      break;
    case 'date_popup':
    case 'date_popup_repeat':
      $element[$from_field]['#type'] = 'date_popup';
      $element[$from_field]['#theme_wrappers'] = array(
        'date_popup',
      );
      break;
    default:
      $element[$from_field]['#type'] = 'date_text';
      $element[$from_field]['#theme_wrappers'] = array(
        'date_text',
      );
      break;
  }

  // If this field uses the 'To', add matching element
  // for the 'To' date, and adapt titles to make it clear which
  // is the 'From' and which is the 'To' .
  if (!empty($field['settings']['todate'])) {
    $element['#date_float'] = TRUE;
    $element[$from_field]['#title'] = t('From date');
    $element[$to_field] = $element[$from_field];
    $element[$to_field]['#title'] = t('To date');
    $element[$to_field]['#default_value'] = isset($element['#default_value'][$to_field]) ? $element['#default_value'][$to_field] : '';
    $element[$to_field]['#required'] = FALSE;
    $element[$to_field]['#weight'] += 0.1;
    if ($instance['widget']['type'] == 'date_select') {
      $description .= ' ' . t("Empty 'To date' values will use the 'From date' values.");
    }
    $element['#fieldset_description'] = $description;
  }
  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 (!empty($field['cardinality']) && empty($field['settings']['repeat'])) {
    $element[$from_field]['#date_title'] = t('@field_name From date value #@delta', array(
      '@field_name' => $instance['label'],
      '@delta' => $delta + 1,
    ));
    if (!empty($field['settings']['todate'])) {
      $element[$to_field]['#date_title'] = t('@field_name To date value #@delta', array(
        '@field_name' => $instance['label'],
        '@delta' => $delta + 1,
      ));
    }
  }
  elseif (!empty($field['settings']['todate'])) {
    $element[$from_field]['#date_title'] = t('@field_name From date', array(
      '@field_name' => $instance['label'],
    ));
    $element[$to_field]['#date_title'] = t('@field_name To date', array(
      '@field_name' => $instance['label'],
    ));
  }
  else {
    $element[$from_field]['#date_title'] = $instance['label'];
  }

  // Make sure field info will be available to the validator which
  // does not get the values in $form.
  $form_state['#field_info'][$field['field_name']] = $field;
  return $element;
}