You are here

public function jCarouselFieldFormatter::viewElements in jQuery carousel 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/jCarouselFieldFormatter.php, line 157

Class

jCarouselFieldFormatter
Plugin annotation @FieldFormatter( id = "jquery_carousel_images", label = @Translation("jQuery Carousel"), field_types = { "image" } )

Namespace

Drupal\jquery_carousel\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $files = $this
    ->getEntitiesToView($items, $langcode);

  // Early opt-out if the field is empty.
  if (empty($files)) {
    return $elements;
  }
  $items = [];
  foreach ($files as $delta => $file) {
    $item = $file->_referringItem;
    $items[$delta] = $item;
  }
  $elements = [
    '#theme' => 'jquery_carousel_field_formatter',
    '#elements' => $items,
    '#items' => $files,
    '#display' => $this->viewMode,
    '#settings' => $this
      ->getSettings(),
  ];
  return $elements;
}