3 namespace Drupal\datetime\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\Core\Form\FormStateInterface;
9 * Plugin implementation of the 'Default' formatter for 'datetime' fields.
12 * id = "datetime_default",
13 * label = @Translation("Default"),
19 class DateTimeDefaultFormatter extends DateTimeFormatterBase {
24 public static function defaultSettings() {
26 'format_type' => 'medium',
27 ] + parent::defaultSettings();
33 protected function formatDate($date) {
34 $format_type = $this->getSetting('format_type');
35 $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()->getName();
36 return $this->dateFormatter->format($date->getTimestamp(), $format_type, '', $timezone != '' ? $timezone : NULL);
42 public function settingsForm(array $form, FormStateInterface $form_state) {
43 $form = parent::settingsForm($form, $form_state);
45 $time = new DrupalDateTime();
46 $format_types = $this->dateFormatStorage->loadMultiple();
48 foreach ($format_types as $type => $type_info) {
49 $format = $this->dateFormatter->format($time->getTimestamp(), $type);
50 $options[$type] = $type_info->label() . ' (' . $format . ')';
53 $form['format_type'] = [
55 '#title' => t('Date format'),
56 '#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."),
57 '#options' => $options,
58 '#default_value' => $this->getSetting('format_type'),
67 public function settingsSummary() {
68 $summary = parent::settingsSummary();
70 $date = new DrupalDateTime();
71 $summary[] = t('Format: @display', ['@display' => $this->formatDate($date)]);