You are here

public function DateTimeDefaultFormatter::settingsForm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeDefaultFormatter::settingsForm()
  2. 9 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeDefaultFormatter::settingsForm()

Returns a form to configure settings for the formatter.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the formatter. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form elements for the formatter settings.

Overrides DateTimeFormatterBase::settingsForm

1 method overrides DateTimeDefaultFormatter::settingsForm()
DateRangeDefaultFormatter::settingsForm in core/modules/datetime_range/src/Plugin/Field/FieldFormatter/DateRangeDefaultFormatter.php
Returns a form to configure settings for the formatter.

File

core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php, line 42

Class

DateTimeDefaultFormatter
Plugin implementation of the 'Default' formatter for 'datetime' fields.

Namespace

Drupal\datetime\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);
  $time = new DrupalDateTime();
  $format_types = $this->dateFormatStorage
    ->loadMultiple();
  $options = [];
  foreach ($format_types as $type => $type_info) {
    $format = $this->dateFormatter
      ->format($time
      ->getTimestamp(), $type);
    $options[$type] = $type_info
      ->label() . ' (' . $format . ')';
  }
  $form['format_type'] = [
    '#type' => 'select',
    '#title' => t('Date format'),
    '#description' => t("Choose a format for displaying the date. Be sure to set a format appropriate for the field, i.e. omitting time for a field that only has a date."),
    '#options' => $options,
    '#default_value' => $this
      ->getSetting('format_type'),
  ];
  return $form;
}