TimeInterval.php in Drupal 8
File
core/modules/views/src/Plugin/views/field/TimeInterval.php
View source
<?php
namespace Drupal\views\Plugin\views\field;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TimeInterval extends FieldPluginBase {
protected $dateFormatter;
public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter) {
$this->dateFormatter = $date_formatter;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('date.formatter'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['granularity'] = [
'default' => 2,
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['granularity'] = [
'#type' => 'textfield',
'#title' => $this
->t('Granularity'),
'#description' => $this
->t('How many different units to display in the string.'),
'#default_value' => $this->options['granularity'],
];
}
public function render(ResultRow $values) {
$value = $values->{$this->field_alias};
return $this->dateFormatter
->formatInterval($value, isset($this->options['granularity']) ? $this->options['granularity'] : 2);
}
}
Classes
Name |
Description |
TimeInterval |
A handler to provide proper displays for time intervals. |