DateTimeTimeAgoFormatter.php in Drupal 9
File
core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeTimeAgoFormatter.php
View source
<?php
namespace Drupal\datetime\Plugin\Field\FieldFormatter;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampAgoFormatter;
class DateTimeTimeAgoFormatter extends TimestampAgoFormatter {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$date = $item->date;
$output = [];
if (!empty($item->date)) {
$output = $this
->formatDate($date);
}
$elements[$delta] = $output;
}
return $elements;
}
protected function formatDate(DrupalDateTime $date) {
return parent::formatTimestamp($date
->getTimestamp());
}
}