DateTimeDefaultFormatter.php

Same filename in other branches
  1. 9 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php
  2. 8.9.x core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php
  3. 10 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php

Namespace

Drupal\datetime\Plugin\Field\FieldFormatter

File

core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php

View source
<?php

namespace Drupal\datetime\Plugin\Field\FieldFormatter;

use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Plugin implementation of the 'Default' formatter for 'datetime' fields.
 */
class DateTimeDefaultFormatter extends DateTimeFormatterBase {
    
    /**
     * {@inheritdoc}
     */
    public static function defaultSettings() {
        return [
            'format_type' => 'medium',
        ] + parent::defaultSettings();
    }
    
    /**
     * {@inheritdoc}
     */
    protected function formatDate($date) {
        $format_type = $this->getSetting('format_type');
        $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()
            ->getName();
        return $this->dateFormatter
            ->format($date->getTimestamp(), $format_type, '', $timezone != '' ? $timezone : NULL);
    }
    
    /**
     * {@inheritdoc}
     */
    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' => $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('format_type'),
        ];
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function settingsSummary() {
        $summary = parent::settingsSummary();
        $date = new DrupalDateTime();
        $summary[] = $this->t('Format: @display', [
            '@display' => $this->formatDate($date),
        ]);
        return $summary;
    }

}

Classes

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.