1 system.admin.inc system_configure_date_formats_form($form, &$form_state, $format = NULL)

Allow users to add additional date formats.

Parameters

array|null $format: (optional) When present, provides the format being edited, otherwise a new format is being created.

File

core/modules/system/system.admin.inc, line 2789
Admin page callbacks for the System module.

Code

function system_configure_date_formats_form($form, &$form_state, $format = NULL) {
  if ($format) {
    backdrop_set_title(t('Edit date format %format', array('%format' => $format['label'])), PASS_THROUGH);
  }
  else {
    backdrop_set_title(t('Add date format'));
  }
  $form_state['format'] = $format;

  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => t('Label'),
    '#maxlength' => 100,
    '#default_value' => empty($format['label']) ? '' : $format['label'],
    '#description' => t('A name for the new format'),
    '#placeholder' => t('e.g. My date format'),
  );

  $form['name'] = array(
    '#type' => 'machine_name',
    '#title' => t('Machine-readable name'),
    '#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
    '#disabled' => !empty($format),
    '#default_value' => !empty($format) ? $format['name'] : '',
    '#machine_name' => array(
      'exists' => 'system_date_format_load',
      'source' => array('label'),
    ),
  );

  $pattern = isset($form_state['values']['pattern']) ? $form_state['values']['pattern'] : (isset($format['pattern']) ? $format['pattern'] : '');
  $preview = !empty($pattern) ? t('Displayed as %date', array('%date' => format_date(REQUEST_TIME, 'custom', $pattern))) : '';

  $form['pattern'] = array(
    '#type' => 'textfield',
    '#title' => t('Format string'),
    '#maxlength' => 100,
    '#description' => t('A date format using PHP date and time codes. See the <a href="@url" target="_blank">PHP manual</a> for available options.', array('@url' => 'https://www.php.net/manual/datetime.format.php#refsect1-datetime.format-parameters')),
    '#placeholder' => t('e.g. F j Y'),
    '#default_value' => $pattern,
    '#field_suffix' => '<small class="pattern-preview">' . $preview . '</small>',
    '#ajax' => array(
      'callback' => 'system_date_time_lookup',
      'event' => 'keyup',
      'progress' => array('type' => 'none', 'message' => NULL),
      'disable' => FALSE,
    ),
    '#required' => TRUE,
    '#wrapper_attributes' => array(
      'id' => 'date-format-pattern',
    ),
  );

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => (!empty($format['name']) ? t('Save format') : t('Add format')),
  );

  return $form;
}