You are here

public function TimestampFormatter::settingsForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::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 FormatterBase::settingsForm

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php, line 102

Class

TimestampFormatter
Plugin implementation of the 'timestamp' formatter.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $elements = parent::settingsForm($form, $form_state);
  $date_formats = [];
  foreach ($this->dateFormatStorage
    ->loadMultiple() as $machine_name => $value) {
    $date_formats[$machine_name] = $this
      ->t('@name format: @date', [
      '@name' => $value
        ->label(),
      '@date' => $this->dateFormatter
        ->format(REQUEST_TIME, $machine_name),
    ]);
  }
  $date_formats['custom'] = $this
    ->t('Custom');
  $elements['date_format'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Date format'),
    '#options' => $date_formats,
    '#default_value' => $this
      ->getSetting('date_format') ?: 'medium',
  ];
  $elements['custom_date_format'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom date format'),
    '#description' => $this
      ->t('See <a href="https://www.php.net/manual/datetime.format.php#refsect1-datetime.format-parameters" target="_blank">the documentation for PHP date formats</a>.'),
    '#default_value' => $this
      ->getSetting('custom_date_format') ?: '',
  ];
  $elements['custom_date_format']['#states']['visible'][] = [
    ':input[name="fields[' . $this->fieldDefinition
      ->getName() . '][settings_edit_form][settings][date_format]"]' => [
      'value' => 'custom',
    ],
  ];
  $elements['timezone'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Time zone'),
    '#options' => [
      '' => $this
        ->t('- Default site/user time zone -'),
    ] + system_time_zones(FALSE, TRUE),
    '#default_value' => $this
      ->getSetting('timezone'),
  ];
  return $elements;
}