View source
<?php
namespace Drupal\field_timer\Plugin\Field\FieldFormatter;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeTimeAgoFormatter;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class FieldTimerSimpleTextFormatter extends DateTimeTimeAgoFormatter {
protected $time;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, DateFormatterInterface $date_formatter, Request $request, TimeInterface $time) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $date_formatter, $request);
$this->time = $time;
}
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('date.formatter'), $container
->get('request_stack')
->getCurrentRequest(), $container
->get('datetime.time'));
}
const TYPE_AUTO = 'auto', TYPE_TIMER = 'timer', TYPE_COUNTDOWN = 'countdown';
public static function defaultSettings() {
$settings = [
'type' => static::TYPE_AUTO,
] + parent::defaultSettings();
return $settings;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = parent::viewElements($items, $langcode);
$type = $this
->getSetting('type');
foreach ($items as $delta => $item) {
switch ($type) {
case static::TYPE_TIMER:
if ($item->date
->getTimestamp() >= $this->time
->getRequestTime()) {
unset($elements[$delta]);
}
break;
case static::TYPE_COUNTDOWN:
if ($item->date
->getTimestamp() < $this->time
->getRequestTime()) {
unset($elements[$delta]);
}
break;
}
}
return $elements;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['type'] = [
'#type' => 'select',
'#title' => $this
->t('Type'),
'#default_value' => $this
->getSetting('type'),
'#options' => $this
->typeOptions(),
'#description' => $this
->t('Switch timer/countdown automatically or disable it.'),
];
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$type = $this
->getSetting('type');
$summary[] = $this
->t('Type: @type', [
'@type' => $this
->typeOptions()[$type],
]);
return $summary;
}
protected function typeOptions() {
return [
static::TYPE_AUTO => $this
->t('Auto'),
static::TYPE_TIMER => $this
->t('Timer'),
static::TYPE_COUNTDOWN => $this
->t('Countdown'),
];
}
}