DurationTimeFormatter.php in Duration Field 8
File
src/Plugin/Field/FieldFormatter/DurationTimeFormatter.php
View source
<?php
namespace Drupal\duration_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
class DurationTimeFormatter extends FormatterBase {
public function settingsSummary() {
$summary = [];
$summary[] = t('Displays the duration in the format @format', [
'@format' => $this
->getDisplayFormat(),
]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$granularity = $this
->getFieldSetting('granularity');
foreach ($items as $delta => $item) {
$duration = new \DateInterval($item->value);
$output = [];
if ($granularity['year'] || $granularity['month'] || $granularity['day']) {
$output[] = $duration
->format('%y') . '-' . $duration
->format('%m') . '-' . $duration
->format('%d');
}
if ($granularity['hour'] || $granularity['minute'] || $granularity['second']) {
$output[] = $duration
->format('%h') . ':' . $duration
->format('%I') . ':' . $duration
->format('%S');
}
$element[$delta] = [
'#markup' => implode(' ', $output),
];
}
return $element;
}
protected function getDisplayFormat() {
$granularity = $this
->getFieldSetting('granularity');
$parts = [];
if ($granularity['year'] || $granularity['month'] || $granularity['day']) {
$parts[] = 'YY-MM-DD';
}
if ($granularity['hour'] || $granularity['minute'] || $granularity['second']) {
$parts[] = 'HH:MM:SS';
}
return implode(' ', $parts);
}
}