You are here

public function MysqlDate::render in Views Custom Table 8

Same name and namespace in other branches
  1. 9.0.x src/Plugin/views/field/MysqlDate.php \Drupal\view_custom_table\Plugin\views\field\MysqlDate::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

File

src/Plugin/views/field/MysqlDate.php, line 151

Class

MysqlDate
A handler to provide a field that is completely custom by the administrator.

Namespace

Drupal\view_custom_table\Plugin\views\field

Code

public function render(ResultRow $values) {
  $date_value = $this
    ->getValue($values);
  $value = strtotime($date_value);
  $format = $this->options['date_format'];
  $customDateFormat = [
    'custom',
    'raw time ago',
    'time ago',
    'raw time hence',
    'time hence',
    'raw time span',
    'time span',
    'raw time span',
    'inverse time span',
    'time span',
  ];
  if (in_array($format, $customDateFormat)) {
    $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 format_date($value, $format, $custom_format, $timezone, 'en');
        }
        return format_date($value, $format, $custom_format, $timezone);
      default:
        return format_date($value, $format, '', $timezone);
    }
  }
}