You are here

public function FieldTimerSimpleTextFormatter::viewElements in Field Timer 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Field/FieldFormatter/FieldTimerSimpleTextFormatter.php \Drupal\field_timer\Plugin\Field\FieldFormatter\FieldTimerSimpleTextFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides DateTimeTimeAgoFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/FieldTimerSimpleTextFormatter.php, line 81

Class

FieldTimerSimpleTextFormatter
Plugin implementation of the 'field_timer_simple_text' formatter.

Namespace

Drupal\field_timer\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = parent::viewElements($items, $langcode);
  $type = $this
    ->getSetting('type');
  foreach ($items as $delta => $item) {
    switch ($type) {
      case static::TYPE_TIMER:
        if ($item->date
          ->getTimestamp() >= $this->time
          ->getRequestTime()) {
          unset($elements[$delta]);
        }
        break;
      case static::TYPE_COUNTDOWN:
        if ($item->date
          ->getTimestamp() < $this->time
          ->getRequestTime()) {
          unset($elements[$delta]);
        }
        break;
    }
  }
  return $elements;
}