You are here

public function GalleryFormatterFormatter::viewElements in Gallery formatter 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 ResponsiveImageFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/GalleryFormatterFormatter.php, line 124

Class

GalleryFormatterFormatter
Plugin for galleryformatter.

Namespace

Drupal\galleryformatter\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $thumb_elements = [];
  $elements = parent::viewElements($items, $langcode);
  foreach ($elements as $key => $item) {
    $elements[$key]['#responsive_image_style_id'] = $this->responsiveImageStyleStorage
      ->load($this
      ->getSetting('slide_style'))
      ->id();
  }

  // Duplicate each element and set it a thumb_style image style.
  foreach ($elements as $element) {
    $new_element = $element;
    $new_element['#responsive_image_style_id'] = $this->responsiveImageStyleStorage
      ->load($this
      ->getSetting('thumb_style'))
      ->id();
    $thumb_elements[] = $new_element;
  }
  return [
    '#theme' => 'galleryformatter',
    '#slides' => $elements,
    '#thumbs' => $thumb_elements,
    '#settings' => $this
      ->getSettings(),
    '#dimensions' => '',
  ];
}