You are here

class TimeInterval in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/src/Plugin/views/field/TimeInterval.php \Drupal\views\Plugin\views\field\TimeInterval
  2. 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\field
View 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

Namesort descending Modifiers Type Description Overrides
TimeInterval::$dateFormatter protected property The date formatter service.
TimeInterval::buildOptionsForm public function
TimeInterval::create public static function
TimeInterval::defineOptions protected function
TimeInterval::render public function
TimeInterval::__construct public function Constructs a TimeInterval plugin object.