You are here

protected function TimeFieldFormatter::viewValue in Time Formatter 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldFormatter/TimeFieldFormatter.php \Drupal\time_formatter\Plugin\Field\FieldFormatter\TimeFieldFormatter::viewValue()

Generate the output appropriate for one field item.

Parameters

\Drupal\Core\Field\FieldItemInterface $item: One field item.

Return value

string The textual output generated.

1 call to TimeFieldFormatter::viewValue()
TimeFieldFormatter::viewElements in src/Plugin/Field/FieldFormatter/TimeFieldFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/TimeFieldFormatter.php, line 192

Class

TimeFieldFormatter
Plugin implementation of the 'time_field_formatter' formatter.

Namespace

Drupal\time_formatter\Plugin\Field\FieldFormatter

Code

protected function viewValue(FieldItemInterface $item) {
  $value = $item->value;
  if ($this
    ->getSetting('storage') == self::STORAGE_SECONDS) {
    $value *= 1000;
  }
  $value = round($value);
  $milliseconds = $value % 1000;
  $value = ($value - $milliseconds) / 1000;
  $seconds = $value % 60;
  $value = ($value - $seconds) / 60;
  if ($this
    ->getSetting('hours') == self::HOURS_NEVER) {
    $minutes = $value;
    $value = 0;
  }
  else {
    $minutes = $value % 60;
    $value = ($value - $minutes) / 60;
  }
  $include_hours = $value || $this
    ->getSetting('hours') == self::HOURS_ALWAYS;
  $return = 'N/A';
  switch ($this
    ->getSetting('display')) {
    case self::DISPLAY_HMSMS:
      $return = $include_hours ? "{$value}h " : '';
      $return .= "{$minutes}m {$seconds}s {$milliseconds}ms";
      break;
    case self::DISPLAY_HMS:
      $return = $include_hours ? "{$value}h " : '';
      $return .= "{$minutes}m {$seconds}s";
      break;
    case self::DISPLAY_NUMBERSMS:
      $format = $include_hours ? "{$value}:%02s:%02s.%03s" : '%s:%02s.%03s';
      $return = sprintf($format, $minutes, $seconds, $milliseconds);
      break;
    case self::DISPLAY_NUMBERS:
      $format = $include_hours ? "{$value}:%02s:%02s" : '%s:%02s';
      $return = sprintf($format, $minutes, $seconds, $milliseconds);
      break;
  }
  return $return;
}