You are here

public function LightgalleryFormatter::viewElements in Lightgallery 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/LightgalleryFormatter.php, line 138

Class

LightgalleryFormatter
Light gallery formatter.

Namespace

Drupal\lightgallery\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {

  /*
   * @var \Drupal\image\Plugin\Field\FieldType\ImageItem $item
   */
  $item_list = [];
  $files = $this
    ->getEntitiesToView($items, $langcode);

  // Early opt-out if the field is empty.
  if (empty($files)) {
    return $item_list;
  }

  // Init lightgallery image style field.
  $lightgallery_image_style_field = new FieldLightgalleryImageStyle();

  // Fetch lightgallery image style.
  $lightgallery_image_style = $this->settings[$lightgallery_image_style_field
    ->getGroup()
    ->getName()][$lightgallery_image_style_field
    ->getName()];

  // Init thumb image style field.
  $thumb_image_style_field = new FieldThumbImageStyle();

  // Fetch thumb image style.
  $thumb_image_style = $this->settings[$thumb_image_style_field
    ->getGroup()
    ->getName()][$thumb_image_style_field
    ->getName()];

  // Init title source field.
  $title_source_field = new FieldTitleSource();
  $title_source = $this->settings[$title_source_field
    ->getGroup()
    ->getName()][$title_source_field
    ->getName()];
  foreach ($files as $file) {
    if ($uri = $file
      ->getFileUri()) {

      // The reffering item is the image.
      $item = $file->_referringItem;

      // Load image urls.
      if (!empty($lightgallery_image_style)) {
        $item_detail['slide'] = $item_detail['thumb'] = ImageStyle::load($lightgallery_image_style)
          ->buildUrl($uri);
      }
      else {
        $item_detail['slide'] = $item_detail['thumb'] = file_create_url($uri);
      }

      // If image styles are different, also load thumb.
      if ($thumb_image_style != $lightgallery_image_style) {
        if (!empty($thumb_image_style)) {

          // Load thumb url.
          $item_detail['thumb'] = ImageStyle::load($thumb_image_style)
            ->buildUrl($uri);
        }
        else {
          $item_detail['thumb'] = file_create_url($uri);
        }
      }
      if (!empty($title_source) && !empty($item->{$title_source})) {

        // Set title of slide.
        $item_detail['title'] = [
          '#markup' => Xss::filterAdmin($item->{$title_source}),
        ];
      }
    }
    $item_list[] = $item_detail;
  }

  // Flatten settings array.
  $options = LightgalleryManager::flattenArray($this->settings);

  // Set unique id, so that multiple instances on one page can be created.
  $unique_id = uniqid();

  // Load libraries.
  $lightgallery_optionset = new LightgalleryOptionset($options);
  $lightgallery_manager = new LightgalleryManager($lightgallery_optionset);

  // Build render array.
  $content = [
    '#theme' => 'lightgallery',
    '#items' => $item_list,
    '#id' => $unique_id,
    '#attached' => $lightgallery_manager
      ->loadLibraries($unique_id),
  ];
  return $content;
}