You are here

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

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

Class

FieldTimerCountyFormatter
Plugin implementation of the 'field_timer_county' formatter.

Namespace

Drupal\field_timer\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = parent::viewElements($items, $langcode);
  $keys = $this
    ->getItemKeys($items);
  $attributes = [
    'class' => [
      'field-timer-county',
    ],
  ];
  $background = $this
    ->getSetting('background');
  if (!empty($background)) {
    $attributes['style'] = 'background:' . $background . ';';
  }
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#theme' => 'container',
      '#attributes' => $attributes,
      '#children' => [
        '#markup' => '<div data-field-timer-key="' . $keys[$delta] . '"  data-timestamp="' . $this
          ->getTimestamp($item) . '"></div>',
      ],
    ];
  }
  return $elements;
}