You are here

protected function JuiceboxFieldFormatter::buildGallery in Juicebox HTML5 Responsive Image Galleries 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/JuiceboxFieldFormatter.php \Drupal\juicebox\Plugin\Field\FieldFormatter\JuiceboxFieldFormatter::buildGallery()

Utility to build a Juicebox gallery based on field formatter data.

Parameters

Drupal\juicebox\JuiceboxGalleryInterface $gallery: An initialized Juicebox gallery object.

Drupal\Core\Field\FieldItemListInterface $items: A list of field items that contain file data for the gallery.

1 call to JuiceboxFieldFormatter::buildGallery()
JuiceboxFieldFormatter::viewElements in src/Plugin/Field/FieldFormatter/JuiceboxFieldFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/JuiceboxFieldFormatter.php, line 284

Class

JuiceboxFieldFormatter
Plugin implementation of the 'juicebox' formatter.

Namespace

Drupal\juicebox\Plugin\Field\FieldFormatter

Code

protected function buildGallery(JuiceboxGalleryInterface $gallery, FieldItemListInterface $items) {

  // Get settings.
  $settings = $this
    ->getSettings();

  // Iterate over items and extract image data.
  foreach ($items as $item) {
    if ($item
      ->getPluginId() === 'field_item:entity_reference') {
      $item = $item->entity->field_media_image[0];
    }
    if ($item
      ->isDisplayed() && !empty($item->target_id)) {

      // Calculate the source data that Juicebox requires.
      $src_data = $this->juicebox
        ->styleImageSrcData($item->entity, $settings['image_style'], $item->entity, $settings['thumb_style'], $settings);

      // Short-circut this iteration if skipping an incompatible file.
      if (!$src_data['juicebox_compatible'] && $settings['incompatible_file_action'] == 'skip') {
        continue;
      }

      // Set the image title. If we have an incompatible file and are
      // configured to show a link, set the title text as the link.
      if (!$src_data['juicebox_compatible'] && $settings['incompatible_file_action'] == 'show_icon_and_link') {
        $anchor = !empty($item->description) ? $item->description : $item->entity
          ->get('filename')->value;
        $title = $this->linkGenerator
          ->generate($anchor, Url::fromUri($src_data['linkURL']));
      }
      else {
        $title = $this
          ->getFieldText($item, $settings['title_source']);
      }

      // Set the image caption.
      $caption = $this
        ->getFieldText($item, $settings['caption_source']);

      // Add this image to the gallery.
      $gallery
        ->addImage($src_data, $title, $caption);
    }
  }

  // Run common build tasks. This is also where the general settings are
  // applied.
  $this->juicebox
    ->runCommonBuild($gallery, $settings, $items);
}