DateTimeCustomFormatter.php

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

Namespace

Drupal\datetime\Plugin\Field\FieldFormatter

File

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

View source
<?php

namespace Drupal\datetime\Plugin\Field\FieldFormatter;

use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;

/**
 * Plugin implementation of the 'Custom' formatter for 'datetime' fields.
 */
class DateTimeCustomFormatter extends DateTimeFormatterBase {
    
    /**
     * {@inheritdoc}
     */
    public static function defaultSettings() {
        return [
            'date_format' => DateTimeItemInterface::DATETIME_STORAGE_FORMAT,
        ] + parent::defaultSettings();
    }
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) {
        // @todo Evaluate removing this method in
        // https://www.drupal.org/node/2793143 to determine if the behavior and
        // markup in the base class implementation can be used instead.
        $elements = [];
        foreach ($items as $delta => $item) {
            if (!empty($item->date)) {
                
                /** @var \Drupal\Core\Datetime\DrupalDateTime $date */
                $date = $item->date;
                $elements[$delta] = $this->buildDate($date);
            }
        }
        return $elements;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function formatDate($date) {
        $format = $this->getSetting('date_format');
        $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()
            ->getName();
        return $this->dateFormatter
            ->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL);
    }
    
    /**
     * {@inheritdoc}
     */
    public function settingsForm(array $form, FormStateInterface $form_state) {
        $form = parent::settingsForm($form, $form_state);
        $form['date_format'] = [
            '#type' => 'textfield',
            '#title' => $this->t('Date/time 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('date_format'),
        ];
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function settingsSummary() {
        $summary = parent::settingsSummary();
        $date = new DrupalDateTime();
        $this->setTimeZone($date);
        $summary[] = $date->format($this->getSetting('date_format'), $this->getFormatSettings());
        return $summary;
    }

}

Classes

Title Deprecated Summary
DateTimeCustomFormatter Plugin implementation of the 'Custom' formatter for 'datetime' fields.

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