You are here

public function SlickFileFormatterBase::buildElements in Slick Carousel 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/SlickFileFormatterBase.php \Drupal\slick\Plugin\Field\FieldFormatter\SlickFileFormatterBase::buildElements()

Build the slick carousel elements.

1 call to SlickFileFormatterBase::buildElements()
SlickFileFormatterBase::viewElements in src/Plugin/Field/FieldFormatter/SlickFileFormatterBase.php

File

src/Plugin/Field/FieldFormatter/SlickFileFormatterBase.php, line 82

Class

SlickFileFormatterBase
Base class for slick image and file ER formatters.

Namespace

Drupal\slick\Plugin\Field\FieldFormatter

Code

public function buildElements(array &$build, $files) {
  $settings =& $build['settings'];
  $item_id = $settings['item_id'];
  $tn_caption = empty($settings['thumbnail_caption']) ? NULL : $settings['thumbnail_caption'];
  $media = method_exists($this, 'getMediaItem');
  foreach ($files as $delta => $file) {
    $settings['delta'] = $delta;
    $settings['type'] = 'image';

    /** @var Drupal\image\Plugin\Field\FieldType\ImageItem $item */
    $item = $file->_referringItem;
    $settings['file_tags'] = $file
      ->getCacheTags();
    $settings['uri'] = $file
      ->getFileUri();
    $element = [
      'item' => $item,
      'settings' => $settings,
    ];

    // If imported Drupal\blazy\Dejavu\BlazyVideoTrait.
    if ($media) {
      if (!empty($this
        ->getImageItem($item))) {
        $element['item'] = $this
          ->getImageItem($item)['item'];
        $element['settings'] = array_merge($settings, $this
          ->getImageItem($item)['settings']);
      }
      $this
        ->getMediaItem($element, $file);
      $settings = $element['settings'];
    }

    // Image with responsive image, lazyLoad, and lightbox supports.
    $element[$item_id] = $this->formatter
      ->getImage($element);
    if (!empty($settings['caption'])) {
      foreach ($settings['caption'] as $caption) {
        $element['caption'][$caption] = empty($element['item']->{$caption}) ? [] : [
          '#markup' => Xss::filterAdmin($element['item']->{$caption}),
        ];
      }
    }

    // Build individual slick item.
    $build['items'][$delta] = $element;

    // Build individual slick thumbnail.
    if (!empty($settings['nav'])) {
      $thumb = [
        'settings' => $settings,
      ];

      // Thumbnail usages: asNavFor pagers, dot, arrows, photobox thumbnails.
      $thumb[$item_id] = empty($settings['thumbnail_style']) ? [] : $this->formatter
        ->getThumbnail($settings, $element['item']);
      $thumb['caption'] = empty($element['item']->{$tn_caption}) ? [] : [
        '#markup' => Xss::filterAdmin($element['item']->{$tn_caption}),
      ];
      $build['thumb']['items'][$delta] = $thumb;
      unset($thumb);
    }
    unset($element);
  }
}