class TimeInterval in Drupal 10
Same name and namespace in other branches
- 8 core/modules/views/src/Plugin/views/field/TimeInterval.php \Drupal\views\Plugin\views\field\TimeInterval
- 9 core/modules/views/src/Plugin/views/field/TimeInterval.php \Drupal\views\Plugin\views\field\TimeInterval
A handler to provide proper displays for time intervals.
Plugin annotation
@ViewsField("time_interval");
Hierarchy
- class \Drupal\views\Plugin\views\field\TimeInterval extends \Drupal\views\Plugin\views\field\FieldPluginBase
Expanded class hierarchy of TimeInterval
File
- core/
modules/ views/ src/ Plugin/ views/ field/ TimeInterval.php, line 17
Namespace
Drupal\views\Plugin\views\fieldView source
class TimeInterval extends FieldPluginBase {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a TimeInterval plugin object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter) {
$this->dateFormatter = $date_formatter;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('date.formatter'));
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['granularity'] = [
'default' => 2,
];
return $options;
}
/**
* {@inheritdoc}
*/
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'],
];
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $values->{$this->field_alias};
return $this->dateFormatter
->formatInterval((int) $value, $this->options['granularity'] ?? 2);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TimeInterval:: |
protected | property | The date formatter service. | |
TimeInterval:: |
public | function | ||
TimeInterval:: |
public static | function | ||
TimeInterval:: |
protected | function | ||
TimeInterval:: |
public | function | ||
TimeInterval:: |
public | function | Constructs a TimeInterval plugin object. |