You are here

public function StoreDateTimeFormatter::settingsForm in Commerce Core 8.2

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 FormatterBase::settingsForm

File

modules/store/src/Plugin/Field/FieldFormatter/StoreDateTimeFormatter.php, line 107

Class

StoreDateTimeFormatter
Plugin implementation of the 'commerce_store_datetime' formatter.

Namespace

Drupal\commerce_store\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);
  $date = new DrupalDateTime('now', $this
    ->getTimezone());

  /** @var \Drupal\Core\Datetime\DateFormatInterface[] $date_formats */
  $date_formats = $this->dateFormatStorage
    ->loadMultiple();
  $options = [];
  foreach ($date_formats as $type => $date_format) {
    $example = $date
      ->format($date_format
      ->getPattern());
    $options[$type] = $date_format
      ->label() . ' (' . $example . ')';
  }
  $form['date_format'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Date format'),
    '#description' => $this
      ->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('date_format'),
  ];
  return $form;
}