View source
<?php
namespace Drupal\field_timer\Plugin\Field\FieldFormatter;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class FieldTimerCountdownFormatterBase extends FieldTimerJsFormatterBase {
protected $time;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, TimeInterface $time) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$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('datetime.time'));
}
const LIBRARY_NAME = 'jquery.countdown';
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 settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['type'] = [
'#type' => 'select',
'#title' => $this
->t('Type'),
'#options' => $this
->typeOptions(),
'#default_value' => $this
->getSetting('type'),
];
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 preparePluginSettings(FieldItemInterface $item, $langcode) {
$settings = $this
->getSettings();
$timestamp = $this
->getTimestamp($item);
$type = $this
->getSetting('type');
if ($type == 'timer' || $type == 'auto' && $timestamp <= $this->time
->getRequestTime()) {
$settings['until'] = FALSE;
$settings['since'] = TRUE;
}
elseif ($type == 'countdown' || $type == 'auto' && $timestamp > $this->time
->getRequestTime()) {
$settings['until'] = TRUE;
$settings['since'] = FALSE;
}
unset($settings['type']);
return $settings;
}
protected function typeOptions() {
return [
static::TYPE_AUTO => $this
->t('Auto'),
static::TYPE_TIMER => $this
->t('Timer'),
static::TYPE_COUNTDOWN => $this
->t('Countdown'),
];
}
protected function getDocumentationLink(array $options = []) {
return Url::fromUri('http://keith-wood.name/countdownRef.html', $options)
->toString();
}
}