function Date::render

Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/field/Date.php \Drupal\views\Plugin\views\field\Date::render()
  2. 10 core/modules/views/src/Plugin/views/field/Date.php \Drupal\views\Plugin\views\field\Date::render()
  3. 11.x core/modules/views/src/Plugin/views/field/Date.php \Drupal\views\Plugin\views\field\Date::render()

Overrides FieldPluginBase::render

1 call to Date::render()
LastTimestamp::render in core/modules/comment/src/Plugin/views/field/LastTimestamp.php
Renders the field.
1 method overrides Date::render()
LastTimestamp::render in core/modules/comment/src/Plugin/views/field/LastTimestamp.php
Renders the field.

File

core/modules/views/src/Plugin/views/field/Date.php, line 137

Class

Date
A handler to provide proper displays for dates.

Namespace

Drupal\views\Plugin\views\field

Code

public function render(ResultRow $values) {
    $value = $this->getValue($values);
    $format = $this->options['date_format'];
    if (in_array($format, [
        'custom',
        'raw time ago',
        'time ago',
        'raw time hence',
        'time hence',
        'raw time span',
        'time span',
        'raw time span',
        'inverse time span',
        'time span',
    ])) {
        $custom_format = $this->options['custom_date_format'];
    }
    if ($value) {
        $timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
        // Will be positive for a datetime in the past (ago), and negative for a
        // datetime in the future (hence).
        $time_diff = REQUEST_TIME - $value;
        switch ($format) {
            case 'raw time ago':
                return $this->dateFormatter
                    ->formatTimeDiffSince($value, [
                    'granularity' => is_numeric($custom_format) ? $custom_format : 2,
                ]);
            case 'time ago':
                return $this->t('%time ago', [
                    '%time' => $this->dateFormatter
                        ->formatTimeDiffSince($value, [
                        'granularity' => is_numeric($custom_format) ? $custom_format : 2,
                    ]),
                ]);
            case 'raw time hence':
                return $this->dateFormatter
                    ->formatTimeDiffUntil($value, [
                    'granularity' => is_numeric($custom_format) ? $custom_format : 2,
                ]);
            case 'time hence':
                return $this->t('%time hence', [
                    '%time' => $this->dateFormatter
                        ->formatTimeDiffUntil($value, [
                        'granularity' => is_numeric($custom_format) ? $custom_format : 2,
                    ]),
                ]);
            case 'raw time span':
                return ($time_diff < 0 ? '-' : '') . $this->dateFormatter
                    ->formatTimeDiffSince($value, [
                    'strict' => FALSE,
                    'granularity' => is_numeric($custom_format) ? $custom_format : 2,
                ]);
            case 'inverse time span':
                return ($time_diff > 0 ? '-' : '') . $this->dateFormatter
                    ->formatTimeDiffSince($value, [
                    'strict' => FALSE,
                    'granularity' => is_numeric($custom_format) ? $custom_format : 2,
                ]);
            case 'time span':
                $time = $this->dateFormatter
                    ->formatTimeDiffSince($value, [
                    'strict' => FALSE,
                    'granularity' => is_numeric($custom_format) ? $custom_format : 2,
                ]);
                return $time_diff < 0 ? $this->t('%time hence', [
                    '%time' => $time,
                ]) : $this->t('%time ago', [
                    '%time' => $time,
                ]);
            case 'custom':
                if ($custom_format == 'r') {
                    return $this->dateFormatter
                        ->format($value, $format, $custom_format, $timezone, 'en');
                }
                return $this->dateFormatter
                    ->format($value, $format, $custom_format, $timezone);
            default:
                return $this->dateFormatter
                    ->format($value, $format, '', $timezone);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.