You are here

function date_formatter_setup_form in Date 5

Same name and namespace in other branches
  1. 5.2 date/date_admin.inc \date_formatter_setup_form()
  2. 6 date/date_admin.inc \date_formatter_setup_form()

A form to create a date formatter option

1 call to date_formatter_setup_form()
date_field_settings_form in ./date_admin.inc

File

./date_admin.inc, line 296

Code

function date_formatter_setup_form($field, $delta) {
  switch ($delta) {
    case 1:
      $name = 'long';
      $label = t('Long');
      $default = variable_get('date_format_long', 'l, F j, Y - H:i');
      break;
    case 2:
      $name = 'medium';
      $label = t('Medium');
      $default = variable_get('date_format_medium', 'D, m/d/Y - H:i');
      break;
    case 3:
      $name = 'short';
      $label = t('Short');
      $default = variable_get('date_format_short', 'm/d/Y - H:i');
      break;
    default:
      $name = 'default';
      $label = t('Default');
      $default = variable_get('date_format_short', 'm/d/Y - H:i');
  }
  $append = $delta > 0 ? '_' . $name : '';
  $form = array(
    '#type' => 'fieldset',
    '#title' => $label,
  );
  $formats = array_merge(date_short_formats(), date_medium_formats(), date_long_formats());
  $options = array();

  // Get an example date and make sure the difference between
  // month and day and 12 and 24 hours will be clear.
  $now = time();
  if (date('m', $now) == date('d', $now)) {
    $now += 86400;
  }
  if (date('H', $now) == date('h', $now)) {
    $now += 43200;
  }

  // This allows us to store the timezone preference as a part of the date format string.
  // Full implementation that will get this working correctly will not come until
  // Version 5.2, but this way the information will not get lost until the upgrade.
  $offset = variable_get('date_default_timezone', 0);
  $timezone_name = variable_get('date_default_timezone_name', 'UTC');
  foreach ($formats as $format) {

    // Create an option that shows date only without time, along with the
    // default string which has both date and time.
    $no_time = date_limit_format($format, array(
      'M',
      'D',
      'Y',
    ));
    $zones = array(
      '',
      'O',
      'P',
      'e',
    );
    foreach ($zones as $zone) {
      $time_format = !empty($zone) ? $format . ' ' . $zone : $format;
      $options[$no_time] = date_format_date($no_time, $now, $offset, $timezone_name);
      $options[$time_format] = date_format_date($time_format, $now, $offset, $timezone_name);
    }
  }
  asort($options);
  $form['output_format_date' . $append] = array(
    '#type' => 'select',
    '#title' => t('Date display'),
    '#default_value' => $field['output_format_date' . $append] ? $field['output_format_date' . $append] : $default,
    '#options' => $options,
    '#multiple' => false,
  );
  $form['output_format_custom' . $append] = array(
    '#type' => 'textfield',
    '#title' => t('*Custom display format'),
    '#default_value' => $field['output_format_custom' . $append] ? $field['output_format_custom' . $append] : '',
  );
  return $form;
}