View source
<?php
namespace Drupal\duration_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\duration_field\Service\GranularityServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DurationTimeFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $granularityService;
public function __construct($plugin_id, $plugin_definition, $field_config, array $settings, $label, $view_mode, array $third_party_settings, GranularityServiceInterface $granularityService) {
parent::__construct($plugin_id, $plugin_definition, $field_config, $settings, $label, $view_mode, $third_party_settings);
$this->granularityService = $granularityService;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('duration_field.granularity.service'));
}
public function settingsSummary() {
$summary = [];
$summary[] = $this
->t('Displays the duration in the format: YY/MM/DD HH:MM:SS');
$summary[] = $this
->t('If only date components are part of the field granularity, time will not be shown');
$summary[] = $this
->t('If only time components are part of the field granularity, the date will not be shown.');
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
foreach ($items as $delta => $item) {
$element[$delta] = [
'#theme' => 'duration_field_duration_time',
'#item' => $item
->get('duration')
->getCastedValue(),
];
}
return $element;
}
}