You are here

public function DateTextWidget::settingsForm in Date 8

Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::settingsForm().

File

lib/Drupal/date/Plugin/field/widget/DateTextWidget.php, line 64
Definition of Drupal\date\Plugin\field\widget\DateTextWidget.

Class

DateTextWidget
Plugin implementation of the 'date' widget.

Namespace

Drupal\date\Plugin\field\widget

Code

public function settingsForm(array $form, array &$form_state) {
  $field = $this->field;
  $instance = $this->instance;
  $widget = $instance['widget'];
  $settings = $widget['settings'];
  if (empty($settings['date_date_format'])) {
    $settings['date_date_format'] = variable_get('date_format_html_date', 'Y-m-d') . ' ' . variable_get('date_format_html_time', 'H:i:s');
  }
  $element = array(
    '#element_validate' => array(
      'date_field_widget_settings_form_validate',
    ),
  );
  $options = array();
  $formats = date_datepicker_formats();
  $example_date = date_example_date();
  foreach ($formats as $f) {
    $options[$f] = $example_date
      ->format($f);
  }
  $element['date_date_format'] = array(
    '#type' => 'select',
    '#title' => t('Date entry format'),
    '#default_value' => $settings['date_date_format'],
    '#options' => $options,
    '#description' => t('Control the order and format of the options users see.'),
    '#weight' => 3,
    '#fieldset' => 'date_format',
  );
  $element['date_time_format'] = array(
    '#type' => 'select',
    '#title' => t('Time entry format'),
    '#default_value' => $settings['date_time_format'],
    '#options' => $options,
    '#description' => t('Control the order and format of the options users see.'),
    '#weight' => 3,
    '#fieldset' => 'date_format',
  );
  if (in_array($widget['type'], array(
    'date_select',
    'date_popup',
  ))) {
    $element['year_range'] = array(
      '#type' => 'date_year_range',
      '#default_value' => $settings['year_range'],
      '#fieldset' => 'date_format',
      '#weight' => 6,
    );
    $element['increment'] = array(
      '#type' => 'select',
      '#title' => t('Time increments'),
      '#default_value' => $settings['increment'],
      '#options' => array(
        1 => t('1 minute'),
        5 => t('5 minute'),
        10 => t('10 minute'),
        15 => t('15 minute'),
        30 => t('30 minute'),
      ),
      '#weight' => 7,
      '#fieldset' => 'date_format',
    );
  }
  else {
    $element['year_range'] = array(
      '#type' => 'hidden',
      '#value' => $settings['year_range'],
    );
    $element['increment'] = array(
      '#type' => 'hidden',
      '#value' => $settings['increment'],
    );
  }
  $element['label_position'] = array(
    '#type' => 'value',
    '#value' => $settings['label_position'],
  );
  $element['text_parts'] = array(
    '#type' => 'value',
    '#value' => $settings['text_parts'],
  );
  $element['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#fieldset' => 'date_format',
    '#weight' => 9,
  );
  if (in_array($widget['type'], array(
    'date_select',
  ))) {
    $options = array(
      'above' => t('Above'),
      'within' => t('Within'),
      'none' => t('None'),
    );
    $description = t("The location of date part labels, like 'Year', 'Month', or 'Day' . 'Above' displays the label as titles above each date part. 'Within' inserts the label as the first option in the select list and in blank textfields. 'None' doesn't label any of the date parts. Theme functions like 'date_part_label_year' and 'date_part_label_month' control label text.");
  }
  else {
    $options = array(
      'above' => t('Above'),
      'none' => t('None'),
    );
    $description = t("The location of date part labels, like 'Year', 'Month', or 'Day' . 'Above' displays the label as titles above each date part. 'None' doesn't label any of the date parts. Theme functions like 'date_part_label_year' and 'date_part_label_month' control label text.");
  }
  $element['advanced']['label_position'] = array(
    '#type' => 'radios',
    '#options' => $options,
    '#default_value' => $settings['label_position'],
    '#title' => t('Position of date part labels'),
    '#description' => $description,
  );
  $element['advanced']['text_parts'] = array(
    '#theme' => $widget['type'] == 'date_select' ? 'date_text_parts' : '',
  );
  $text_parts = (array) $settings['text_parts'];
  foreach (DateGranularity::granularityNames() as $key => $value) {
    if ($widget['type'] == 'date_select') {
      $element['advanced']['text_parts'][$key] = array(
        '#type' => 'radios',
        '#default_value' => in_array($key, $text_parts) ? 1 : 0,
        '#options' => array(
          0 => '',
          1 => '',
        ),
      );
    }
    else {
      $element['advanced']['text_parts'][$key] = array(
        '#type' => 'value',
        '#value' => (int) in_array($key, (array) $settings['text_parts']),
      );
    }
  }
  $context = array(
    'field' => $field,
    'instance' => $instance,
  );
  drupal_alter('date_field_widget_settings_form', $element, $context);
  return $element;
}