DateTimePlainFormatter.php

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

Namespace

Drupal\datetime\Plugin\Field\FieldFormatter

File

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

View source
<?php

namespace Drupal\datetime\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;

/**
 * Plugin implementation of the 'Plain' formatter for 'datetime' fields.
 *
 * @FieldFormatter(
 *   id = "datetime_plain",
 *   label = @Translation("Plain"),
 *   field_types = {
 *     "datetime"
 *   }
 *)
 */
class DateTimePlainFormatter extends DateTimeFormatterBase {
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) {
        $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->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
        $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()
            ->getName();
        return $this->dateFormatter
            ->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL);
    }

}

Classes

Title Deprecated Summary
DateTimePlainFormatter Plugin implementation of the 'Plain' formatter for 'datetime' fields.

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