1 date.elements.inc date_combo_element_process($element, &$form_state, $form)

Process an individual date element.

File

core/modules/date/date.elements.inc, line 1417
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;
  }

  $delta = $element['#delta'];
  $date_is_default = $element['#date_is_default'];

  $field = field_widget_field($element, $form_state);
  $instance = field_widget_instance($element, $form_state);

  $from_field = 'value';
  $to_field = 'value2';

  // 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.
  $process = date_process_values($field, $instance);
  if (empty($element['#default_value'])) {
    $element['#default_value'] = array();
  }
  foreach ($process as $processed) {
    if (!isset($element['#default_value'][$processed])) {
      $element['#default_value'][$processed] = '';
    }
    $date = date_local_date($element['#default_value'], $element['#date_timezone'], $field, $instance, $processed);
    $element['#default_value'][$processed] = is_object($date) ? 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,
    '#attributes' => array('data-toggle-todate' => TRUE),
    '#access' => $field['settings']['todate'] == 'optional',
  );

  $element[$from_field] = array(
    '#field' => $field,
    '#instance' => $instance,
    '#weight' => $instance['widget']['weight'],
    '#required' => ($element['#required'] && $delta == 0) ? 1 : 0,
    '#default_value' => isset($element['#default_value'][$from_field]) ? $element['#default_value'][$from_field] : '',
    '#delta' => $delta,
    '#date_title' => $instance['label'],
    '#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($element['#description']) ? t($element['#description']) : '';
  unset($element['#description']);

  switch ($instance['widget']['type']) {
    case 'date_select':
      $element[$from_field]['#type'] = 'date_select';
      $element[$from_field]['#theme_wrappers'] = array('date_select');
      $element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
      break;

    case 'date_popup':
      $element[$from_field]['#type'] = 'date_popup';
      $element[$from_field]['#theme_wrappers'] = array('date_popup');
      $element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
      break;

    default:
      $element[$from_field]['#type'] = 'date_text';
      $element[$from_field]['#theme_wrappers'] = array('date_text');
      $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[$to_field] = $element[$from_field];
    $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'] += .2;
    $element[$to_field]['#prefix'] = '';
    $element[$to_field]['#date_title'] = $instance['label'];
    $element['#attached']['js'][] = backdrop_get_path('module', 'date') . '/js/date-range.js';
  }
  else {
    $element[$from_field]['#description'] = $description;
  }

  // Set labels for each field in a multiple value widget.
  if ($field['cardinality'] != 1) {
    $element[$from_field]['#title'] = t('Date');
    $element[$from_field]['#date_title_printed'] = TRUE;
    if (!empty($field['settings']['todate'])) {
      $element[$from_field]['#title'] = t('Start date');
      $element[$to_field]['#title'] = t('End date');
      $element[$to_field]['#date_title_printed'] = TRUE;
    }
  }
  // Single value with from and to dates.
  elseif (!empty($field['settings']['todate'])) {
    $element[$from_field]['#title'] = t('@field_name start date', array('@field_name' => $instance['label']));
    $element[$from_field]['#date_title_printed'] = $element['#date_title_printed'];
    $element[$to_field]['#title'] = t('@field_name end date', array('@field_name' => $instance['label']));
    $element[$to_field]['#date_title_printed'] = $element['#date_title_printed'];
  }
  // Single value with just a single date.
  else {
    $element[$from_field]['#title'] = check_plain($instance['label']);
    $element[$from_field]['#date_title_printed'] = $element['#date_title_printed'];
  }

  $context = array(
    'field' => $field,
    'instance' => $instance,
    'form' => $form,
  );
  backdrop_alter('date_combo_process', $element, $form_state, $context);

  return $element;
}