You are here

function date_combo_element_process in Date 7.3

Same name and namespace in other branches
  1. 8 date_elements.inc \date_combo_element_process()
  2. 7 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 258
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.
  $process = date_process_values($field, $instance);
  foreach ($process as $processed) {
    if (!isset($element['#default_value'][$processed])) {
      if (empty($element['#default_value']) || !is_array($element['#default_value'])) {
        $element['#default_value'] = array();
      }
      $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' && is_array($element['#default_value']) && !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' => $element['#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_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'],
  );

  // Date repeat is a multiple value field. So the description is removed from
  // the single element earlier. Let's get it back.
  if (isset($element['show_repeat_settings']) && !empty($element['value']['#instance']['description'])) {
    $element['#description'] = $element['value']['#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'] = 'date_select';
      $element[$from_field]['#theme_wrappers'] = array(
        'date_select',
      );
      $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'] = 'date_popup';
      $element[$from_field]['#theme_wrappers'] = array(
        'date_popup',
      );

      // Disable autocomplete in browsers.
      // @see https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
      $element[$from_field]['#attributes']['autocomplete'] = 'off';
      $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;
  }

  // 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]['#title_display'] = 'none';
    $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.
    $element['#description'] .= '<span class="js-hide"> ' . t("Empty 'End date' values will use the 'Start date' values.") . '</span>';
    if ($field['settings']['todate'] == 'optional') {
      $element[$to_field]['#states'] = array(
        'visible' => array(
          'input[name="' . $show_id . '"]' => array(
            'checked' => TRUE,
          ),
        ),
      );
    }
  }

  // Create label for error messages that make sense in multiple values
  // and when the title field is left blank.
  if ($field['cardinality'] != 1 && empty($field['settings']['repeat'])) {
    $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'] = t('@field_name', array(
      '@field_name' => $instance['label'],
    ));
  }

  // Make changes if instance is set to be rendered as a regular field.
  if (!empty($instance['widget']['settings']['no_fieldset'])) {
    unset($element[$from_field]['#description']);
    if (!empty($field['settings']['todate']) && isset($element['#description'])) {
      $element['#description'] .= '<span class="js-hide"> ' . t("Empty 'End date' values will use the 'Start date' values.") . '</span>';
    }
  }
  $context = array(
    'field' => $field,
    'instance' => $instance,
    'form' => $form,
  );

  // Trigger hook_date_combo_process_alter().
  drupal_alter('date_combo_process', $element, $form_state, $context);
  return $element;
}