You are here

public function Date::render in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Plugin/views/field/Date.php \Drupal\views\Plugin\views\field\Date::render()

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

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 142
Contains \Drupal\views\Plugin\views\field\Date.

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, array(
    '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;
    $time_diff = REQUEST_TIME - $value;

    // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence)
    switch ($format) {
      case 'raw time ago':
        return $this->dateFormatter
          ->formatTimeDiffSince($value, array(
          'granularity' => is_numeric($custom_format) ? $custom_format : 2,
        ));
      case 'time ago':
        return $this
          ->t('%time ago', array(
          '%time' => $this->dateFormatter
            ->formatTimeDiffSince($value, array(
            'granularity' => is_numeric($custom_format) ? $custom_format : 2,
          )),
        ));
      case 'raw time hence':
        return $this->dateFormatter
          ->formatTimeDiffUntil($value, array(
          'granularity' => is_numeric($custom_format) ? $custom_format : 2,
        ));
      case 'time hence':
        return $this
          ->t('%time hence', array(
          '%time' => $this->dateFormatter
            ->formatTimeDiffUntil($value, array(
            'granularity' => is_numeric($custom_format) ? $custom_format : 2,
          )),
        ));
      case 'raw time span':
        return ($time_diff < 0 ? '-' : '') . $this->dateFormatter
          ->formatTimeDiffSince($value, array(
          'strict' => FALSE,
          'granularity' => is_numeric($custom_format) ? $custom_format : 2,
        ));
      case 'inverse time span':
        return ($time_diff > 0 ? '-' : '') . $this->dateFormatter
          ->formatTimeDiffSince($value, array(
          'strict' => FALSE,
          'granularity' => is_numeric($custom_format) ? $custom_format : 2,
        ));
      case 'time span':
        $time = $this->dateFormatter
          ->formatTimeDiffSince($value, array(
          'strict' => FALSE,
          'granularity' => is_numeric($custom_format) ? $custom_format : 2,
        ));
        return $time_diff < 0 ? $this
          ->t('%time hence', array(
          '%time' => $time,
        )) : $this
          ->t('%time ago', array(
          '%time' => $time,
        ));
      case 'custom':
        if ($custom_format == 'r') {
          return format_date($value, $format, $custom_format, $timezone, 'en');
        }
        return format_date($value, $format, $custom_format, $timezone);
      default:
        return format_date($value, $format, '', $timezone);
    }
  }
}