class DateTimeDefaultFormatter

Same name in other branches
  1. 9 core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeDefaultFormatter
  2. 8.9.x core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeDefaultFormatter
  3. 11.x core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeDefaultFormatter

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

Hierarchy

  • class \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeDefaultFormatter extends \Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeFormatterBase

Expanded class hierarchy of DateTimeDefaultFormatter

1 file declares its use of DateTimeDefaultFormatter
DateRangeDefaultFormatter.php in core/modules/datetime_range/src/Plugin/Field/FieldFormatter/DateRangeDefaultFormatter.php

File

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

Namespace

Drupal\datetime\Plugin\Field\FieldFormatter
View source
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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DateTimeDefaultFormatter::defaultSettings public static function Overrides DateTimeFormatterBase::defaultSettings 1
DateTimeDefaultFormatter::formatDate protected function Overrides DateTimeFormatterBase::formatDate
DateTimeDefaultFormatter::settingsForm public function Overrides DateTimeFormatterBase::settingsForm 1
DateTimeDefaultFormatter::settingsSummary public function Overrides DateTimeFormatterBase::settingsSummary 1
DateTimeFormatterBase::$dateFormatStorage protected property The date format entity storage.
DateTimeFormatterBase::$dateFormatter protected property The date formatter service.
DateTimeFormatterBase::buildDate protected function Creates a render array from a date object.
DateTimeFormatterBase::buildDateWithIsoAttribute protected function Creates a render array from a date object with ISO date attribute.
DateTimeFormatterBase::create public static function Overrides FormatterBase::create
DateTimeFormatterBase::getFormatSettings protected function Gets a settings array suitable for DrupalDateTime::format().
DateTimeFormatterBase::setTimeZone protected function Sets the proper time zone on a DrupalDateTime object for the current user.
DateTimeFormatterBase::viewElements public function Overrides FormatterInterface::viewElements 2
DateTimeFormatterBase::__construct public function Constructs a new DateTimeDefaultFormatter. Overrides FormatterBase::__construct
FormatterBase::$fieldDefinition protected property The field definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::getFieldSetting protected function Returns the value of a field setting.
FormatterBase::getFieldSettings protected function Returns the array of field settings.
FormatterBase::isApplicable public static function Overrides FormatterInterface::isApplicable 12
FormatterBase::prepareView public function Overrides FormatterInterface::prepareView 2
FormatterBase::view public function Overrides FormatterInterface::view 1
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 2
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::$thirdPartySettings protected property The plugin settings injected by third party modules.
PluginSettingsBase::calculateDependencies public function Overrides DependentPluginInterface::calculateDependencies 6
PluginSettingsBase::getSetting public function Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::getThirdPartyProviders public function Overrides ThirdPartySettingsInterface::getThirdPartyProviders
PluginSettingsBase::getThirdPartySetting public function Overrides ThirdPartySettingsInterface::getThirdPartySetting
PluginSettingsBase::getThirdPartySettings public function Overrides ThirdPartySettingsInterface::getThirdPartySettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::onDependencyRemoval public function Overrides PluginSettingsInterface::onDependencyRemoval 3
PluginSettingsBase::setSetting public function Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Overrides PluginSettingsInterface::setSettings
PluginSettingsBase::setThirdPartySetting public function Overrides ThirdPartySettingsInterface::setThirdPartySetting
PluginSettingsBase::unsetThirdPartySetting public function Overrides ThirdPartySettingsInterface::unsetThirdPartySetting

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