You are here

public function FieldTimerCountdownFormatter::viewElements in Field Timer 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/FieldTimerCountdownFormatter.php \Drupal\field_timer\Plugin\Field\FieldFormatter\FieldTimerCountdownFormatter::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 FieldTimerJsFormatterBase::viewElements

File

src/Plugin/Field/FieldFormatter/FieldTimerCountdownFormatter.php, line 88

Class

FieldTimerCountdownFormatter
Plugin implementation of the 'field_timer_countdown' formatter.

Namespace

Drupal\field_timer\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = parent::viewElements($items, $langcode);
  $language = $this
    ->getLanguage($langcode);
  if ($language != 'en') {
    $elements['#attached']['library'][] = 'field_timer/' . static::LIBRARY_NAME . '.' . $language;
  }
  $keys = $this
    ->getItemKeys($items);
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#markup' => '<span class="field-timer-jquery-countdown" data-field-timer-key="' . $keys[$delta] . '" data-timestamp="' . $this
        ->getTimestamp($item) . '"></span>',
    ];
  }
  return $elements;
}