You are here

public function JsTimerDefaultFormatter::viewElements in Javascript Timer 8

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 FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/JsTimerDefaultFormatter.php, line 40

Class

JsTimerDefaultFormatter
Plugin implementation of the 'JsTimer' formatter for 'datetime' fields.

Namespace

Drupal\jstimer\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  foreach ($items as $delta => $item) {
    $date = $item->date;
    $output = [];
    if (!empty($item->date)) {
      if ($this
        ->getFieldSetting('datetime_type') == 'date') {

        // A date without time will pick up the current time, use the default.
        datetime_date_default_time($date);
      }

      // $date is a DrupalDateTime object
      $args = $this
        ->dateToWidget($date
        ->getTimestamp(), "jst_timer", $this
        ->getSettings());
      $output = jst_timer_show($args['widget_args']);
    }
    $elements[$delta] = array(
      '#markup' => $output,
    );
  }
  return $elements;
}